/**
 * Environmental Data Types for LocalGreenChain
 * Tracks soil, climate, nutrients, and growing conditions
 */

// Soil Composition
export interface SoilComposition {
  type: 'clay' | 'sand' | 'silt' | 'loam' | 'peat' | 'chalk' | 'custom';
  customType?: string;

  // Soil properties
  pH: number; // 0-14, most plants 6.0-7.5
  texture: 'heavy' | 'medium' | 'light';
  drainage: 'poor' | 'moderate' | 'good' | 'excellent';
  organicMatter: number; // percentage 0-100

  // Composition percentages (should sum to ~100)
  clayPercent?: number;
  sandPercent?: number;
  siltPercent?: number;

  // Amendments/additives
  amendments?: SoilAmendment[];

  notes?: string;
}

export interface SoilAmendment {
  type: 'compost' | 'manure' | 'perlite' | 'vermiculite' | 'peat_moss' | 'coco_coir' | 'biochar' | 'lime' | 'sulfur' | 'other';
  name: string;
  amount?: string; // e.g., "2 cups per gallon", "10%"
  dateAdded: string;
}

// Nutrients & Fertilization
export interface NutrientProfile {
  // NPK values (percentage)
  nitrogen: number; // N
  phosphorus: number; // P
  potassium: number; // K

  // Secondary nutrients
  calcium?: number;
  magnesium?: number;
  sulfur?: number;

  // Micronutrients (ppm or mg/L)
  iron?: number;
  manganese?: number;
  zinc?: number;
  copper?: number;
  boron?: number;
  molybdenum?: number;

  // EC (Electrical Conductivity) - measure of nutrient concentration
  ec?: number; // mS/cm

  // TDS (Total Dissolved Solids)
  tds?: number; // ppm

  lastTested?: string;
}

export interface FertilizerApplication {
  id: string;
  date: string;
  type: 'organic' | 'synthetic' | 'liquid' | 'granular' | 'foliar' | 'slow_release';
  name: string;
  npk?: string; // e.g., "10-10-10", "5-2-3"
  amount: string;
  frequency?: string; // e.g., "weekly", "bi-weekly", "monthly"
  notes?: string;
}

// Sunlight & Climate
export interface LightingConditions {
  type: 'natural' | 'artificial' | 'mixed';

  // Natural light
  naturalLight?: {
    exposure: 'full_sun' | 'partial_sun' | 'partial_shade' | 'full_shade';
    hoursPerDay: number; // 0-24
    direction: 'north' | 'south' | 'east' | 'west' | 'multiple';
    quality: 'direct' | 'filtered' | 'dappled' | 'indirect';
  };

  // Artificial light
  artificialLight?: {
    type: 'LED' | 'fluorescent' | 'HPS' | 'MH' | 'incandescent' | 'mixed';
    spectrum?: string; // e.g., "full spectrum", "6500K", "2700K"
    wattage?: number;
    hoursPerDay: number;
    distance?: number; // cm from plant
  };

  // Light measurements
  ppfd?: number; // Photosynthetic Photon Flux Density (μmol/m²/s)
  dli?: number; // Daily Light Integral (mol/m²/day)
}

export interface ClimateConditions {
  // Temperature (Celsius)
  temperatureDay: number;
  temperatureNight: number;
  temperatureMin?: number;
  temperatureMax?: number;

  // Humidity (percentage)
  humidityAverage: number;
  humidityMin?: number;
  humidityMax?: number;

  // Air circulation
  airflow: 'none' | 'minimal' | 'moderate' | 'strong';
  ventilation: 'poor' | 'adequate' | 'good' | 'excellent';

  // CO2 levels (ppm)
  co2?: number; // ambient ~400, enhanced ~1200-1500

  // Seasonal variation
  season?: 'spring' | 'summer' | 'fall' | 'winter';
  zone?: string; // USDA hardiness zone, e.g., "9b", "10a"
}

// Growing Environment
export interface GrowingEnvironment {
  location: EnvironmentLocation;
  container?: ContainerInfo;
  soil: SoilComposition;
  nutrients: NutrientProfile;
  fertilizers?: FertilizerApplication[];
  lighting: LightingConditions;
  climate: ClimateConditions;
  watering: WateringSchedule;
  surroundings?: SurroundingEnvironment;

