import { create } from "zustand";

interface SimulationUsageRecord {
    activeTime: number;
    isActive: boolean;
    idleTime: number;
    type: "roboticArm" | "vehicle" | "transfer" | "storageUnit" | "crane" | "human" | "machine";
    assetId: string;
}

// Product → holds multiple usage records
interface ProductSimulation {
    productId: string;
    simulateData: SimulationUsageRecord[];
}

// Version → holds multiple products
interface VersionSimulation {
    versionId: string;
    products: ProductSimulation[];
}

// Project → holds multiple versions
interface ProjectSimulation {
    projectId: string | undefined;
    versions: VersionSimulation[];
}
// or same file

interface SimulationManagerStore {
    simulationRecords: ProjectSimulation[];
    setSimulationRecords: (simulateData: ProjectSimulation[]) => void;
    addSimulationRecord: (projectId: string | undefined, versionId: string, productId: string, record: SimulationUsageRecord) => void;
    addSimulationRecords: (projectId: string | undefined, versionId: string, productId: string, records: SimulationUsageRecord[]) => void;
    resetProductRecords: (projectId: string, versionId: string, productId: string) => void;
    getProjectById: (projectId: string | undefined) => ProjectSimulation | undefined;
    getVersionById: (projectId: string | undefined, versionId: string) => VersionSimulation | undefined;
    getProductById: (projectId: string | undefined, versionId: string, productId: string) => ProductSimulation | undefined;
}

export const useSimulationManager = create<SimulationManagerStore>((set, get) => ({
    simulationRecords: [],

    addSimulationRecord: (projectId, versionId, productId, record) =>
        set((state) => {
            const projects = state.simulationRecords.map((project) => {
                if (project.projectId !== projectId) return project;

                return {
                    ...project,
                    versions: project.versions.map((version) => {
                        if (version.versionId !== versionId) return version;

                        return {
                            ...version,
                            products: version.products.map((product) => (product.productId === productId ? { ...product, simulateData: [...product.simulateData, record] } : product)),
                        };
                    }),
                };
            });

            // If project doesn't exist, create it
            if (!state.simulationRecords.find((p) => p.projectId === projectId)) {
                projects.push({
                    projectId,
                    versions: [
                        {
                            versionId,
                            products: [{ productId, simulateData: [record] }],
                        },
                    ],
                });
            } else {
                const project = projects.find((p) => p.projectId === projectId)!;
                if (!project.versions.find((v) => v.versionId === versionId)) {
                    project.versions.push({
                        versionId,
                        products: [{ productId, simulateData: [record] }],
                    });
                } else {
                    const version = project.versions.find((v) => v.versionId === versionId)!;
                    if (!version.products.find((p) => p.productId === productId)) {
                        version.products.push({ productId, simulateData: [record] });
                    }
                }
            }

            return { simulationRecords: projects };
        }),
    addSimulationRecords: (projectId, versionId, productId, records) =>
        set((state) => {
            const projects = state.simulationRecords.map((project) => {
                if (project.projectId !== projectId) return project;

                return {
                    ...project,
                    versions: project.versions.map((version) => {
                        if (version.versionId !== versionId) return version;

                        return {
                            ...version,
                            products: version.products.map((product) => (product.productId === productId ? { ...product, simulateData: [...product.simulateData, ...records] } : product)),
                        };
                    }),
                };
            });

            // same creation logic for new project/version/product
            if (!state.simulationRecords.find((p) => p.projectId === projectId)) {
                projects.push({
                    projectId,
                    versions: [
                        {
                            versionId,
                            products: [{ productId, simulateData: [...records] }],
                        },
                    ],
                });
            } else {
                const project = projects.find((p) => p.projectId === projectId)!;
                if (!project.versions.find((v) => v.versionId === versionId)) {
                    project.versions.push({
                        versionId,
                        products: [{ productId, simulateData: [...records] }],
                    });
                } else {
                    const version = project.versions.find((v) => v.versionId === versionId)!;
                    if (!version.products.find((p) => p.productId === productId)) {
                        version.products.push({ productId, simulateData: [...records] });
                    }
                }
            }

            return { simulationRecords: projects };
        }),

    resetProductRecords: (projectId, versionId, productId) =>
        set((state) => {
            const projects = state.simulationRecords.map((project) => {
                if (project.projectId !== projectId) return project;

                return {
                    ...project,
                    versions: project.versions.map((version) => {
                        if (version.versionId !== versionId) return version;

                        return {
                            ...version,
                            products: version.products.map((product) => (product.productId === productId ? { ...product, simulateData: [] } : product)),
                        };
                    }),
                };
            });

            return { simulationRecords: projects };
        }),
    setSimulationRecords: (simulateData) => {
        set({ simulationRecords: simulateData });
    },
    getProjectById: (projectId: string | undefined) => {
        return get().simulationRecords.find((p: ProjectSimulation) => p.projectId === projectId);
    },

    getVersionById: (projectId: string | undefined, versionId: string) => {
        const project = get().simulationRecords.find((p: ProjectSimulation) => p.projectId === projectId);
        return project?.versions.find((v: VersionSimulation) => v.versionId === versionId);
    },

    getProductById: (projectId: string | undefined, versionId: string, productId: string) => {
        const project = get().simulationRecords.find((p: ProjectSimulation) => p.projectId === projectId);
        const version = project?.versions.find((v: VersionSimulation) => v.versionId === versionId);
        return version?.products.find((p: ProductSimulation) => p.productId === productId);
    },
}));
///////////////////////////
