export interface Land {
  id: string;
  code: string;
  name: string;
  area: number;
  location: string;
  soilType: string;
  plantingMode: string;
  tags: string[];
  status: string;
  description?: string;
  createdAt: string;
  updatedAt: string;
}

export interface LandTag {
  id: string;
  name: string;
  color: string;
  description?: string;
  createdAt: string;
}

export interface SoilType {
  id: string;
  name: string;
  key: string;
  color: string;
}

export interface PlantingMode {
  id: string;
  name: string;
  key: string;
  emoji: string;
}

export interface FilterCondition {
  soilTypes: string[];
  plantingModes: string[];
  tags: string[];
  minArea: string;
  maxArea: string;
  keyword: string;
}

export interface StatisticsResult {
  totalCount: number;
  totalArea: number;
  avgArea: number;
  maxArea: number;
  minArea: number;
  soilTypeDistribution: { name: string; count: number; area: number; color: string }[];
  plantingModeDistribution: { name: string; count: number; area: number; emoji: string }[];
  tagDistribution: { name: string; count: number; color: string }[];
}

export interface LandStatisticsState {
  fields: Land[];
  tags: LandTag[];
  soilTypes: SoilType[];
  plantingModes: PlantingMode[];
  filters: FilterCondition;
  statistics: StatisticsResult | null;
  chartType: 'bar' | 'pie';
}

export const initialState: LandStatisticsState = {
  fields: [],
  tags: [],
  soilTypes: [],
  plantingModes: [],
  filters: {
    soilTypes: [],
    plantingModes: [],
    tags: [],
    minArea: '',
    maxArea: '',
    keyword: '',
  },
  statistics: null,
  chartType: 'bar',
};

export type LandStatisticsAction =
  | { type: 'SET_FIELDS'; payload: Land[] }
  | { type: 'SET_TAGS'; payload: LandTag[] }
  | { type: 'SET_SOIL_TYPES'; payload: SoilType[] }
  | { type: 'SET_PLANTING_MODES'; payload: PlantingMode[] }
  | { type: 'SET_FILTERS'; payload: FilterCondition }
  | { type: 'UPDATE_FILTER'; payload: { key: keyof FilterCondition; value: any } }
  | { type: 'TOGGLE_ARRAY_FILTER'; payload: { key: 'soilTypes' | 'plantingModes' | 'tags'; value: string } }
  | { type: 'CLEAR_FILTERS' }
  | { type: 'SET_STATISTICS'; payload: StatisticsResult | null }
  | { type: 'SET_CHART_TYPE'; payload: 'bar' | 'pie' };

export function LandStatisticsReducer(state: LandStatisticsState, action: LandStatisticsAction): LandStatisticsState {
  switch (action.type) {
    case 'SET_FIELDS':
      return { ...state, fields: action.payload };
    case 'SET_TAGS':
      return { ...state, tags: action.payload };
    case 'SET_SOIL_TYPES':
      return { ...state, soilTypes: action.payload };
    case 'SET_PLANTING_MODES':
      return { ...state, plantingModes: action.payload };
    case 'SET_FILTERS':
      return { ...state, filters: action.payload };
    case 'UPDATE_FILTER':
      return { ...state, filters: { ...state.filters, [action.payload.key]: action.payload.value } };
    case 'TOGGLE_ARRAY_FILTER':
      const currentArray = state.filters[action.payload.key];
      const newArray = currentArray.includes(action.payload.value)
        ? currentArray.filter(v => v !== action.payload.value)
        : [...currentArray, action.payload.value];
      return { ...state, filters: { ...state.filters, [action.payload.key]: newArray } };
    case 'CLEAR_FILTERS':
      return {
        ...state,
        filters: {
          soilTypes: [],
          plantingModes: [],
          tags: [],
          minArea: '',
          maxArea: '',
          keyword: '',
        },
        statistics: null
      };
    case 'SET_STATISTICS':
      return { ...state, statistics: action.payload };
    case 'SET_CHART_TYPE':
      return { ...state, chartType: action.payload };
    default:
      return state;
  }
}