export interface MetricMeta {
  label:     string
  unit:      string
  icon:      string
  group:     string
  precision: number
  color:     string
}

export const METRICS: Record<string, MetricMeta> = {
  // Environment
  temperature:            { label: 'Temperature',        unit: '°C',      icon: '🌡️',  group: 'Environment',  precision: 1, color: '#f97316' },
  humidity:          { label: 'Humidity',           unit: '%',       icon: '💧',  group: 'Environment',  precision: 1, color: '#38bdf8' },
  dew_point:       { label: 'Dew Point',          unit: '°C',      icon: '🌫️',  group: 'Environment',  precision: 1, color: '#818cf8' },
  heat_index:      { label: 'Feels Like',         unit: '°C',      icon: '🤔',  group: 'Environment',  precision: 1, color: '#fb923c' },
  pressure_hpa:      { label: 'Pressure',           unit: ' hPa',    icon: '📊',  group: 'Environment',  precision: 1, color: '#a78bfa' },
  pressure_slp:      { label: 'Sea Level Pressure', unit: ' hPa',    icon: '📊',  group: 'Environment',  precision: 1, color: '#a78bfa' },
  humidity_abs:      { label: 'Absolute Humidity',  unit: ' g/m³',   icon: '💦',  group: 'Environment',  precision: 2, color: '#67e8f9' },
  vapor_pressure_deficit:           { label: 'Vapour Pressure Deficit', unit: ' kPa', icon: '🌿', group: 'Environment', precision: 2, color: '#4ade80' },
  air_quality:         { label: 'Air Quality',        unit: '/100',    icon: '🏭',  group: 'Environment',  precision: 0, color: '#34d399' },
  air_quality_ohms:          { label: 'Gas Resistance',     unit: ' Ω',      icon: '⚗️',  group: 'Environment',  precision: 0, color: '#fbbf24' },
  // Light
  lux:             { label: 'Illuminance',        unit: ' lux',    icon: '☀️',  group: 'Light',        precision: 0, color: '#fde68a' },
  uv_index:             { label: 'UV Index',           unit: '',        icon: '🕶️',  group: 'Light',        precision: 1, color: '#f472b6' },
  solar_wm2:       { label: 'Solar Irradiance',   unit: ' W/m²',   icon: '⚡',  group: 'Light',        precision: 1, color: '#fcd34d' },
  uv_dose:      { label: 'UV Dose Today',      unit: ' mJ/cm²', icon: '📡',  group: 'Light',        precision: 2, color: '#f9a8d4' },
  light_burn_time_min:   { label: 'Burn Time',          unit: ' min',    icon: '🔥',  group: 'Light',        precision: 0, color: '#fb7185' },
  clouds:       { label: 'Cloud Cover',        unit: '%',       icon: '☁️',  group: 'Light',        precision: 0, color: '#94a3b8' },
  day_light_integral:             { label: 'Daily Light Integral', unit: ' mol/m²', icon: '🌱', group: 'Light',       precision: 3, color: '#86efac' },
  visibility:   { label: 'Visibility',         unit: ' km',     icon: '👁️',  group: 'Light',        precision: 1, color: '#7dd3fc' },
  // Wind
  wind_speed:        { label: 'Wind Speed',         unit: ' km/h',   icon: '💨',  group: 'Wind',         precision: 1, color: '#93c5fd' },
  wind_gusts:        { label: 'Wind Gusts',         unit: ' km/h',   icon: '🌬️',  group: 'Wind',         precision: 1, color: '#60a5fa' },
  wind_direction:    { label: 'Wind Direction',     unit: '°',       icon: '🧭',  group: 'Wind',         precision: 0, color: '#818cf8' },
  // Seismic
  magnitude:     { label: 'Seismic Magnitude',  unit: ' g',      icon: '🌍',  group: 'Seismic',      precision: 4, color: '#f87171' },
  seismic_x:            { label: 'Accel X',            unit: ' g',      icon: '📐',  group: 'Seismic',      precision: 3, color: '#fca5a5' },
  seismic_y:            { label: 'Accel Y',            unit: ' g',      icon: '📐',  group: 'Seismic',      precision: 3, color: '#fca5a5' },
  seismic_z:            { label: 'Accel Z',            unit: ' g',      icon: '📐',  group: 'Seismic',      precision: 3, color: '#fca5a5' },
  // Compass
  heading:       { label: 'Compass Heading',    unit: '°',       icon: '🧭',  group: 'Compass',      precision: 1, color: '#c084fc' },
  // Ground
  ground_distance:    { label: 'Ground Distance',    unit: ' cm',     icon: '📏',  group: 'Ground',       precision: 0, color: '#a3e635' },
  accumulation: { label: 'Accumulation Depth', unit: ' cm', icon: '❄️', group: 'Ground',    precision: 1, color: '#bfdbfe' },
  lidar_strength: { label: 'LIDAR Strength',     unit: '',        icon: '📡',  group: 'Ground',       precision: 0, color: '#6ee7b7' },
  // Lightning
  lightning_distance: { label: 'Lightning Distance', unit: ' km',     icon: '⚡',  group: 'Lightning',    precision: 0, color: '#fde047' },
  lightning_rate: { label: 'Strike Rate',   unit: '/hr',     icon: '⚡',  group: 'Lightning',    precision: 1, color: '#facc15' },
  // Moon
  moon_illumination: { label: 'Moon Illumination',  unit: '%',       icon: '🌙',  group: 'Celestial',    precision: 0, color: '#e2e8f0' },
  // Sun
  sun_elevation:         { label: 'Solar Elevation',    unit: '°',       icon: '☀️',  group: 'Celestial',    precision: 1, color: '#fef08a' },
  // Space
  space_kp_index:        { label: 'Kp Index',           unit: '',        icon: '🌌',  group: 'Space',        precision: 1, color: '#818cf8' },
  space_solar_wind_speed_kms: { label: 'Solar Wind',    unit: ' km/s',   icon: '☄️',  group: 'Space',        precision: 0, color: '#c4b5fd' },
  space_imf_bz:          { label: 'IMF Bz',             unit: ' nT',     icon: '🧲',  group: 'Space',        precision: 1, color: '#a5b4fc' },
  // Marine
  wave_height:           { label: 'Wave Height',        unit: ' m',      icon: '🌊',  group: 'Marine',       precision: 2, color: '#38bdf8' },
  swell_wave_height:     { label: 'Swell Height',       unit: ' m',      icon: '🌊',  group: 'Marine',       precision: 2, color: '#7dd3fc' },
  // Forecast
  forecast_precipitation_mm:           { label: 'Precipitation',  unit: ' mm', icon: '🌧️', group: 'Forecast', precision: 1, color: '#38bdf8' },
  forecast_precipitation_probability:  { label: 'Rain Chance',    unit: '%',   icon: '🌂', group: 'Forecast', precision: 0, color: '#60a5fa' },
}

export function formatValue(key: string, value: number | string | null): string {
  if (value === null || value === undefined) return '—'
  const meta = METRICS[key]
  if (!meta) return String(value)
  if (typeof value === 'number') return `${value.toFixed(meta.precision)}${meta.unit}`
  return `${value}${meta.unit}`
}

export const GROUPS = [...new Set(Object.values(METRICS).map(m => m.group))]
