// ComponentFactory.h
#pragma once
#include <unordered_map>
#include <functional>
#include <memory>
#include <string>

#include "entity.h"
#include "component.h"
#include "components/audio_component.h"
#include "components/identity_component.h"
#include "components/render_component.h"
#include "components/transform_component.h"
#include "components/physics_component.h"
#include "components/shader_component.h"
#include "components/model_path_component.h"

class ComponentFactory {
public:
    static ComponentFactory& Get() {
        static ComponentFactory instance;
        return instance;
    }

    template<typename T>
    void EnregistrerComposant(const std::string& typeName) {
        m_creators[typeName] = [](std::shared_ptr<ecs::Entity> entity) -> std::shared_ptr<ecs::Component> {
            return entity->AddComponent<T>();
        };
    }

    std::shared_ptr<ecs::Component> CreerComposant(const std::string& typeName, 
                                                  std::shared_ptr<ecs::Entity> entity) {
        auto it = m_creators.find(typeName);
        if (it != m_creators.end()) {
            return it->second(entity);
        }
        return nullptr;
    }

private:
    std::unordered_map<std::string, std::function<std::shared_ptr<ecs::Component>(std::shared_ptr<ecs::Entity>)>> m_creators;
};

// Fonction d'initialisation pour enregistrer tous les composants
inline void EnregistrerTousLesComposants() {
    auto& factory = ComponentFactory::Get();
    factory.EnregistrerComposant<ecs::TransformComponent>("TransformComponent");
    factory.EnregistrerComposant<ecs::PhysicsComponent>("PhysicsComponent");
    factory.EnregistrerComposant<ecs::ShaderComponent>("ShaderComponent");
    factory.EnregistrerComposant<ecs::ModelPathComponent>("ModelPathComponent");
    factory.EnregistrerComposant<ecs::IdentityComponent>("IdentityComponent");
    factory.EnregistrerComposant<ecs::RenderComponent>("RenderComponent");
    factory.EnregistrerComposant<ecs::AudioComponent>("AudioComponent");
    // Ajouter d'autres composants ici
}
