import { create } from 'zustand';

// 地块接口
export interface LandParcel {
  id: string;
  name: string;
  area: number;
  location: string;
  soil_type: string;
  land_use_type: string;
  status: 'active' | 'inactive' | 'reserved';
  coordinates?: string | null;
  owner_id?: string | null;
  created_at: string;
  updated_at: string;
}

// 土壤类型接口
export interface SoilType {
  id: string;
  name: string;
  description?: string | null;
  ph_level?: number | null;
  organic_matter?: number | null;
  nitrogen_content?: number | null;
  phosphorus_content?: number | null;
  potassium_content?: number | null;
  created_at: string;
}

// Land state interface
export interface LandState {
  landParcels: LandParcel[];
  currentLandParcel: LandParcel | null;
  soilTypes: SoilType[];
  currentSoilType: SoilType | null;

  // Actions
  setLandParcels: (landParcels: LandParcel[]) => void;
  setCurrentLandParcel: (landParcel: LandParcel | null) => void;
  setSoilTypes: (soilTypes: SoilType[]) => void;
  setCurrentSoilType: (soilType: SoilType | null) => void;

  // Getters
  getLandParcels: () => LandParcel[];
  getCurrentLandParcel: () => LandParcel | null;
  getSoilTypes: () => SoilType[];
  getCurrentSoilType: () => SoilType | null;
}

// Create Land store
export const useLandStore = create<LandState>((set, get) => ({
  landParcels: [],
  currentLandParcel: null,
  soilTypes: [],
  currentSoilType: null,

  setLandParcels: (landParcels: LandParcel[]) => {
    set({ landParcels });
  },

  setCurrentLandParcel: (landParcel: LandParcel | null) => {
    set({ currentLandParcel: landParcel });
  },

  setSoilTypes: (soilTypes: SoilType[]) => {
    set({ soilTypes });
  },

  setCurrentSoilType: (soilType: SoilType | null) => {
    set({ currentSoilType: soilType });
  },

  getLandParcels: () => {
    return get().landParcels;
  },

  getCurrentLandParcel: () => {
    return get().currentLandParcel;
  },

  getSoilTypes: () => {
    return get().soilTypes;
  },

  getCurrentSoilType: () => {
    return get().currentSoilType;
  },
}));

export default useLandStore;