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

interface MachineStore {
    machines: MachineStatus[];

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

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

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

    // Helpers
    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: [],

        // Actions
        addMachine: (productId, machine) => {
            set((state) => {
                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);
                }
            });
        },

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

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

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