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

interface VehicleStatus extends VehicleEventSchema {
    productId: string;
    isActive: boolean;
    idleTime: number;
    activeTime: number;
    currentLoad: number;
    distanceTraveled: number;
}

interface VehiclesStore {
    vehicles: VehicleStatus[];

    addVehicle: (productId: string, event: VehicleEventSchema) => void;
    removeVehicle: (modelUuid: string) => void;
    updateVehicle: (
        modelUuid: string,
        updates: Partial<Omit<VehicleStatus, 'modelUuid' | 'productId'>>
    ) => void;

    setVehicleActive: (modelUuid: string, isActive: boolean) => void;
    incrementVehicleLoad: (modelUuid: string, incrementBy: number) => void;
    decrementVehicleLoad: (modelUuid: string, decrementBy: number) => void;
    setVehicleState: (modelUuid: string, newState: VehicleStatus['state']) => void;
    incrementActiveTime: (modelUuid: string, incrementBy: number) => void;
    incrementIdleTime: (modelUuid: string, incrementBy: number) => void;

    getVehicleById: (modelUuid: string) => VehicleStatus | undefined;
    getVehiclesByProduct: (productId: string) => VehicleStatus[];
    getVehiclesByState: (state: string) => VehicleStatus[];
    getActiveVehicles: () => VehicleStatus[];
}

export const useVehicleStore = create<VehiclesStore>()(
    immer((set, get) => ({
        vehicles: [],

        addVehicle: (productId, event) => {
            set((state) => {
                state.vehicles.push({
                    ...event,
                    productId,
                    isActive: false,
                    idleTime: 0,
                    activeTime: 0,
                    currentLoad: 0,
                    distanceTraveled: 0,
                });
            });
        },

        removeVehicle: (modelUuid) => {
            set((state) => {
                state.vehicles = state.vehicles.filter(v => v.modelUuid !== modelUuid);
            });
        },

        updateVehicle: (modelUuid, updates) => {
            set((state) => {
                const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid);
                if (vehicle) {
                    Object.assign(vehicle, updates);
                }
            });
        },

        setVehicleActive: (modelUuid, isActive) => {
            set((state) => {
                const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid);
                if (vehicle) {
                    vehicle.isActive = isActive;
                }
            });
        },

        incrementVehicleLoad: (modelUuid, incrementBy) => {
            set((state) => {
                const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid);
                if (vehicle) {
                    vehicle.currentLoad += incrementBy;
                }
            });
        },

        decrementVehicleLoad: (modelUuid, decrementBy) => {
            set((state) => {
                const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid);
                if (vehicle) {
                    vehicle.currentLoad -= decrementBy;
                }
            });
        },

        setVehicleState: (modelUuid, newState) => {
            set((state) => {
                const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid);
                if (vehicle) {
                    vehicle.state = newState;
                }
            });
        },

        incrementActiveTime: (modelUuid, incrementBy) => {
            set((state) => {
                const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid);
                if (vehicle) {
                    vehicle.activeTime += incrementBy;
                }
            });
        },

        incrementIdleTime: (modelUuid, incrementBy) => {
            set((state) => {
                const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid);
                if (vehicle) {
                    vehicle.idleTime += incrementBy;
                }
            });
        },

        getVehicleById: (modelUuid) => {
            return get().vehicles.find(v => v.modelUuid === modelUuid);
        },

        getVehiclesByProduct: (productId) => {
            return get().vehicles.filter(v => v.productId === productId);
        },

        getVehiclesByState: (state) => {
            return get().vehicles.filter(v => v.state === state);
        },

        getActiveVehicles: () => {
            return get().vehicles.filter(v => v.isActive);
        }
    }))
);
