/**
 * Map geometry, bundled rather than fetched.
 *
 * The national map has to render in a hackathon venue with unreliable WiFi, on a machine
 * that may be entirely offline. A tile service would be the first thing to fail and the
 * most visible when it did, so the outline lives here as a coarse polygon and everything
 * else is drawn from the asset coordinates already in the database.
 *
 * The outline is a simplified national boundary intended for schematic operational
 * display — it is not survey-grade and is not used for any calculation.
 */

/** [longitude, latitude] pairs tracing the national boundary. */
export const SAUDI_OUTLINE: Array<[number, number]> = [
  [34.62, 29.38],
  [35.16, 29.35],
  [36.02, 29.2],
  [36.75, 29.87],
  [37.5, 30.9],
  [38.0, 31.4],
  [38.79, 31.9],
  [39.3, 32.16],
  [40.4, 31.94],
  [41.2, 31.5],
  [42.08, 31.1],
  [43.0, 30.5],
  [44.7, 29.2],
  [45.9, 29.1],
  [46.4, 29.09],
  [47.45, 28.99],
  [47.7, 28.53],
  [48.42, 28.54],
  [48.8, 27.7],
  [49.3, 27.2],
  [49.6, 27.02],
  [50.05, 26.6],
  [50.22, 26.15],
  [50.1, 25.6],
  [50.55, 25.0],
  [50.85, 24.72],
  [51.28, 24.62],
  [51.6, 24.3],
  [52.0, 24.12],
  [52.6, 23.0],
  [54.0, 22.85],
  [55.2, 22.7],
  [55.65, 22.0],
  [55.2, 20.6],
  [54.9, 19.95],
  [52.0, 19.0],
  [50.0, 18.8],
  [49.0, 18.6],
  [47.6, 17.45],
  [47.0, 17.0],
  [45.2, 17.42],
  [43.4, 17.4],
  [43.15, 16.7],
  [42.78, 16.38],
  [42.6, 16.42],
  [41.7, 17.9],
  [41.0, 19.0],
  [40.3, 19.9],
  [39.6, 20.5],
  [39.15, 21.3],
  [38.9, 22.0],
  [38.4, 22.8],
  [38.0, 23.5],
  [37.55, 24.1],
  [37.2, 24.6],
  [36.9, 25.2],
  [36.6, 25.7],
  [36.1, 26.4],
  [35.6, 27.4],
  [35.2, 28.0],
  [34.9, 28.1],
  [34.75, 28.6],
  [34.62, 29.38],
]

export const SAUDI_BOUNDS = {
  minLon: 34.4,
  maxLon: 56.0,
  minLat: 15.9,
  maxLat: 32.4,
}

/** Reference latitude used to correct longitude spacing (1° lon shrinks with latitude). */
const CENTRE_LAT_RAD = ((SAUDI_BOUNDS.minLat + SAUDI_BOUNDS.maxLat) / 2) * (Math.PI / 180)
const LON_SCALE = Math.cos(CENTRE_LAT_RAD)

const RAW_WIDTH = (SAUDI_BOUNDS.maxLon - SAUDI_BOUNDS.minLon) * LON_SCALE
const RAW_HEIGHT = SAUDI_BOUNDS.maxLat - SAUDI_BOUNDS.minLat

/** SVG canvas, sized so one unit of latitude and longitude cover comparable ground. */
export const MAP_WIDTH = 1000
export const MAP_HEIGHT = Math.round((RAW_HEIGHT / RAW_WIDTH) * MAP_WIDTH)

/**
 * Equirectangular projection with a cosine correction on longitude.
 *
 * Enough for a national schematic: at this extent the distortion against a proper
 * conformal projection is smaller than the markers drawn on top of it, and it keeps the
 * transform trivially invertible for hit-testing.
 */
export function project(lon: number, lat: number): { x: number; y: number } {
  const x = ((lon - SAUDI_BOUNDS.minLon) * LON_SCALE) / RAW_WIDTH
  const y = (SAUDI_BOUNDS.maxLat - lat) / RAW_HEIGHT
  return { x: x * MAP_WIDTH, y: y * MAP_HEIGHT }
}

export const SAUDI_OUTLINE_PATH = (() => {
  const points = SAUDI_OUTLINE.map(([lon, lat]) => {
    const { x, y } = project(lon, lat)
    return `${x.toFixed(1)},${y.toFixed(1)}`
  })
  return `M ${points.join(' L ')} Z`
})()

/**
 * The transmission backbone drawn between regional centres.
 *
 * A schematic of how the regions interconnect, not a circuit-accurate model — it exists
 * so an operator can see at a glance which neighbours a stressed region can lean on.
 */
export const BACKBONE_LINKS: Array<[string, string]> = [
  ['RYD', 'QSM'],
  ['RYD', 'DMM'],
  ['RYD', 'MAK'],
  ['QSM', 'DMM'],
  ['QSM', 'MED'],
  ['MED', 'TUU'],
  ['TUU', 'NEO'],
  ['MED', 'JED'],
  ['JED', 'MAK'],
  ['MAK', 'ABT'],
  ['ABT', 'JAZ'],
]
