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

interface StorageUnitStore {
    storageUnits: StorageUnitStatus[];

    // Actions
    addStorageUnit: (productId: string, storageUnit: StorageEventSchema) => void;
    removeStorageUnit: (modelUuid: string) => void;
    updateStorageUnit: (
        modelUuid: string,
        updates: Partial<Omit<StorageUnitStatus, 'modelUuid' | 'productId'>>
    ) => void;

    // Status updates
    setStorageUnitActive: (modelUuid: string, isActive: boolean) => void;
    setStorageUnitState: (modelUuid: string, newState: StorageUnitStatus['state']) => void;

    // Load updates
    updateStorageUnitLoad: (modelUuid: string, incrementBy: number) => void;

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

    // Helpers
    getStorageUnitById: (modelUuid: string) => StorageUnitStatus | undefined;
    getStorageUnitsByProduct: (productId: string) => StorageUnitStatus[];
    getStorageUnitsBystate: (state: string) => StorageUnitStatus[];
    getActiveStorageUnits: () => StorageUnitStatus[];
    getIdleStorageUnits: () => StorageUnitStatus[];
    getFullStorageUnits: () => StorageUnitStatus[];
    getEmptyStorageUnits: () => StorageUnitStatus[];
}

export const useStorageUnitStore = create<StorageUnitStore>()(
    immer((set, get) => ({
        storageUnits: [],

        // Actions
        addStorageUnit: (productId, storageUnit) => {
            set((state) => {
                state.storageUnits.push({
                    ...storageUnit,
                    productId,
                    isActive: false,
                    idleTime: 0,
                    activeTime: 0,
                    currentLoad: 0,
                    state: 'idle',
                });
            });
        },

        removeStorageUnit: (modelUuid) => {
            set((state) => {
                state.storageUnits = state.storageUnits.filter(s => s.modelUuid !== modelUuid);
            });
        },

        updateStorageUnit: (modelUuid, updates) => {
            set((state) => {
                const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
                if (unit) {
                    Object.assign(unit, updates);
                }
            });
        },

        // Status updates
        setStorageUnitActive: (modelUuid, isActive) => {
            set((state) => {
                const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
                if (unit) {
                    unit.isActive = isActive;
                }
            });
        },

        setStorageUnitState: (modelUuid, newState) => {
            set((state) => {
                const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
                if (unit) {
                    unit.state = newState;
                }
            });
        },

        // Load updates
        updateStorageUnitLoad: (modelUuid, incrementBy) => {
            set((state) => {
                const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
                if (unit) {
                    unit.currentLoad += incrementBy;
                }
            });
        },

        // Time tracking
        incrementActiveTime: (modelUuid, incrementBy) => {
            set((state) => {
                const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
                if (unit) {
                    unit.activeTime += incrementBy;
                }
            });
        },

        incrementIdleTime: (modelUuid, incrementBy) => {
            set((state) => {
                const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
                if (unit) {
                    unit.idleTime += incrementBy;
                }
            });
        },

        // Helpers
        getStorageUnitById: (modelUuid) => {
            return get().storageUnits.find(s => s.modelUuid === modelUuid);
        },

        getStorageUnitsByProduct: (productId) => {
            return get().storageUnits.filter(s => s.productId === productId);
        },

        getStorageUnitsBystate: (state) => {
            return get().storageUnits.filter(s => s.state === state);
        },

        getActiveStorageUnits: () => {
            return get().storageUnits.filter(s => s.isActive);
        },

        getIdleStorageUnits: () => {
            return get().storageUnits.filter(s => !s.isActive && s.state === 'idle');
        },

        getFullStorageUnits: () => {
            return get().storageUnits.filter(
                s => s.currentLoad >= s.point.action.storageCapacity
            );
        },

        getEmptyStorageUnits: () => {
            return get().storageUnits.filter(s => s.currentLoad === 0);
        },
    }))
);
