/**
 * Regional reference data.
 *
 * Coordinates are real; demand, capacity and climate coefficients are plausible
 * engineering figures chosen for the simulation — they are NOT operator data.
 * Everything derived from them is labelled as simulated in the UI (§20).
 */

export interface RegionSeed {
  code: string
  name: string
  nameAr: string
  lat: number
  lon: number
  populationK: number
  peakDemandMw: number
  capacityMw: number
  climate: string
  climateAr: string
  /** Yearly mean air temperature (°C). */
  annualMeanC: number
  /** Half the peak-to-trough seasonal swing (°C). */
  seasonalAmpC: number
  /** Half the peak-to-trough daily swing (°C). */
  dailyAmpC: number
  baseHumidityPct: number
  baseWindMs: number
  baseCloudPct: number
  baseDustIndex: number
  /** Fraction of demand that is deferrable (industrial, EV, thermal storage). */
  flexibleShare: number
}

export const REGIONS: RegionSeed[] = [
  {
    code: 'RYD',
    name: 'Riyadh',
    nameAr: 'الرياض',
    lat: 24.7136,
    lon: 46.6753,
    populationK: 7676,
    peakDemandMw: 18500,
    capacityMw: 21800,
    climate: 'Hot desert, extreme summer peaks',
    climateAr: 'صحراوي حار مع ذروة صيفية شديدة',
    annualMeanC: 26,
    seasonalAmpC: 12,
    dailyAmpC: 8,
    baseHumidityPct: 14,
    baseWindMs: 3.6,
    baseCloudPct: 8,
    baseDustIndex: 0.34,
    flexibleShare: 0.16,
  },
  {
    code: 'JED',
    name: 'Jeddah',
    nameAr: 'جدة',
    lat: 21.4858,
    lon: 39.1925,
    populationK: 4697,
    peakDemandMw: 9800,
    capacityMw: 11400,
    climate: 'Coastal, humid, narrow daily swing',
    climateAr: 'ساحلي رطب بمدى حراري يومي ضيق',
    annualMeanC: 28.5,
    seasonalAmpC: 5.5,
    dailyAmpC: 4.5,
    baseHumidityPct: 62,
    baseWindMs: 4.2,
    baseCloudPct: 18,
    baseDustIndex: 0.19,
    flexibleShare: 0.12,
  },
  {
    code: 'DMM',
    name: 'Dammam',
    nameAr: 'الدمام',
    lat: 26.4207,
    lon: 50.0888,
    populationK: 1532,
    peakDemandMw: 12400,
    capacityMw: 14600,
    climate: 'Gulf coast, humid with heavy industrial load',
    climateAr: 'ساحل الخليج، رطب مع أحمال صناعية كثيفة',
    annualMeanC: 27,
    seasonalAmpC: 10,
    dailyAmpC: 5.5,
    baseHumidityPct: 56,
    baseWindMs: 4.6,
    baseCloudPct: 14,
    baseDustIndex: 0.3,
    flexibleShare: 0.24,
  },
  {
    code: 'MAK',
    name: 'Makkah',
    nameAr: 'مكة المكرمة',
    lat: 21.3891,
    lon: 39.8579,
    populationK: 2043,
    peakDemandMw: 6200,
    capacityMw: 7400,
    climate: 'Very hot inland valley, pilgrimage demand spikes',
    climateAr: 'وادٍ داخلي شديد الحرارة مع ذروات موسم الحج',
    annualMeanC: 31,
    seasonalAmpC: 6.5,
    dailyAmpC: 6,
    baseHumidityPct: 32,
    baseWindMs: 3.1,
    baseCloudPct: 12,
    baseDustIndex: 0.26,
    flexibleShare: 0.1,
  },
  {
    code: 'MED',
    name: 'Madinah',
    nameAr: 'المدينة المنورة',
    lat: 24.5247,
    lon: 39.5692,
    populationK: 1512,
    peakDemandMw: 4100,
    capacityMw: 4950,
    climate: 'Hot arid plateau',
    climateAr: 'هضبة قاحلة حارة',
    annualMeanC: 28,
    seasonalAmpC: 10,
    dailyAmpC: 8,
    baseHumidityPct: 18,
    baseWindMs: 3.4,
    baseCloudPct: 9,
    baseDustIndex: 0.29,
    flexibleShare: 0.11,
  },
  {
    code: 'TUU',
    name: 'Tabuk',
    nameAr: 'تبوك',
    lat: 28.3838,
    lon: 36.555,
    populationK: 667,
    peakDemandMw: 2300,
    capacityMw: 3100,
    climate: 'High desert, cool nights, strong solar resource',
    climateAr: 'صحراء مرتفعة بليالٍ باردة وإشعاع شمسي قوي',
    annualMeanC: 22,
    seasonalAmpC: 10,
    dailyAmpC: 10,
    baseHumidityPct: 22,
    baseWindMs: 4.8,
    baseCloudPct: 10,
    baseDustIndex: 0.2,
    flexibleShare: 0.14,
  },
  {
    code: 'NEO',
    name: 'NEOM',
    nameAr: 'نيوم',
    lat: 27.9654,
    lon: 35.4383,
    populationK: 44,
    peakDemandMw: 1900,
    capacityMw: 3600,
    climate: 'Red Sea coast, exceptional wind corridor',
    climateAr: 'ساحل البحر الأحمر، ممر رياح استثنائي',
    annualMeanC: 24,
    seasonalAmpC: 8,
    dailyAmpC: 7,
    baseHumidityPct: 46,
    baseWindMs: 7.1,
    baseCloudPct: 11,
    baseDustIndex: 0.14,
    flexibleShare: 0.31,
  },
  {
    code: 'JAZ',
    name: 'Jazan',
    nameAr: 'جازان',
    lat: 16.8894,
    lon: 42.5706,
    populationK: 197,
    peakDemandMw: 2600,
    capacityMw: 3050,
    climate: 'Tropical coast, very humid',
    climateAr: 'ساحل مداري شديد الرطوبة',
    annualMeanC: 30,
    seasonalAmpC: 4.5,
    dailyAmpC: 4,
    baseHumidityPct: 71,
    baseWindMs: 3.5,
    baseCloudPct: 26,
    baseDustIndex: 0.16,
    flexibleShare: 0.19,
  },
  {
    code: 'ABT',
    name: 'Abha',
    nameAr: 'أبها',
    lat: 18.2465,
    lon: 42.5117,
    populationK: 366,
    peakDemandMw: 2200,
    capacityMw: 2700,
    climate: 'Mountain highlands, mild with frequent cloud',
    climateAr: 'مرتفعات جبلية معتدلة كثيرة الغيوم',
    annualMeanC: 19,
    seasonalAmpC: 6,
    dailyAmpC: 8,
    baseHumidityPct: 48,
    baseWindMs: 4.1,
    baseCloudPct: 38,
    baseDustIndex: 0.09,
    flexibleShare: 0.09,
  },
  {
    code: 'QSM',
    name: 'Qassim',
    nameAr: 'القصيم',
    lat: 26.326,
    lon: 43.975,
    populationK: 807,
    peakDemandMw: 3400,
    capacityMw: 4150,
    climate: 'Arid agricultural plain, dust-prone',
    climateAr: 'سهل زراعي قاحل معرّض للغبار',
    annualMeanC: 25.5,
    seasonalAmpC: 12,
    dailyAmpC: 9,
    baseHumidityPct: 17,
    baseWindMs: 3.9,
    baseCloudPct: 8,
    baseDustIndex: 0.36,
    flexibleShare: 0.21,
  },
]

export const REGION_BY_CODE = new Map(REGIONS.map((r) => [r.code, r]))

export function getRegion(code: string): RegionSeed {
  const region = REGION_BY_CODE.get(code)
  if (!region) throw new Error(`Unknown region code: ${code}`)
  return region
}

/** National totals, derived rather than hard-coded so they stay consistent. */
export const NATIONAL_PEAK_MW = REGIONS.reduce((sum, r) => sum + r.peakDemandMw, 0)
export const NATIONAL_CAPACITY_MW = REGIONS.reduce((sum, r) => sum + r.capacityMw, 0)
