#pragma once
#include <Ogre.h>
#include <flecs.h>
#include <string>
#include "EulerUtils.hpp"

// Component definitions
struct TransformComponent {
    Ogre::SceneNode* node;
    Ogre::Vector3 position;
    Ogre::Quaternion orientation;
    Ogre::Vector3 scale;

    TransformComponent() : node(nullptr), position(Ogre::Vector3::ZERO),
                           orientation(Ogre::Quaternion::IDENTITY),
                           scale(Ogre::Vector3::UNIT_SCALE) {}

    void applyToNode() {
        if (node) {
            node->setPosition(position);
            node->setOrientation(orientation);
            node->setScale(scale);
        }
    }

    void updateFromNode() {
        if (node) {
            position = node->getPosition();
            orientation = node->getOrientation();
            scale = node->getScale();
        }
    }

    void setEulerDegrees(const Ogre::Vector3& degrees) {
        orientation = EulerUtils::fromEulerDegrees(degrees);
        applyToNode();
    }

    Ogre::Vector3 getEulerDegrees() const {
        return EulerUtils::toEulerDegrees(orientation);
    }

    void rotateEulerDegrees(const Ogre::Vector3& deltaDegrees) {
        Ogre::Vector3 current = getEulerDegrees();
        Ogre::Vector3 newEuler = current + deltaDegrees;
        newEuler = EulerUtils::normalizeEulerDegrees(newEuler);
        setEulerDegrees(newEuler);
    }
};

struct RenderableComponent {
    Ogre::Entity* entity;
    std::string meshName;

    RenderableComponent() : entity(nullptr) {}
    RenderableComponent(const std::string& mesh) : meshName(mesh), entity(nullptr) {}
};

struct EditorTag {
    bool isSelected;
    EditorTag() : isSelected(false) {}
};

struct EntityNameComponent {
    std::string name;
    EntityNameComponent() : name("Entity") {}
    EntityNameComponent(const std::string& n) : name(n) {}
};

// Register components with flecs 4.1.4
inline void registerComponents(flecs::world& world) {
    // Simple component registration - this is all that's needed
    world.component<TransformComponent>();
    world.component<RenderableComponent>();
    world.component<EditorTag>();
    world.component<EntityNameComponent>();
}
