/**
 * 多因子评价类型定义
 */

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 FactorWeight {
  id: string;
  name: string;
  weight: number;
  unit: string;
}

export interface BatchProgress {
  total: number;
  processed: number;
  highSuitability: number;
  mediumSuitability: number;
  lowSuitability: number;
  currentField: string;
}

// 作物相关类型
export interface Crop {
  id: string;
  cropName: string;
  category: '粮食作物' | '经济作物' | '蔬菜作物' | '果树作物';
  description: string;
  growthCycle: {
    days: number;
    seasons: string[];
  };
  soilRequirements: {
    ph: { optimal: [number, number]; acceptable: [number, number] };
    organicMatter: { optimal: [number, number]; acceptable: [number, number] };
    soilDepth: { optimal: [number, number]; acceptable: [number, number] };
    nitrogen: { optimal: [number, number]; acceptable: [number, number] };
    phosphorus: { optimal: [number, number]; acceptable: [number, number] };
    potassium: { optimal: [number, number]; acceptable: [number, number] };
    drainage: { optimal: [number, number]; acceptable: [number, number] };
  };
  climateRequirements: {
    temperature: { optimal: [number, number]; acceptable: [number, number] };
    rainfall: { optimal: [number, number]; acceptable: [number, number] };
    sunlight: { optimal: [number, number]; acceptable: [number, number] };
  };
  expectedYield: {
    high: [number, number];
    medium: [number, number];
    low: [number, number];
  };
  riskFactors: RiskFactor[];
}

export interface RiskFactor {
  id: string;
  name: string;
  condition: string;
  suggestion: string;
  severity: 'high' | 'medium' | 'low';
  category: 'soil' | 'climate' | 'nutrient' | 'disease' | 'pest';
}

export interface CropRecommendation {
  crop: Crop;
  matchScore: number;
  suitabilityLevel: '高度推荐' | '推荐' | '谨慎种植' | '不推荐';
  matchDetails: MatchDetail[];
  applicableRisks: RiskFactor[];
  expectedYield: [number, number];
}

export interface MatchDetail {
  factor: string;
  value: number;
  status: 'optimal' | 'acceptable' | 'poor';
  score: number;
}

export interface FieldFactors {
  ph: number;
  organic: number;
  depth: number;
  nitrogen: number;
  phosphorus: number;
  potassium: number;
  drainage: number;
  temperature?: number;
  rainfall?: number;
  sunlight?: number;
}

// 评价因子配置
export const DEFAULT_FACTORS: FactorWeight[] = [
  { id: 'ph', name: 'pH值', weight: 20, unit: '' },
  { id: 'organic', name: '有机质含量', weight: 25, unit: 'g/kg' },
  { id: 'depth', name: '土层厚度', weight: 20, unit: 'cm' },
  { id: 'nitrogen', name: '全氮', weight: 10, unit: 'g/kg' },
  { id: 'phosphorus', name: '全磷', weight: 10, unit: 'g/kg' },
  { id: 'potassium', name: '全钾', weight: 10, unit: 'g/kg' },
  { id: 'drainage', name: '排水性', weight: 5, unit: '' },
];

// 预设权重方案
export const WEIGHT_PRESETS = {
  grain: [
    { id: 'ph', name: 'pH值', weight: 20, unit: '' },
    { id: 'organic', name: '有机质含量', weight: 30, unit: 'g/kg' },
    { id: 'depth', name: '土层厚度', weight: 25, unit: 'cm' },
    { id: 'nitrogen', name: '全氮', weight: 8, unit: 'g/kg' },
    { id: 'phosphorus', name: '全磷', weight: 8, unit: 'g/kg' },
    { id: 'potassium', name: '全钾', weight: 8, unit: 'g/kg' },
    { id: 'drainage', name: '排水性', weight: 1, unit: '' },
  ],
  economic: [
    { id: 'ph', name: 'pH值', weight: 25, unit: '' },
    { id: 'organic', name: '有机质含量', weight: 35, unit: 'g/kg' },
    { id: 'depth', name: '土层厚度', weight: 15, unit: 'cm' },
    { id: 'nitrogen', name: '全氮', weight: 7, unit: 'g/kg' },
    { id: 'phosphorus', name: '全磷', weight: 7, unit: 'g/kg' },
    { id: 'potassium', name: '全钾', weight: 7, unit: 'g/kg' },
    { id: 'drainage', name: '排水性', weight: 4, unit: '' },
  ]
};

// 地块模拟数据
export const MOCK_FIELDS = [
  { id: 'field-1', name: '东区1号地', code: 'DB001', area: 85.5 },
  { id: 'field-2', name: '西区2号地', code: 'DB002', area: 92.3 },
  { id: 'field-3', name: '南区3号地', code: 'DB003', area: 78.7 },
];

// 工具函数
export const getGradeColor = (grade: string): string => {
  switch (grade) {
    case '高度适宜': return '#22c55e';
    case '一般适宜': return '#eab308';
    case '不适宜': return '#ef4444';
    default: return '#6b7280';
  }
};

export const getScoreColor = (score: number): string => {
  if (score >= 80) return '#22c55e';
  if (score >= 60) return '#eab308';
  return '#ef4444';
};

export const getSuitabilityLevelColor = (level: string): {
  bg: string;
  border: string;
  text: string;
} => {
  switch (level) {
    case '高度推荐':
      return { bg: '#22c55e', border: '#22c55e', text: '#22c55e' };
    case '推荐':
      return { bg: '#3b82f6', border: '#3b82f6', text: '#3b82f6' };
    case '谨慎种植':
      return { bg: '#eab308', border: '#eab308', text: '#eab308' };
    default:
      return { bg: '#6b7280', border: '#6b7280', text: '#6b7280' };
  }
};

export const formatDate = (dateString: string): string => {
  const date = new Date(dateString);
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  });
};