#include "CellGrid.hpp"
#include "../ui/ComponentRegistration.hpp"
#include "../ui/CellGridEditor.hpp"
#include "../ui/LotEditor.hpp"
#include "../ui/DistrictEditor.hpp"
#include "../ui/TownEditor.hpp"
#include "../ui/RoofEditor.hpp"
#include "../ui/RoomEditor.hpp"

#include "../ui/ClearAreaEditor.hpp"
#include "../ui/FurnitureTemplateEditor.hpp"

// Register CellGrid component
REGISTER_COMPONENT_GROUP("Cell Grid", "Cell Grid", CellGridComponent, CellGridEditor)
{
    registry.registerComponent<CellGridComponent>(
        "Cell Grid", "Cell Grid",
        std::make_unique<CellGridEditor>(),
        [sceneMgr](flecs::entity e) {
            if (!e.has<CellGridComponent>()) {
                e.set<CellGridComponent>({});
            }
        },
        [sceneMgr](flecs::entity e) {
            if (e.has<CellGridComponent>()) {
                e.remove<CellGridComponent>();
            }
        }
    );
}

// Register Lot component
REGISTER_COMPONENT_GROUP("Lot", "Cell Grid", LotComponent, LotEditor)
{
    registry.registerComponent<LotComponent>(
        "Lot", "Cell Grid",
        std::make_unique<LotEditor>(),
        [sceneMgr](flecs::entity e) {
            if (!e.has<LotComponent>()) {
                e.set<LotComponent>({});
            }
        },
        [sceneMgr](flecs::entity e) {
            if (e.has<LotComponent>()) {
                e.remove<LotComponent>();
            }
        }
    );
}

// Register District component
REGISTER_COMPONENT_GROUP("District", "Cell Grid", DistrictComponent, DistrictEditor)
{
    registry.registerComponent<DistrictComponent>(
        "District", "Cell Grid",
        std::make_unique<DistrictEditor>(),
        [sceneMgr](flecs::entity e) {
            if (!e.has<DistrictComponent>()) {
                e.set<DistrictComponent>({});
            }
        },
        [sceneMgr](flecs::entity e) {
            if (e.has<DistrictComponent>()) {
                e.remove<DistrictComponent>();
            }
        }
    );
}

// Register Town component
REGISTER_COMPONENT_GROUP("Town", "Cell Grid", TownComponent, TownEditor)
{
    registry.registerComponent<TownComponent>(
        "Town", "Cell Grid",
        std::make_unique<TownEditor>(),
        [sceneMgr](flecs::entity e) {
            if (!e.has<TownComponent>()) {
                e.set<TownComponent>({});
            }
        },
        [sceneMgr](flecs::entity e) {
            if (e.has<TownComponent>()) {
                e.remove<TownComponent>();
            }
        }
    );
}

// Register Roof component
REGISTER_COMPONENT_GROUP("Roof", "Cell Grid", RoofComponent, RoofEditor)
{
    registry.registerComponent<RoofComponent>(
        "Roof", "Cell Grid",
        std::make_unique<RoofEditor>(),
        [sceneMgr](flecs::entity e) {
            if (!e.has<RoofComponent>()) {
                e.set<RoofComponent>({});
            }
        },
        [sceneMgr](flecs::entity e) {
            if (e.has<RoofComponent>()) {
                e.remove<RoofComponent>();
            }
        }
    );
}

// Register Room component (in Room Layout group)
REGISTER_COMPONENT_GROUP("Room", "Room Layout", RoomComponent, RoomEditor)
{
    registry.registerComponent<RoomComponent>(
        "Room", "Room Layout",
        std::make_unique<RoomEditor>(),
        [sceneMgr](flecs::entity e) {
            if (!e.has<RoomComponent>()) {
                e.set<RoomComponent>({});
            }
        },
        [sceneMgr](flecs::entity e) {
            if (e.has<RoomComponent>()) {
                e.remove<RoomComponent>();
            }
        }
    );
}

// Register FurnitureTemplate component
REGISTER_COMPONENT_GROUP("Furniture Template", "Cell Grid", FurnitureTemplateComponent, FurnitureTemplateEditor)
{
    registry.registerComponent<FurnitureTemplateComponent>(
        "Furniture Template", "Cell Grid",
        std::make_unique<FurnitureTemplateEditor>(),
        [sceneMgr](flecs::entity e) {
            if (!e.has<FurnitureTemplateComponent>()) {
                e.set<FurnitureTemplateComponent>({});
            }
        },
        [sceneMgr](flecs::entity e) {
            if (e.has<FurnitureTemplateComponent>()) {
                e.remove<FurnitureTemplateComponent>();
            }
        }
    );
}

// Register ClearArea component (in Room Layout group)
REGISTER_COMPONENT_GROUP("Clear Area", "Room Layout", ClearAreaComponent, ClearAreaEditor)
{
    registry.registerComponent<ClearAreaComponent>(
        "Clear Area", "Room Layout",
        std::make_unique<ClearAreaEditor>(),
        [sceneMgr](flecs::entity e) {
            if (!e.has<ClearAreaComponent>()) {
                e.set<ClearAreaComponent>({});
            }
        },
        [sceneMgr](flecs::entity e) {
            if (e.has<ClearAreaComponent>()) {
                e.remove<ClearAreaComponent>();
            }
        }
    );
}

// Note: ExteriorGenerationComponent has been removed.
// Exterior generation now automatically runs at the end of the RoomLayoutSystem pipeline.
// This mirrors the original Lua behavior where create_exterior() was called after all rooms were defined.
