import { create } from 'zustand';
import { immer } from 'zustand/middleware/immer';

interface ConveyorStore {
    conveyors: ConveyorStatus[];

    addConveyor: (productUuid: string, event: ConveyorEventSchema) => void;
    removeConveyor: (modelUuid: string) => void;
    updateConveyor: (
        modelUuid: string,
        updates: Partial<Omit<ConveyorStatus, 'modelUuid' | 'productUuid'>>
    ) => void;
    clearConveyors: () => void;

    setConveyorActive: (modelUuid: string, isActive: boolean) => void;
    setConveyorState: (modelUuid: string, newState: ConveyorStatus['state']) => void;
    setConveyorPaused: (modelUuid: string, isPaused: boolean) => void;

    incrementActiveTime: (modelUuid: string, incrementBy: number) => void;
    incrementIdleTime: (modelUuid: string, incrementBy: number) => void;

    getConveyorById: (modelUuid: string) => ConveyorStatus | undefined;
    getConveyorsByProduct: (productUuid: string) => ConveyorStatus[];
    getConveyorsByState: (state: string) => ConveyorStatus[];
    getActiveConveyors: () => ConveyorStatus[];
    getIdleConveyors: () => ConveyorStatus[];
}

export const createConveyorStore = () => {
    return create<ConveyorStore>()(
        immer((set, get) => ({
            conveyors: [],

            addConveyor: (productUuid, event) => {
                set((state) => {
                    const exists = state.conveyors.some(c => c.modelUuid === event.modelUuid);
                    if (!exists) {
                        state.conveyors.push({
                            ...event,
                            productUuid,
                            isActive: false,
                            isPaused: false,
                            idleTime: 0,
                            activeTime: 0,
                            state: 'idle',
                        });
                    }
                });
            },

            removeConveyor: (modelUuid) => {
                set((state) => {
                    state.conveyors = state.conveyors.filter(c => c.modelUuid !== modelUuid);
                });
            },

            updateConveyor: (modelUuid, updates) => {
                set((state) => {
                    const conveyor = state.conveyors.find(c => c.modelUuid === modelUuid);
                    if (conveyor) {
                        Object.assign(conveyor, updates);
                    }
                });
            },

            clearConveyors: () => {
                set((state) => {
                    state.conveyors = [];
                });
            },

            setConveyorActive: (modelUuid, isActive) => {
                set((state) => {
                    const conveyor = state.conveyors.find(c => c.modelUuid === modelUuid);
                    if (conveyor) {
                        conveyor.isActive = isActive;
                    }
                });
            },

            setConveyorState: (modelUuid, newState) => {
                set((state) => {
                    const conveyor = state.conveyors.find(c => c.modelUuid === modelUuid);
                    if (conveyor) {
                        conveyor.state = newState;
                    }
                });
            },

            setConveyorPaused: (modelUuid, isPaused) => {
                set((state) => {
                    const conveyor = state.conveyors.find(c => c.modelUuid === modelUuid);
                    if (conveyor) {
                        conveyor.isPaused = isPaused;
                    }
                });
            },

            incrementActiveTime: (modelUuid, incrementBy) => {
                set((state) => {
                    const conveyor = state.conveyors.find(c => c.modelUuid === modelUuid);
                    if (conveyor) {
                        conveyor.activeTime += incrementBy;
                    }
                });
            },

            incrementIdleTime: (modelUuid, incrementBy) => {
                set((state) => {
                    const conveyor = state.conveyors.find(c => c.modelUuid === modelUuid);
                    if (conveyor) {
                        conveyor.idleTime += incrementBy;
                    }
                });
            },

            getConveyorById: (modelUuid) => {
                return get().conveyors.find(c => c.modelUuid === modelUuid);
            },

            getConveyorsByProduct: (productUuid) => {
                return get().conveyors.filter(c => c.productUuid === productUuid);
            },

            getConveyorsByState: (state) => {
                return get().conveyors.filter(c => c.state === state);
            },

            getActiveConveyors: () => {
                return get().conveyors.filter(c => c.isActive);
            },

            getIdleConveyors: () => {
                return get().conveyors.filter(c => !c.isActive && c.state === 'idle');
            },
        }))
    )
}

export type ConveyorStoreType = ReturnType<typeof createConveyorStore>;