import { useCallback } from "react";
import { useParams } from "react-router-dom";
import { getUserData } from "../getUserData";
import { getZoneData } from "../../services/visulization/zone/getZones";
import { zoneCameraUpdate } from "../../services/visulization/zone/zoneCameraUpdation";
import { setAssetsApi } from "../../services/factoryBuilder/asset/floorAsset/setAssetsApi";
import { useSelectedZoneStore } from "../../store/visualization/useZoneStore";
import { useSubModuleStore } from "../../store/ui/useModuleStore";
import { useSceneContext } from "../../modules/scene/sceneContext";
import { useZoneAssetId } from "../../store/builder/store";

export const useZoneAssetHandlers = () => {
    const { projectId } = useParams();
    const { organization } = getUserData();

    const { selectedZone, setSelectedZone } = useSelectedZoneStore();
    const { setSubModule } = useSubModuleStore();
    const { zoneAssetId, setZoneAssetId } = useZoneAssetId();
    const { assetStore, zoneStore, versionStore } = useSceneContext();
    const { selectedVersion } = versionStore();
    const { setName } = assetStore();
    const { zones, setZoneName } = zoneStore();

    // 🔹 Zone selection
    const handleSelectZone = useCallback(
        async (id: string) => {
            if (selectedZone?.zoneUuid === id || id === "unassigned-zone") return;
            setSubModule("zoneProperties");

            const response = await getZoneData(id, organization, projectId, selectedVersion?.versionId || "");
            if (!response) return;

            setSelectedZone({
                zoneName: response.zoneName,
                activeSides: response.activeSides ?? [],
                panelOrder: response.panelOrder ?? [],
                lockedPanels: response.lockedPanels ?? [],
                widgets: response.widgets ?? [],
                zoneUuid: response.zoneUuid,
                zoneViewPortTarget: response.viewPortTarget ?? [],
                zoneViewPortPosition: response.viewPortPosition ?? [],
            });
        },
        [organization, projectId, selectedVersion, selectedZone?.zoneUuid, setSelectedZone, setSubModule]
    );

    // 🔹 Zone rename
    const handleZoneNameChange = useCallback(
        async (newName: string) => {
            const isDuplicate = zones.some((zone: any) => zone.zoneName?.trim().toLowerCase() === newName.trim().toLowerCase() && zone.zoneUuid !== selectedZone.zoneUuid);
            if (isDuplicate) {
                alert("Zone name already exists. Please choose a different name.");
                return;
            }

            const response = await zoneCameraUpdate({ zoneUuid: selectedZone.zoneUuid, zoneName: newName }, organization, projectId, selectedVersion?.versionId || "");

            if (response.message === "zone updated") {
                setSelectedZone((prev) => ({ ...prev, zoneName: newName }));
                setZoneName(selectedZone.zoneUuid, newName);
            }
        },
        [organization, projectId, selectedVersion, selectedZone, setSelectedZone, setZoneName, zones]
    );

    // 🔹 Asset selection
    const handleAssetClick = useCallback(
        (asset: ListAsset) => {
            setZoneAssetId(asset);
        },
        [setZoneAssetId]
    );

    // 🔹 Asset rename
    const handleZoneAssetName = useCallback(
        async (newName: string) => {
            if (!zoneAssetId?.id) return;
            const response = await setAssetsApi({
                modelUuid: zoneAssetId.id,
                modelName: newName,
                projectId,
                versionId: selectedVersion?.versionId || "",
            });
            setName(zoneAssetId.id, response.modelName);
        },
        [projectId, selectedVersion, setName, zoneAssetId]
    );

    return {
        handleSelectZone,
        handleZoneNameChange,
        handleAssetClick,
        handleZoneAssetName,
    };
};
