import { create } from 'zustand';

// 农机设备接口
export interface Machinery {
  id: string;
  name: string;
  type: string;
  model: string;
  status: 'active' | 'maintenance' | 'inactive' | 'repair';
  license_plate?: string | null;
  purchase_date?: string | null;
  last_maintenance_date?: string | null;
  next_maintenance_date?: string | null;
  operator_id?: string | null;
  location?: string | null;
  created_at: string;
  updated_at: string;
}

// 驾驶员接口
export interface Driver {
  id: string;
  name: string;
  phone: string;
  license_number: string;
  license_type: string;
  experience_years?: number | null;
  status: 'active' | 'inactive' | 'on_leave';
  hire_date?: string | null;
  created_at: string;
  updated_at: string;
}

// Machinery state interface
export interface MachineryState {
  machineries: Machinery[];
  currentMachinery: Machinery | null;
  drivers: Driver[];
  currentDriver: Driver | null;

  // Actions
  setMachineries: (machineries: Machinery[]) => void;
  setCurrentMachinery: (machinery: Machinery | null) => void;
  setDrivers: (drivers: Driver[]) => void;
  setCurrentDriver: (driver: Driver | null) => void;

  // Getters
  getMachineries: () => Machinery[];
  getCurrentMachinery: () => Machinery | null;
  getDrivers: () => Driver[];
  getCurrentDriver: () => Driver | null;
}

// Create Machinery store
export const useMachineryStore = create<MachineryState>((set, get) => ({
  machineries: [],
  currentMachinery: null,
  drivers: [],
  currentDriver: null,

  setMachineries: (machineries: Machinery[]) => {
    set({ machineries });
  },

  setCurrentMachinery: (machinery: Machinery | null) => {
    set({ currentMachinery: machinery });
  },

  setDrivers: (drivers: Driver[]) => {
    set({ drivers });
  },

  setCurrentDriver: (driver: Driver | null) => {
    set({ currentDriver: driver });
  },

  getMachineries: () => {
    return get().machineries;
  },

  getCurrentMachinery: () => {
    return get().currentMachinery;
  },

  getDrivers: () => {
    return get().drivers;
  },

  getCurrentDriver: () => {
    return get().currentDriver;
  },
}));

export default useMachineryStore;