'use client';

import { useReducer } from 'react';
import { toast } from 'sonner';

export interface EvaluationFactor {
  id: string;
  name: string;
  value: number;
  weight: number;
  unit: string;
  optimalRange: [number, number];
  score: number;
}

export interface SuitabilityResult {
  fieldId: string;
  fieldName: string;
  totalScore: number;
  grade: '高度适宜' | '一般适宜' | '不适宜';
  factors: EvaluationFactor[];
  timestamp: string;
}

export interface CropRecommendationState {
  evaluationResults: SuitabilityResult[];
  selectedField: string;
  showKnowledgeBase: boolean;
}

export type CropRecommendAction =
  | { type: 'SET_SELECTED_FIELD'; payload: string }
  | { type: 'SET_SHOW_KNOWLEDGE_BASE'; payload: boolean }
  | { type: 'SET_EVALUATION_RESULTS'; payload: SuitabilityResult[] }
  | { type: 'UPDATE_EVALUATION_RESULT'; payload: SuitabilityResult };

const initialState: CropRecommendationState = {
  evaluationResults: [
    {
      fieldId: 'field-1',
      fieldName: '东区1号地',
      totalScore: 87,
      grade: '高度适宜',
      factors: [
        { id: 'ph', name: 'pH值', value: 6.5, weight: 20, unit: '', optimalRange: [6.0, 7.5], score: 95 },
        { id: 'organic', name: '有机质含量', value: 32, weight: 25, unit: 'g/kg', optimalRange: [25, 40], score: 90 },
        { id: 'depth', name: '土层厚度', value: 85, weight: 20, unit: 'cm', optimalRange: [60, 100], score: 88 },
        { id: 'nitrogen', name: '全氮', value: 1.8, weight: 10, unit: 'g/kg', optimalRange: [1.5, 2.5], score: 85 },
        { id: 'phosphorus', name: '全磷', value: 1.2, weight: 10, unit: 'g/kg', optimalRange: [1.0, 2.0], score: 80 },
        { id: 'potassium', name: '全钾', value: 18, weight: 10, unit: 'g/kg', optimalRange: [15, 25], score: 82 },
        { id: 'drainage', name: '排水性', value: 4, weight: 5, unit: '', optimalRange: [3, 5], score: 90 },
      ],
      timestamp: '2024-10-15 14:30',
    },
    {
      fieldId: 'field-2',
      fieldName: '西区2号地',
      totalScore: 72,
      grade: '一般适宜',
      factors: [
        { id: 'ph', name: 'pH值', value: 7.8, weight: 20, unit: '', optimalRange: [6.0, 7.5], score: 65 },
        { id: 'organic', name: '有机质含量', value: 22, weight: 25, unit: 'g/kg', optimalRange: [25, 40], score: 70 },
        { id: 'depth', name: '土层厚度', value: 55, weight: 20, unit: 'cm', optimalRange: [60, 100], score: 68 },
        { id: 'nitrogen', name: '全氮', value: 1.3, weight: 10, unit: 'g/kg', optimalRange: [1.5, 2.5], score: 72 },
        { id: 'phosphorus', name: '全磷', value: 0.9, weight: 10, unit: 'g/kg', optimalRange: [1.0, 2.0], score: 75 },
        { id: 'potassium', name: '全钾', value: 14, weight: 10, unit: 'g/kg', optimalRange: [15, 25], score: 78 },
        { id: 'drainage', name: '排水性', value: 3, weight: 5, unit: '', optimalRange: [3, 5], score: 85 },
      ],
      timestamp: '2024-10-15 14:28',
    },
    {
      fieldId: 'field-3',
      fieldName: '南区3号地',
      totalScore: 58,
      grade: '不适宜',
      factors: [
        { id: 'ph', name: 'pH值', value: 8.5, weight: 20, unit: '', optimalRange: [6.0, 7.5], score: 45 },
        { id: 'organic', name: '有机质含量', value: 15, weight: 25, unit: 'g/kg', optimalRange: [25, 40], score: 52 },
        { id: 'depth', name: '土层厚度', value: 42, weight: 20, unit: 'cm', optimalRange: [60, 100], score: 55 },
        { id: 'nitrogen', name: '全氮', value: 0.8, weight: 10, unit: 'g/kg', optimalRange: [1.5, 2.5], score: 60 },
        { id: 'phosphorus', name: '全磷', value: 0.6, weight: 10, unit: 'g/kg', optimalRange: [1.0, 2.0], score: 65 },
        { id: 'potassium', name: '全钾', value: 10, weight: 10, unit: 'g/kg', optimalRange: [15, 25], score: 58 },
        { id: 'drainage', name: '排水性', value: 2, weight: 5, unit: '', optimalRange: [3, 5], score: 70 },
      ],
      timestamp: '2024-10-15 14:25',
    },
  ],
  selectedField: 'field-1',
  showKnowledgeBase: false,
};

export function cropRecommendReducer(
  state: CropRecommendationState = initialState,
  action: CropRecommendAction
): CropRecommendationState {
  switch (action.type) {
    case 'SET_SELECTED_FIELD':
      return {
        ...state,
        selectedField: action.payload,
      };
    case 'SET_SHOW_KNOWLEDGE_BASE':
      return {
        ...state,
        showKnowledgeBase: action.payload,
      };
    case 'SET_EVALUATION_RESULTS':
      return {
        ...state,
        evaluationResults: action.payload,
      };
    case 'UPDATE_EVALUATION_RESULT':
      return {
        ...state,
        evaluationResults: state.evaluationResults.map(result =>
          result.fieldId === action.payload.fieldId ? action.payload : result
        ),
      };
    default:
      return state;
  }
}

export { initialState };