'use client';

export interface FieldData {
  id: string;
  name: string;
  area: number;
  location: string;
  soilType: string;
  ph: number;
  organicMatter: number;
  nitrogen: number;
  phosphorus: number;
  potassium: number;
  soilDepth: number;
  slope: number;
  currentCrop: string;
  yield: number;
  suitabilityScore: number;
  suitabilityGrade: '高度适宜' | '一般适宜' | '不适宜';
  irrigation: string;
  drainage: string;
}

export interface MultiDimensionState {
  selectedFields: string[];
  fieldsData: FieldData[];
}

export type MultiDimensionAction =
  | { type: 'SET_SELECTED_FIELDS'; payload: string[] }
  | { type: 'ADD_FIELD'; payload: string }
  | { type: 'REMOVE_FIELD'; payload: string }
  | { type: 'RESET_FIELDS' };

// 模拟地块数据
const initialFieldsData: FieldData[] = [
  {
    id: 'field-1',
    name: '东区1号地',
    area: 50.5,
    location: '东经120.15°, 北纬30.25°',
    soilType: '壤土',
    ph: 6.5,
    organicMatter: 32,
    nitrogen: 1.8,
    phosphorus: 1.2,
    potassium: 18,
    soilDepth: 85,
    slope: 3,
    currentCrop: '水稻',
    yield: 750,
    suitabilityScore: 87,
    suitabilityGrade: '高度适宜',
    irrigation: '喷灌',
    drainage: '良好',
  },
  {
    id: 'field-2',
    name: '西区2号地',
    area: 45.2,
    location: '东经120.12°, 北纬30.28°',
    soilType: '粘土',
    ph: 7.8,
    organicMatter: 22,
    nitrogen: 1.3,
    phosphorus: 0.9,
    potassium: 14,
    soilDepth: 55,
    slope: 5,
    currentCrop: '玉米',
    yield: 650,
    suitabilityScore: 72,
    suitabilityGrade: '一般适宜',
    irrigation: '滴灌',
    drainage: '中等',
  },
  {
    id: 'field-3',
    name: '南区3号地',
    area: 38.8,
    location: '东经120.18°, 北纬30.22°',
    soilType: '砂土',
    ph: 8.5,
    organicMatter: 15,
    nitrogen: 0.8,
    phosphorus: 0.6,
    potassium: 10,
    soilDepth: 42,
    slope: 8,
    currentCrop: '小麦',
    yield: 480,
    suitabilityScore: 58,
    suitabilityGrade: '不适宜',
    irrigation: '漫灌',
    drainage: '较差',
  },
  {
    id: 'field-4',
    name: '北区4号地',
    area: 55.0,
    location: '东经120.20°, 北纬30.30°',
    soilType: '壤土',
    ph: 6.8,
    organicMatter: 28,
    nitrogen: 1.6,
    phosphorus: 1.0,
    potassium: 16,
    soilDepth: 75,
    slope: 2,
    currentCrop: '大豆',
    yield: 380,
    suitabilityScore: 82,
    suitabilityGrade: '高度适宜',
    irrigation: '喷灌',
    drainage: '良好',
  },
];

export const initialState: MultiDimensionState = {
  selectedFields: ['field-1', 'field-2'],
  fieldsData: initialFieldsData,
};

export function multiDimensionReducer(
  state: MultiDimensionState,
  action: MultiDimensionAction
): MultiDimensionState {
  switch (action.type) {
    case 'SET_SELECTED_FIELDS':
      return { ...state, selectedFields: action.payload };

    case 'ADD_FIELD':
      if (state.selectedFields.length >= 4) {
        return state; // 最多只能同时对比4个地块
      }
      return { ...state, selectedFields: [...state.selectedFields, action.payload] };

    case 'REMOVE_FIELD':
      if (state.selectedFields.length <= 2) {
        return state; // 至少需要选择2个地块进行对比
      }
      return {
        ...state,
        selectedFields: state.selectedFields.filter(id => id !== action.payload)
      };

    case 'RESET_FIELDS':
      return { ...state, selectedFields: ['field-1', 'field-2'] };

    default:
      return state;
  }
}