  // Tracking
  monitoringFrequency?: 'daily' | 'weekly' | 'bi-weekly' | 'monthly';
  lastUpdated: string;
  notes?: string;
}

export interface EnvironmentLocation {
  type: 'indoor' | 'outdoor' | 'greenhouse' | 'polytunnel' | 'shade_house' | 'window' | 'balcony';
  description?: string;

  // For indoor
  room?: string; // e.g., "bedroom", "basement", "grow tent"

  // For outdoor
  exposureToElements?: 'protected' | 'semi_protected' | 'exposed';
  elevation?: number; // meters above sea level
  slope?: 'flat' | 'gentle' | 'moderate' | 'steep';
  aspect?: 'north' | 'south' | 'east' | 'west'; // slope direction
}

export interface ContainerInfo {
  type: 'pot' | 'raised_bed' | 'ground' | 'hydroponic' | 'aeroponic' | 'aquaponic' | 'fabric_pot' | 'hanging_basket';
  material?: 'plastic' | 'terracotta' | 'ceramic' | 'fabric' | 'wood' | 'metal' | 'concrete';
  size?: string; // e.g., "5 gallon", "30cm diameter", "4x8 feet"
  volume?: number; // liters
  depth?: number; // cm
  drainage: 'yes' | 'no';
  drainageHoles?: number;
}

export interface WateringSchedule {
  method: 'hand_water' | 'drip' | 'soaker_hose' | 'sprinkler' | 'self_watering' | 'hydroponic' | 'rain';
  frequency?: string; // e.g., "daily", "every 2-3 days", "weekly"
  amount?: string; // e.g., "1 liter", "until runoff", "1 inch"
  waterSource: 'tap' | 'well' | 'rain' | 'filtered' | 'distilled' | 'RO';
  waterQuality?: {
    pH?: number;
    tds?: number; // ppm
    chlorine?: 'yes' | 'no' | 'filtered';
  };
}

export interface SurroundingEnvironment {
  // Companion plants
  companionPlants?: string[]; // other plant species nearby

  // Nearby features
  nearbyTrees?: boolean;
  nearbyStructures?: string; // e.g., "building", "wall", "fence"
  groundCover?: string; // e.g., "mulch", "grass", "bare soil", "gravel"

  // Wildlife & pests
  pollinators?: string[]; // e.g., "bees", "butterflies", "hummingbirds"
  beneficialInsects?: string[]; // e.g., "ladybugs", "lacewings"
  pests?: PestInfo[];
  diseases?: DiseaseInfo[];

  // Microclimate factors
  windExposure?: 'sheltered' | 'moderate' | 'exposed' | 'windy';
  frostPocket?: boolean;
  heatTrap?: boolean;

  // Ecosystem type
  ecosystem?: 'urban' | 'suburban' | 'rural' | 'forest' | 'desert' | 'coastal' | 'mountain' | 'tropical';
}

export interface PestInfo {
  name: string;
  severity: 'minor' | 'moderate' | 'severe';
  treatment?: string;
  dateObserved: string;
}

export interface DiseaseInfo {
  name: string;
  symptoms?: string;
  severity: 'minor' | 'moderate' | 'severe';
  treatment?: string;
  dateObserved: string;
}

// Environmental Comparison & Analysis
export interface EnvironmentalComparison {
  plant1: string; // plant ID
  plant2: string; // plant ID
  similarities: string[];
  differences: string[];
  score: number; // 0-100, how similar the environments are
}

export interface GrowthMetrics {
  plantId: string;
  measurements: PlantMeasurement[];
  healthScore: number; // 0-100
  vigor: 'poor' | 'fair' | 'good' | 'excellent';
  issues?: string[];
}

export interface PlantMeasurement {
  date: string;
  height?: number; // cm
  width?: number; // cm
  leafCount?: number;
  flowerCount?: number;
  fruitCount?: number;
  notes?: string;
  photos?: string[];
}

// Helper types for environmental recommendations
export interface EnvironmentalRecommendation {
  category: 'soil' | 'nutrients' | 'light' | 'water' | 'climate' | 'general';
  priority: 'low' | 'medium' | 'high' | 'critical';
  issue: string;
  recommendation: string;
  impact: string;
}
