// 通用类型定义

// API响应类型
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
  success: boolean
}

// 分页类型
export interface PaginationParams {
  page: number
  pageSize: number
  total?: number
}

export interface PaginatedResponse<T> extends ApiResponse<T[]> {
  data: T[]
  pagination: {
    page: number
    pageSize: number
    total: number
    totalPages: number
  }
}

// 排序类型
export interface SortParams {
  field: string
  order: 'asc' | 'desc'
}

// 搜索类型
export interface SearchParams {
  keyword?: string
  filters?: Record<string, any>
}

// 表格列定义
export interface ColumnDef<T = any> {
  key: string
  title: string
  dataIndex: keyof T
  width?: number
  fixed?: 'left' | 'right'
  sortable?: boolean
  filterable?: boolean
  render?: (value: any, record: T, index: number) => React.ReactNode
}

// 表单字段类型
export interface FormField {
  name: string
  label: string
  type: 'text' | 'number' | 'select' | 'date' | 'textarea' | 'checkbox' | 'radio'
  required?: boolean
  placeholder?: string
  options?: Array<{ label: string; value: any }>
  validation?: {
    min?: number
    max?: number
    pattern?: RegExp
    message?: string
  }
}

// 模态框类型
export interface ModalProps {
  visible: boolean
  title: string
  onClose: () => void
  onConfirm?: () => void
  width?: number
  maskClosable?: boolean
}

// 通知类型
export interface NotificationProps {
  type: 'success' | 'error' | 'warning' | 'info'
  title: string
  message?: string
  duration?: number
}

// 文件上传类型
export interface UploadFile {
  id: string
  name: string
  size: number
  type: string
  url?: string
  status: 'uploading' | 'done' | 'error'
  progress?: number
}

// 用户类型
export interface User {
  id: string
  username: string
  email: string
  phone: string
  avatar?: string
  role: UserRole
  status: UserStatus
  createdAt: string
  updatedAt: string
}

export enum UserRole {
  ADMIN = 'admin',
  MANAGER = 'manager',
  OPERATOR = 'operator',
  VIEWER = 'viewer'
}

export enum UserStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  SUSPENDED = 'suspended'
}

// 农机类型
export interface Machinery {
  id: string
  name: string
  type: MachineryType
  model: string
  brand: string
  serialNumber: string
  status: MachineryStatus
  location?: Location
  purchaseDate: string
  warrantyExpiry?: string
  lastMaintenance?: string
  nextMaintenance?: string
  operatingHours: number
  fuelType: FuelType
  specifications: MachinerySpecs
  documents: Document[]
  createdAt: string
  updatedAt: string
}

export enum MachineryType {
  TRACTOR = 'tractor',
  HARVESTER = 'harvester',
  PLANTER = 'planter',
  IRRIGATION = 'irrigation',
  TILLER = 'tiller',
  SPRAYER = 'sprayer',
  OTHER = 'other'
}

export enum MachineryStatus {
  RUNNING = 'running',
  IDLE = 'idle',
  MAINTENANCE = 'maintenance',
  ERROR = 'error',
  OFFLINE = 'offline'
}

export enum FuelType {
  DIESEL = 'diesel',
  GASOLINE = 'gasoline',
  ELECTRIC = 'electric',
  HYBRID = 'hybrid'
}

export interface Location {
  latitude: number
  longitude: number
  address: string
  fieldId?: string
}

export interface MachinerySpecs {
  power?: number // 功率 (kW)
  weight?: number // 重量 (kg)
  width?: number // 宽度 (cm)
  height?: number // 高度 (cm)
  length?: number // 长度 (cm)
  capacity?: number // 载重/容量
  speed?: number // 最大速度 (km/h)
}

export interface Document {
  id: string
  name: string
  type: string
  url: string
  size: number
  uploadedAt: string
}

// 地块类型
export interface Field {
  id: string
  name: string
  code: string
  area: number // 面积 (亩)
  location: Location
  soilType: SoilType
  cropType?: CropType
  irrigationType: IrrigationType
  status: FieldStatus
  owner: string
  createdAt: string
  updatedAt: string
}

