// SPDX-License-Identifier: Apache-2.0
// Copyright 2025 C++ Best Practices Example
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RESOURCE_MANAGER_HPP
#define RESOURCE_MANAGER_HPP

/**
 * @file resource_manager.hpp
 * @brief Header-only implementation of ResourceManager following C++ best practices
 * @author Generated Example
 * @date 2025
 */

#include <vector>
#include <memory>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <functional>

namespace best_practices {

/**
 * @brief A template-based resource manager demonstrating modern C++ practices
 * @tparam T The type of data to manage
 */
template<typename T>
class ResourceManager {
private:
    std::unique_ptr<std::vector<T>> data_;
    std::string name_;
    
public:
    // Type aliases for better readability
    using value_type = T;
    using size_type = std::size_t;
    using reference = T&;
    using const_reference = const T&;
    using iterator = typename std::vector<T>::iterator;
    using const_iterator = typename std::vector<T>::const_iterator;
    
    /**
     * @brief Constructor with parameter validation
     * @param name The name of the resource manager
     * @throws std::invalid_argument if name is empty
     */
    explicit ResourceManager(const std::string& name) 
        : data_(std::make_unique<std::vector<T>>()), name_(name) {
        if (name.empty()) {
            throw std::invalid_argument("Name cannot be empty");
        }
    }
    
    /**
     * @brief Constructor with initial capacity
     * @param name The name of the resource manager
     * @param initial_capacity Initial capacity for the container
     */
    ResourceManager(const std::string& name, size_type initial_capacity)
        : data_(std::make_unique<std::vector<T>>()), name_(name) {
        if (name.empty()) {
            throw std::invalid_argument("Name cannot be empty");
        }
        data_->reserve(initial_capacity);
    }
    
    // Rule of 5: Define copy/move constructors and assignment operators
    ResourceManager(const ResourceManager& other) 
        : data_(std::make_unique<std::vector<T>>(*other.data_)), 
          name_(other.name_) {}
    
    ResourceManager(ResourceManager&& other) noexcept 
        : data_(std::move(other.data_)), name_(std::move(other.name_)) {}
    
    ResourceManager& operator=(const ResourceManager& other) {
        if (this != &other) {
            data_ = std::make_unique<std::vector<T>>(*other.data_);
            name_ = other.name_;
        }
        return *this;
    }
    
    ResourceManager& operator=(ResourceManager&& other) noexcept {
        if (this != &other) {
            data_ = std::move(other.data_);
            name_ = std::move(other.name_);
        }
        return *this;
    }
    
    // Destructor (default is fine due to RAII)
    ~ResourceManager() = default;
    
    /**
     * @brief Add an element to the managed data
     * @param value The value to add
     */
    void push_back(const T& value) {
        data_->push_back(value);
    }
    
    /**
     * @brief Add an element to the managed data (move version)
     * @param value The value to move
     */
    void push_back(T&& value) {
        data_->push_back(std::move(value));
    }
    
    /**
     * @brief Emplace an element in-place
     * @tparam Args Argument types for construction
     * @param args Arguments to forward to T's constructor
     */
    template<typename... Args>
    void emplace_back(Args&&... args) {
        data_->emplace_back(std::forward<Args>(args)...);
    }
    
    /**
     * @brief Get the name of the resource manager
     * @return const reference to the name
     */
    const std::string& getName() const noexcept {
        return name_;
    }
    
    /**
     * @brief Get the size of managed data
     * @return size of the data vector
     */
    size_type size() const noexcept {
        return data_->size();
    }
    
    /**
     * @brief Check if the container is empty
     * @return true if empty, false otherwise
     */
    bool empty() const noexcept {
        return data_->empty();
    }
    
    /**
     * @brief Get element at index with bounds checking
     * @param index The index to access
     * @return const reference to the element
     * @throws std::out_of_range if index is invalid
     */
    const_reference at(size_type index) const {
        return data_->at(index);
    }
    
    /**
     * @brief Get element at index with bounds checking (non-const version)
     * @param index The index to access
     * @return reference to the element
     * @throws std::out_of_range if index is invalid
     */
    reference at(size_type index) {
        return data_->at(index);
    }
    
    /**
     * @brief Get element at index without bounds checking
     * @param index The index to access
     * @return const reference to the element
     */
    const_reference operator[](size_type index) const noexcept {
        return (*data_)[index];
    }
    
    /**
     * @brief Get element at index without bounds checking (non-const version)
     * @param index The index to access
     * @return reference to the element
     */
    reference operator[](size_type index) noexcept {
        return (*data_)[index];
    }
    
    // Iterator support
    iterator begin() noexcept { return data_->begin(); }
    const_iterator begin() const noexcept { return data_->begin(); }
    const_iterator cbegin() const noexcept { return data_->cbegin(); }
    
    iterator end() noexcept { return data_->end(); }
    const_iterator end() const noexcept { return data_->end(); }
    const_iterator cend() const noexcept { return data_->cend(); }
    
    /**
     * @brief Apply a function to all elements
     * @tparam Func Function type
     * @param func Function to apply
     */
    template<typename Func>
    void for_each(Func&& func) const {
        std::for_each(data_->begin(), data_->end(), std::forward<Func>(func));
    }
    
    /**
     * @brief Apply a predicate and return matching elements
     * @tparam Predicate Predicate function type
     * @param pred Predicate to apply
     * @return Vector containing matching elements
     */
    template<typename Predicate>
    std::vector<T> filter(Predicate&& pred) const {
        std::vector<T> result;
        std::copy_if(data_->begin(), data_->end(), std::back_inserter(result),
                     std::forward<Predicate>(pred));
        return result;
    }
    
    /**
     * @brief Transform elements using a function
     * @tparam Func Function type
     * @param func Transformation function
     * @return Vector containing transformed elements
     */
    template<typename Func>
    auto transform(Func&& func) const -> std::vector<decltype(func(std::declval<T>()))> {
        using result_type = decltype(func(std::declval<T>()));
        std::vector<result_type> result;
        result.reserve(data_->size());
        
        std::transform(data_->begin(), data_->end(), std::back_inserter(result),
                       std::forward<Func>(func));
        return result;
    }
    
    /**
     * @brief Clear all elements
     */
    void clear() noexcept {
        data_->clear();
    }
    
    /**
     * @brief Reserve capacity
     * @param capacity New capacity
     */
    void reserve(size_type capacity) {
        data_->reserve(capacity);
    }
};

/**
 * @brief Factory function for creating ResourceManager instances
 * @tparam T The type to manage
 * @param name The name for the resource manager
 * @return unique_ptr to ResourceManager
 */
template<typename T>
std::unique_ptr<ResourceManager<T>> make_resource_manager(const std::string& name) {
    return std::make_unique<ResourceManager<T>>(name);
}

/**
 * @brief Factory function with initial capacity
 * @tparam T The type to manage
 * @param name The name for the resource manager
 * @param initial_capacity Initial capacity
 * @return unique_ptr to ResourceManager
 */
template<typename T>
std::unique_ptr<ResourceManager<T>> make_resource_manager(const std::string& name, 
                                                          std::size_t initial_capacity) {
    return std::make_unique<ResourceManager<T>>(name, initial_capacity);
}

} // namespace best_practices

#endif // RESOURCE_MANAGER_HPP