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

interface MachineStore {
    machines: MachineStatus[];

    addMachine: (productId: string, machine: MachineEventSchema) => void;
    removeMachine: (modelUuid: string) => void;
    updateMachine: (
        modelUuid: string,
        updates: Partial<Omit<MachineStatus, 'modelUuid' | 'productId'>>
    ) => void;
    clearMachines: () => void;

    addCurrentAction: (modelUuid: string, actionUuid: string) => void;
    removeCurrentAction: (modelUuid: string) => void;

    setMachineActive: (modelUuid: string, isActive: boolean) => void;
    setMachineState: (modelUuid: string, newState: MachineStatus['state']) => void;

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

    getMachineById: (modelUuid: string) => MachineStatus | undefined;
    getMachinesByProduct: (productId: string) => MachineStatus[];
    getMachinesBystate: (state: string) => MachineStatus[];
    getActiveMachines: () => MachineStatus[];
    getIdleMachines: () => MachineStatus[];
}

export const useMachineStore = create<MachineStore>()(
    immer((set, get) => ({
        machines: [],

        addMachine: (productId, machine) => {
            set((state) => {
                const exists = state.machines.some(m => m.modelUuid === machine.modelUuid);
                if (!exists) {
                    state.machines.push({
                        ...machine,
                        productId,
                        isActive: false,
                        idleTime: 0,
                        activeTime: 0,
                        state: 'idle',
                    });
                }
            });
        },

        removeMachine: (modelUuid) => {
            set((state) => {
                state.machines = state.machines.filter(m => m.modelUuid !== modelUuid);
            });
        },

        updateMachine: (modelUuid, updates) => {
            set((state) => {
                const machine = state.machines.find(m => m.modelUuid === modelUuid);
                if (machine) {
                    Object.assign(machine, updates);
                }
            });
        },

        clearMachines: () => {
            set((state) => {
                state.machines = [];
            });
        },

        addCurrentAction: (modelUuid) => {
            set((state) => {
                const armBot = state.machines.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    const action = armBot.point.action;
                    if (action) {
                        armBot.currentAction = {
                            actionUuid: action.actionUuid,
                            actionName: action.actionName,
                            materialType: null
                        };
                    }
                }
            });
        },

        removeCurrentAction: (modelUuid) => {
            set((state) => {
                const armBot = state.machines.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    armBot.currentAction = undefined;
                }
            });
        },

        setMachineActive: (modelUuid, isActive) => {
            set((state) => {
                const machine = state.machines.find(m => m.modelUuid === modelUuid);
                if (machine) {
                    machine.isActive = isActive;
                }
            });
        },

        setMachineState: (modelUuid, newState) => {
            set((state) => {
                const machine = state.machines.find(m => m.modelUuid === modelUuid);
                if (machine) {
                    machine.state = newState;
                }
            });
        },

        incrementActiveTime: (modelUuid, incrementBy) => {
            set((state) => {
                const machine = state.machines.find(m => m.modelUuid === modelUuid);
                if (machine) {
                    machine.activeTime += incrementBy;
                }
            });
        },

        incrementIdleTime: (modelUuid, incrementBy) => {
            set((state) => {
                const machine = state.machines.find(m => m.modelUuid === modelUuid);
                if (machine) {
                    machine.idleTime += incrementBy;
                }
            });
        },

        getMachineById: (modelUuid) => {
            return get().machines.find(m => m.modelUuid === modelUuid);
        },

        getMachinesByProduct: (productId) => {
            return get().machines.filter(m => m.productId === productId);
        },

        getMachinesBystate: (state) => {
            return get().machines.filter(m => m.state === state);
        },

        getActiveMachines: () => {
            return get().machines.filter(m => m.isActive);
        },

        getIdleMachines: () => {
            return get().machines.filter(m => !m.isActive && m.state === 'idle');
        },
    }))
);
