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

interface ConveyorStore {
    conveyors: ConveyorStatus[];

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

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

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

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

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

        addConveyor: (productId, event) => {
            set((state) => {
                state.conveyors.push({
                    ...event,
                    productId,
                    isActive: 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);
                }
            });
        },

        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;
                }
            });
        },

        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: (productId) => {
            return get().conveyors.filter(c => c.productId === productId);
        },

        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');
        },
    }))
);