export enum SoilType {
  SANDY = 'sandy',
  CLAY = 'clay',
  LOAMY = 'loamy',
  SILT = 'silt',
  PEATY = 'peaty'
}

export enum CropType {
  RICE = 'rice',
  WHEAT = 'wheat',
  CORN = 'corn',
  SOYBEAN = 'soybean',
  VEGETABLE = 'vegetable',
  FRUIT = 'fruit'
}

export enum IrrigationType {
  DRIP = 'drip',
  SPRINKLER = 'sprinkler',
  FLOOD = 'flood',
  CENTER_PIVOT = 'center_pivot'
}

export enum FieldStatus {
  ACTIVE = 'active',
  FALLOW = 'fallow',
  MAINTENANCE = 'maintenance'
}

// 农事记录类型
export interface OperationRecord {
  id: string
  fieldId: string
  machineryId?: string
  operatorId: string
  type: OperationType
  status: OperationStatus
  startTime: string
  endTime?: string
  area?: number
  cropType?: CropType
  materials: OperationMaterial[]
  notes?: string
  cost?: number
  createdAt: string
  updatedAt: string
}

export enum OperationType {
  PLOWING = 'plowing',
  PLANTING = 'planting',
  IRRIGATING = 'irrigating',
  FERTILIZING = 'fertilizing',
  HARVESTING = 'harvesting',
  PESTICIDE = 'pesticide',
  WEEDING = 'weeding'
}

export enum OperationStatus {
  PLANNED = 'planned',
  IN_PROGRESS = 'in_progress',
  COMPLETED = 'completed',
  CANCELLED = 'cancelled'
}

export interface OperationMaterial {
  name: string
  type: 'seed' | 'fertilizer' | 'pesticide' | 'other'
  quantity: number
  unit: string
  cost?: number
}

// 天气类型
export interface Weather {
  id: string
  location: Location
  date: string
  temperature: {
    min: number
    max: number
    current: number
  }
  humidity: number
  rainfall: number
  windSpeed: number
  windDirection: number
  pressure: number
  visibility: number
  uvIndex: number
  condition: WeatherCondition
  forecast?: WeatherForecast[]
}

export enum WeatherCondition {
  SUNNY = 'sunny',
  CLOUDY = 'cloudy',
  RAINY = 'rainy',
  STORMY = 'stormy',
  SNOWY = 'snowy',
  FOGGY = 'foggy'
}

export interface WeatherForecast {
  date: string
  condition: WeatherCondition
  temperature: {
    min: number
    max: number
  }
  rainfall: number
  windSpeed: number
}

// 主题类型
export type Theme = 'light' | 'dark' | 'system'

// 语言类型
export type Locale = 'zh-CN' | 'en-US'

// 应用配置类型
export interface AppConfig {
  theme: Theme
  locale: Locale
  apiBaseUrl: string
  enableNotifications: boolean
  enableAnalytics: boolean
  version: string
}

// 路由类型
export interface Route {
  path: string
  component: React.ComponentType<any>
  exact?: boolean
  title?: string
  icon?: React.ReactNode
  children?: Route[]
  permission?: string[]
}

// 菜单类型
export interface MenuItem {
  key: string
  label: string
  icon?: React.ReactNode
  path?: string
  children?: MenuItem[]
  permission?: string[]
}

// 图表数据类型
export interface ChartData {
  name: string
  value: number
  color?: string
}

export interface TimeSeriesData {
  timestamp: string
  value: number
  category?: string
}

// 统计数据类型
export interface DashboardStats {
  totalMachinery: number
  activeMachinery: number
  totalFields: number
  activeFields: number
  totalOperations: number
  pendingOperations: number
  todayRevenue: number
  monthlyRevenue: number
}

// 错误类型
export interface AppError {
  code: string
  message: string
  details?: any
  timestamp: string
}

// 日志类型
export interface LogEntry {
  id: string
  level: LogLevel
  message: string
  module: string
  userId?: string
  timestamp: string
  metadata?: Record<string, any>
}

export enum LogLevel {
  DEBUG = 'debug',
  INFO = 'info',
  WARNING = 'warning',
  ERROR = 'error',
  CRITICAL = 'critical'
}