import { create } from 'zustand';

// 灌溉系统接口
export interface IrrigationSystem {
  id: string;
  name: string;
  type: string;
  status: 'active' | 'inactive' | 'maintenance';
  location: string;
  capacity: number;
  flow_rate: number;
  installation_date?: string | null;
  last_maintenance_date?: string | null;
  created_at: string;
  updated_at: string;
}

// 灌溉区域接口
export interface IrrigationZone {
  id: string;
  name: string;
  system_id: string;
  area: number;
  crop_type: string;
  soil_moisture_threshold: number;
  irrigation_duration: number;
  status: 'active' | 'inactive';
  coordinates?: string | null;
  created_at: string;
  updated_at: string;
}

// 灌溉调度接口
export interface IrrigationSchedule {
  id: string;
  zone_id: string;
  start_time: string;
  duration: number;
  frequency: string;
  water_amount: number;
  status: 'pending' | 'active' | 'completed' | 'cancelled';
  created_at: string;
  updated_at: string;
}

// Irrigation state interface
export interface IrrigationState {
  irrigationSystems: IrrigationSystem[];
  currentSystem: IrrigationSystem | null;
  irrigationZones: IrrigationZone[];
  currentZone: IrrigationZone | null;
  irrigationSchedules: IrrigationSchedule[];
  currentSchedule: IrrigationSchedule | null;

  // Actions
  setIrrigationSystems: (systems: IrrigationSystem[]) => void;
  setCurrentSystem: (system: IrrigationSystem | null) => void;
  setIrrigationZones: (zones: IrrigationZone[]) => void;
  setCurrentZone: (zone: IrrigationZone | null) => void;
  setIrrigationSchedules: (schedules: IrrigationSchedule[]) => void;
  setCurrentSchedule: (schedule: IrrigationSchedule | null) => void;

  // Getters
  getIrrigationSystems: () => IrrigationSystem[];
  getCurrentSystem: () => IrrigationSystem | null;
  getIrrigationZones: () => IrrigationZone[];
  getCurrentZone: () => IrrigationZone | null;
  getIrrigationSchedules: () => IrrigationSchedule[];
  getCurrentSchedule: () => IrrigationSchedule | null;
}

// Create Irrigation store
export const useIrrigationStore = create<IrrigationState>((set, get) => ({
  irrigationSystems: [],
  currentSystem: null,
  irrigationZones: [],
  currentZone: null,
  irrigationSchedules: [],
  currentSchedule: null,

  setIrrigationSystems: (systems: IrrigationSystem[]) => {
    set({ irrigationSystems: systems });
  },

  setCurrentSystem: (system: IrrigationSystem | null) => {
    set({ currentSystem: system });
  },

  setIrrigationZones: (zones: IrrigationZone[]) => {
    set({ irrigationZones: zones });
  },

  setCurrentZone: (zone: IrrigationZone | null) => {
    set({ currentZone: zone });
  },

  setIrrigationSchedules: (schedules: IrrigationSchedule[]) => {
    set({ irrigationSchedules: schedules });
  },

  setCurrentSchedule: (schedule: IrrigationSchedule | null) => {
    set({ currentSchedule: schedule });
  },

  getIrrigationSystems: () => {
    return get().irrigationSystems;
  },

  getCurrentSystem: () => {
    return get().currentSystem;
  },

  getIrrigationZones: () => {
    return get().irrigationZones;
  },

  getCurrentZone: () => {
    return get().currentZone;
  },

  getIrrigationSchedules: () => {
    return get().irrigationSchedules;
  },

  getCurrentSchedule: () => {
    return get().currentSchedule;
  },
}));

export default useIrrigationStore;