import type { HumidityProfile, LightProfile, SpaceType } from '../../types/garden';

const gardenValueLabels: Readonly<Record<string, string>> = {
  garden: 'Garden',
  garden_bed: 'Garden Bed',
  bright_indirect: 'Bright Indirect',
  direct_sun: 'Direct Sun',
  filtered_light: 'Filtered Light',
  partial_shade: 'Partial Shade',
  full_shade: 'Full Shade',
  morning_sun: 'Morning Sun',
  afternoon_sun: 'Afternoon Sun',
  evening_sun: 'Evening Sun',
  all_day_sun: 'All-day sun',
  outdoor_container: 'Outdoor Container',
  led: 'LED',
  fluorescent: 'Fluorescent',
  grow_light: 'Grow Light',
  living_room: 'Living Room',
  seed_starting_area: 'Seed Starting Area',
  propagation_station: 'Propagation Station',
  rooted_cutting: 'Rooted cutting',
  young_plant: 'Young plant',
  mature_plant: 'Mature plant',
  established_plant: 'Established plant',
  less_than_1_month: 'Less than 1 month',
  one_to_six_months: '1-6 months',
  six_to_twelve_months: '6-12 months',
  one_to_three_years: '1-3 years',
  three_plus_years: '3+ years',
  northeast: 'Northeast',
  northwest: 'Northwest',
  southeast: 'Southeast',
  southwest: 'Southwest',
  pest_treatment: 'Pest Treatment',
  watering_tool: 'Watering Tool',
  not_applicable: 'N/A',
  one_to_three_ft: '1-3 ft',
  four_to_six_ft: '4-6 ft',
  six_plus_ft: '6+ ft',
  nursery_pot: 'Nursery Pot',
  self_watering: 'Self-Watering',
  raised_bed: 'Raised Bed',
  has_holes: 'Has Holes',
  no_holes: 'No Holes',
  potting_mix: 'Potting Mix',
  cactus_mix: 'Cactus/Succulent Mix',
  orchid_bark: 'Orchid Mix',
  coco_coir: 'Coco Coir',
  perlite: 'Perlite',
  vermiculite: 'Vermiculite',
  sphagnum_moss: 'Sphagnum Moss',
  soil: 'Soil',
  sand: 'Sand',
  compost: 'Compost',
  pumice: 'Pumice',
  charcoal: 'Charcoal',
  seed_starting: 'Seed Starting Mix',
  garden_soil: 'Garden Soil',
  worm_castings: 'Worm Castings',
  bark_chips: 'Bark Chips',
  lava_rock: 'Lava Rock',
  water: 'Water',
  hydroponic: 'Hydroponic System',
  top_watering: 'Top Watering',
  bottom_watering: 'Bottom Watering',
  watering_can: 'Watering Can',
  hose: 'Hose',
  soak: 'Soak',
  mist: 'Mist',
  humidifier: 'Humidifier',
  gravel: 'Gravel',
  pebbles: 'Pebbles',
  moss: 'Moss',
  none: 'None',
};

function titleCaseToken(value: string): string {
  return value.replace(/\b\w/g, letter => letter.toUpperCase());
}

export function formatGardenValue(value: string): string {
  const clean = typeof value === 'string' ? value.trim() : '';
  if (!clean) return '';
  return gardenValueLabels[clean] || titleCaseToken(clean.replace(/_/g, ' '));
}

export function formatSpaceType(value: SpaceType): string {
  return formatGardenValue(value);
}

export function formatLightProfile(value: LightProfile): string {
  const labels: Record<LightProfile, string> = {
    low_light: 'Low light',
    medium_light: 'Medium light',
    bright_indirect: 'Bright indirect',
    direct_sun: 'Direct sun',
  };
  return labels[value] || formatGardenValue(value as string) || 'Not set';
}

export function formatHumidityProfile(value: HumidityProfile): string {
  const labels: Record<HumidityProfile, string> = {
    dry: 'Dry',
    average: 'Average',
    humid: 'Humid',
  };
  return labels[value] || formatGardenValue(value as string) || 'Not set';
}

export function formatPlantType(value: string): string {
  const labels: Readonly<Record<string, string>> = {
    houseplant: 'Houseplant',
    tree: 'Tree',
    garden: 'Garden',
    crop: 'Crop',
    garden_crop: 'Garden / Crop',
    cutting: 'Cutting',
    unsure: 'Unsure',
  };
  const clean = typeof value === 'string' ? value.trim() : '';
  if (!clean) return '';
  return labels[clean] || titleCaseToken(clean.replace(/_/g, ' '));
}

export function formatLocationContext(value: string): string {
  const labels: Readonly<Record<string, string>> = {
    indoor: 'Indoor',
    outdoor: 'Outdoor',
    public_park_trail: 'Park / Trail',
    garden_yard: 'Garden / Yard',
    public_spaces: 'Public Spaces',
    street_tree: 'Street Tree',
    nursery_store: 'Store / Nursery',
    home: 'Home',
    unsure: 'Unsure',
  };
  const clean = typeof value === 'string' ? value.trim() : '';
  if (!clean) return '';
  return labels[clean] || titleCaseToken(clean.replace(/_/g, ' / '));
}

export function formatIdentificationProvider(value?: string | null): string {
  if (!value?.trim()) return '';
  const clean = value.trim();
  const labels: Readonly<Record<string, string>> = {
    'plant.id': 'Plant.id',
    'xiaomi-mimo': 'Pixie Intelligence',
    claude: 'Claude',
    unknown: 'Unknown',
  };
  return labels[clean] || formatGardenValue(clean);
}
