import { create } from 'zustand';

// 农业资产接口
export interface AgriculturalAsset {
  id: string;
  name: string;
  type: string;
  category: string;
  brand?: string | null;
  model?: string | null;
  serial_number?: string | null;
  purchase_date?: string | null;
  purchase_price?: number | null;
  current_value?: number | null;
  depreciation_rate?: number | null;
  status: 'active' | 'maintenance' | 'retired' | 'lost';
  location?: string | null;
  assigned_to?: string | null;
  warranty_expiry?: string | null;
  created_at: string;
  updated_at: string;
}

// 维护记录接口
export interface MaintenanceRecord {
  id: string;
  asset_id: string;
  maintenance_type: string;
  description?: string | null;
  cost?: number | null;
  performed_by?: string | null;
  performed_date?: string | null;
  next_maintenance_date?: string | null;
  status: 'scheduled' | 'in_progress' | 'completed' | 'cancelled';
  notes?: string | null;
  created_at: string;
  updated_at: string;
}

// 资产库存接口
export interface AssetInventory {
  id: string;
  asset_type: string;
  name: string;
  description?: string | null;
  quantity: number;
  unit: string;
  unit_cost?: number | null;
  total_value?: number | null;
  reorder_level?: number | null;
  location?: string | null;
  supplier?: string | null;
  last_updated?: string | null;
  created_at: string;
  updated_at: string;
}

// Asset state interface
export interface AssetState {
  assets: AgriculturalAsset[];
  currentAsset: AgriculturalAsset | null;
  maintenanceRecords: MaintenanceRecord[];
  currentMaintenance: MaintenanceRecord | null;
  assetInventory: AssetInventory[];
  currentInventory: AssetInventory | null;

  // Actions
  setAssets: (assets: AgriculturalAsset[]) => void;
  setCurrentAsset: (asset: AgriculturalAsset | null) => void;
  setMaintenanceRecords: (records: MaintenanceRecord[]) => void;
  setCurrentMaintenance: (record: MaintenanceRecord | null) => void;
  setAssetInventory: (inventory: AssetInventory[]) => void;
  setCurrentInventory: (inventory: AssetInventory | null) => void;

  // Getters
  getAssets: () => AgriculturalAsset[];
  getCurrentAsset: () => AgriculturalAsset | null;
  getMaintenanceRecords: () => MaintenanceRecord[];
  getCurrentMaintenance: () => MaintenanceRecord | null;
  getAssetInventory: () => AssetInventory[];
  getCurrentInventory: () => AssetInventory | null;
}

// Create Asset store
export const useAssetStore = create<AssetState>((set, get) => ({
  assets: [],
  currentAsset: null,
  maintenanceRecords: [],
  currentMaintenance: null,
  assetInventory: [],
  currentInventory: null,

  setAssets: (assets: AgriculturalAsset[]) => {
    set({ assets });
  },

  setCurrentAsset: (asset: AgriculturalAsset | null) => {
    set({ currentAsset: asset });
  },

  setMaintenanceRecords: (records: MaintenanceRecord[]) => {
    set({ maintenanceRecords: records });
  },

  setCurrentMaintenance: (record: MaintenanceRecord | null) => {
    set({ currentMaintenance: record });
  },

  setAssetInventory: (inventory: AssetInventory[]) => {
    set({ assetInventory: inventory });
  },

  setCurrentInventory: (inventory: AssetInventory | null) => {
    set({ currentInventory: inventory });
  },

  getAssets: () => {
    return get().assets;
  },

  getCurrentAsset: () => {
    return get().currentAsset;
  },

  getMaintenanceRecords: () => {
    return get().maintenanceRecords;
  },

  getCurrentMaintenance: () => {
    return get().currentMaintenance;
  },

  getAssetInventory: () => {
    return get().assetInventory;
  },

  getCurrentInventory: () => {
    return get().currentInventory;
  },
}));

export default useAssetStore;