/**
 * Base de données des fréquences satellites amateur
 * Source: AMSAT Live Satellite Frequencies, Feb 2026 + updated DB Mar 2026
 */

export const SAT_TYPE = {
  FM: 'FM', LINEAR: 'Linear', APRS: 'APRS', CW: 'CW', DATA: 'Data',
}

export const TYPE_STYLE = {
  FM:     { bg: '#e0f0ff', color: '#1a5a9a', border: '#b0d0f0' },
  Linear: { bg: '#e8f8ee', color: '#1a7a40', border: '#a0d8b0' },
  APRS:   { bg: '#fff8e0', color: '#8a6010', border: '#e0c870' },
  CW:     { bg: '#f5f0ff', color: '#5a30a0', border: '#c0a0e0' },
  Data:   { bg: '#f0f0f0', color: '#5a6a7a', border: '#c0cad0' },
}

const DB = [
  // ── FM Voice ───────────────────────────────────────────────────────────────
  {
    keys: ['AO-91', 'FOX-1B', 'RADFXSAT', 'AO-91 (RADFXSAT/FOX-1B)'],
    displayName: 'AO-91 (RadFxSat/Fox-1B)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 145960000, upHz: 435250000, mode: 'FM', notes: 'CTCSS 67 Hz — Do not use during eclipse (battery status)' }]
  },
  {
    keys: ['AO-92', 'FOX-1D'],
    displayName: 'AO-92 (Fox-1D)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 145880000, upHz: 435350000, mode: 'FM', notes: 'CTCSS 67 Hz. Re-entered.' }]
  },
  {
    keys: ['AO-123', 'ASRTU-1'],
    displayName: 'AO-123 (ASRTU-1)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 435400000, upHz: 145850000, mode: 'FM', notes: 'CTCSS 67 Hz — FM/9600bps BPSK QPSK SSDV. Active everytime.' }]
  },
  {
    keys: ['AO-27', 'EYESAT-A'],
    displayName: 'AO-27 (EYESAT-A)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436795000, upHz: 145850000, mode: 'FM', notes: 'FM / 1200bps AFSK. Inactive.' }]
  },
  {
    keys: ['CAS-3H', 'LILACSAT-2'],
    displayName: 'CAS-3H (LilacSat-2)',
    type: SAT_TYPE.FM,
    freqs: [
      { downHz: 437200000, upHz: 144350000, mode: 'FM', notes: 'No CTCSS - FM transponder has no set schedule' },
      { downHz: 437225000, upHz: 145875000, mode: 'APRS', notes: 'APRS / FSK downlink' },
    ]
  },
  {
    keys: ['IO-86', 'LAPAN-A2'],
    displayName: 'IO-86 (LAPAN-A2)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 435880000, upHz: 145880000, mode: 'FM', notes: 'CTCSS 88.5 Hz — Low inclination orbit ±30°' }]
  },
  {
    keys: ['ISS (ZARYA)', 'ISS', 'ZARYA'],
    displayName: 'ISS',
    type: SAT_TYPE.FM,
    freqs: [
      { downHz: 437800000, upHz: 145990000, mode: 'FM',   notes: 'FM Voice répéteur, CTCSS 67.0 Hz. Active sometimes.' },
      { downHz: 144825000, upHz: 0,         mode: 'APRS', notes: 'APRS Europe 144.825 MHz / 1200bps AFSK' },
    ]
  },
  {
    keys: ['PO-101', 'DIWATA-2B'],
    displayName: 'PO-101 (Diwata-2)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 145900000, upHz: 437500000, mode: 'FM', notes: 'CTCSS 141.3 Hz. Active sometimes.' }]
  },
  {
    keys: ['RS95S', 'QMR-KWT-2'],
    displayName: 'RS95S (QMR-KWT-2)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436950000, upHz: 145920000, mode: 'FM', notes: 'CTCSS 67 Hz' }]
  },
  {
    keys: ['SO-50', 'SAUDISAT-1C'],
    displayName: 'SO-50 (SaudiSat-1C)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436795000, upHz: 145850000, mode: 'FM', notes: 'Arm with 74.4 Hz tone (2s carrier), then CTCSS 67 Hz. Active everytime.' }]
  },
  {
    keys: ['SO-125', 'HADES-ICM'],
    displayName: 'SO-125 (HADES-ICM)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436666000, upHz: 145875000, mode: 'FM', notes: 'FM/200–2400bps USB. Beacon 436.666 MHz. Active everytime.' }]
  },
  {
    keys: ['SO-124', 'HADES-R'],
    displayName: 'SO-124 (HADES-R)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436888000, upHz: 145925000, mode: 'FM', notes: 'FM/200–2400bps USB. Beacon 436.888 MHz. Active sometimes.' }]
  },
  {
    keys: ['SO-121', 'HADES-D'],
    displayName: 'SO-121 (HADES-D)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436666000, upHz: 145875000, mode: 'FM', notes: 'FM/USB 50–9600bps. Beacon 436.666 MHz. Re-entered.' }]
  },
  {
    keys: ['LO-87'],
    displayName: 'LO-87',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436900000, upHz: 145990000, mode: 'FM', notes: 'FM Voice' }]
  },
  {
    keys: ['EO-88', 'NAYIF-1'],
    displayName: 'EO-88 (Nayif-1)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 145960000, upHz: 435015000, mode: 'FM', notes: 'CTCSS 67 Hz. Re-entered.' }]
  },
  {
    keys: ['AO-85', 'FOX-1A'],
    displayName: 'AO-85 (Fox-1A)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 145980000, upHz: 435182000, mode: 'FM', notes: 'FM CTCSS 67.0 Hz / 200bps DUV. Breakdown.' }]
  },
  {
    keys: ['AO-95', 'FOX-1CLIFF'],
    displayName: 'AO-95 (Fox-1Cliff)',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 145920000, upHz: 435300000, mode: 'FM', notes: 'FM CTCSS 67.0 Hz / DUV only. Inactive.' }]
  },
  {
    keys: ['UVSQ-SAT', 'UVSQ'],
    displayName: 'UVSQ-Sat',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 437020000, upHz: 145905000, mode: 'FM', notes: 'FM / 1200–9600bps BPSK. Re-entered.' }]
  },
  {
    keys: ['TEVEL-1'],
    displayName: 'TEVEL-1',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Re-entered.' }]
  },
  {
    keys: ['TEVEL-2'],
    displayName: 'TEVEL-2',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Re-entered.' }]
  },
  {
    keys: ['TEVEL-3'],
    displayName: 'TEVEL-3',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Re-entered.' }]
  },
  {
    keys: ['TEVEL-4'],
    displayName: 'TEVEL-4',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Re-entered.' }]
  },
  {
    keys: ['TEVEL-5'],
    displayName: 'TEVEL-5',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Re-entered.' }]
  },
  {
    keys: ['TEVEL-6'],
    displayName: 'TEVEL-6',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Re-entered.' }]
  },
  {
    keys: ['TEVEL-7'],
    displayName: 'TEVEL-7',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Re-entered.' }]
  },
  {
    keys: ['TEVEL-8'],
    displayName: 'TEVEL-8',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Re-entered.' }]
  },
  {
    keys: ['TEVEL2-1'],
    displayName: 'TEVEL2-1',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Active sometimes.' }]
  },
  {
    keys: ['TEVEL2-2'],
    displayName: 'TEVEL2-2',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Active sometimes.' }]
  },
  {
    keys: ['TEVEL2-3'],
    displayName: 'TEVEL2-3',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Active sometimes.' }]
  },
  {
    keys: ['TEVEL2-4'],
    displayName: 'TEVEL2-4',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Active sometimes.' }]
  },
  {
    keys: ['TEVEL2-5'],
    displayName: 'TEVEL2-5',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Active sometimes.' }]
  },
  {
    keys: ['TEVEL2-6'],
    displayName: 'TEVEL2-6',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Active sometimes.' }]
  },
  {
    keys: ['TEVEL2-7'],
    displayName: 'TEVEL2-7',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Active sometimes.' }]
  },
  {
    keys: ['TEVEL2-8'],
    displayName: 'TEVEL2-8',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Active sometimes.' }]
  },
  {
    keys: ['TEVEL2-9'],
    displayName: 'TEVEL2-9',
    type: SAT_TYPE.FM,
    freqs: [{ downHz: 436400000, upHz: 145970000, mode: 'FM', notes: 'FM / 9600bps BPSK. Active sometimes.' }]
  },

  // ── Linear Transponders ────────────────────────────────────────────────────
  {
    keys: ['AO-7', 'AO-07'],
    displayName: 'AO-7',
    type: SAT_TYPE.LINEAR,
    freqs: [
      { downHz: 145950000, upHz: 432150000, mode: 'LSB', notes: 'Mode B (U/v Inverting) — Up 432.125–432.175 / Down 145.925–145.975. Beacon 29.502 MHz. Sunlight only.' },
      { downHz: 29450000,  upHz: 145900000, mode: 'USB', notes: 'Mode A (V/a Non-Inverting) — Up 145.850–145.950 / Down 29.400–29.500. Beacon 29.502 MHz. Sunlight only.' },
    ]
  },
  {
    keys: ['AO-73', 'FUNCUBE-1'],
    displayName: 'AO-73 (FUNcube-1)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 145960000, upHz: 435140000, mode: 'LSB', notes: 'U/v Inverting — Up 435.130–435.150 / Down 145.950–145.970. Beacon 145.935 MHz. Active in eclipse.' }]
  },
  {
    keys: ['FO-29', 'JAS-2'],
    displayName: 'FO-29 (JAS-2)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 435850000, upHz: 145950000, mode: 'LSB', notes: 'V/v Inverting — Up 145.900–146.000 / Down 435.800–435.900. CW Beacon 435.795 MHz. Weekend schedule.' }]
  },
  {
    keys: ['JO-97', 'JY1SAT', 'JY1-SAT'],
    displayName: 'JO-97 (JY1Sat)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 145865000, upHz: 435110000, mode: 'LSB', notes: 'U/v Inverting — Up 435.100–435.120 / Down 145.855–145.875. Beacon 145.840 MHz. Active everytime.' }]
  },
  {
    keys: ['RS-44'],
    displayName: 'RS-44',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 435640000, upHz: 145965000, mode: 'LSB', notes: 'V/v Inverting — Up 145.935–145.995 / Down 435.610–435.670. CW Beacon 435.605 MHz. Active everytime.' }]
  },
  {
    keys: ['TO-108', 'CAS-6', 'TIANQIN-1'],
    displayName: 'TO-108 (CAS-6)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 145925000, upHz: 435280000, mode: 'LSB', notes: 'U/v Inverting — Up 435.270–435.290 / Down 145.915–145.935. Beacon 145.910 MHz. Inactive.' }]
  },
  {
    keys: ["QO-100", "ES'HAIL-2", "ESHAIL-2", "P4-A"],
    displayName: "QO-100 (Es'hail-2)",
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 10489750000, upHz: 2400250000, mode: 'USB', notes: "Geostationary 25.9°E — Up 2400.0–2400.5 MHz / Down 10489.5–10490.0 MHz" }]
  },
  {
    keys: ['CAS-4A'],
    displayName: 'CAS-4A',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 145855000, upHz: 435180000, mode: 'USB', notes: 'Linear. Re-entered.' }]
  },
  {
    keys: ['CAS-4B'],
    displayName: 'CAS-4B',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 145910000, upHz: 435280000, mode: 'USB', notes: 'Linear. Re-entered.' }]
  },
  {
    keys: ['XW-2A', 'CAS-3A'],
    displayName: 'XW-2A (CAS-3A)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 145675000, upHz: 435030000, mode: 'USB', notes: 'Inverting. Re-entered.' }]
  },
  {
    keys: ['XW-2B', 'CAS-3B'],
    displayName: 'XW-2B (CAS-3B)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 145730000, upHz: 435090000, mode: 'USB', notes: 'Inverting. Re-entered.' }]
  },
  {
    keys: ['XW-2C', 'CAS-3C'],
    displayName: 'XW-2C (CAS-3C)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 145785000, upHz: 435150000, mode: 'USB', notes: 'Inverting. Re-entered.' }]
  },
  {
    keys: ['XW-2D', 'CAS-3D'],
    displayName: 'XW-2D (CAS-3D)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 145840000, upHz: 435210000, mode: 'USB', notes: 'Inverting. Re-entered.' }]
  },
  {
    keys: ['XW-2F', 'CAS-3F'],
    displayName: 'XW-2F (CAS-3F)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 145945000, upHz: 435330000, mode: 'USB', notes: 'Inverting. Re-entered.' }]
  },
  {
    keys: ['FO-99', 'NEXUS'],
    displayName: 'FO-99 (NEXUS)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 435895000, upHz: 145915000, mode: 'LSB', notes: 'V/v Inverting — Up 145.900–145.930 / Down 435.880–435.910. Beacon 437.075 MHz. Re-entered.' }]
  },
  {
    keys: ['HO-113', 'CAS-9', 'XW-3'],
    displayName: 'HO-113 (CAS-9/XW-3)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 435180000, upHz: 145870000, mode: 'LSB', notes: 'SSB 4800bps GMSK — Up 145.855–145.885 / Down 435.165–435.195. Beacon 435.575 MHz. Inactive.' }]
  },
  {
    keys: ['CAS-5A', 'FO-118'],
    displayName: 'CAS-5A (FO-118)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 435540000, upHz: 145820000, mode: 'USB', notes: 'SSB 30kHz bandwidth. Beacon 435.570 MHz. Re-entered.' }]
  },
  {
    keys: ['CAS-10', 'HO-119', 'XW-4'],
    displayName: 'CAS-10 (HO-119/XW-4)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 435180000, upHz: 145870000, mode: 'USB', notes: 'SSB 30kHz bandwidth. Beacon 435.575 MHz. Re-entered.' }]
  },
  {
    keys: ['MESAT-1', 'MO-122'],
    displayName: 'MESAT-1 (MO-122)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 435825000, upHz: 145925000, mode: 'LSB', notes: 'Up 145.910–145.940 / Down 435.810–435.840. Beacon 435.800 MHz. Inactive.' }]
  },
  {
    keys: ['AO-109', 'FOX-1E', 'RADFXSAT-2', 'RADFXSAT2'],
    displayName: 'AO-109 (Fox-1E/RadFxSat-2)',
    type: SAT_TYPE.LINEAR,
    freqs: [{ downHz: 435775000, upHz: 145875000, mode: 'LSB', notes: 'Up 145.860–145.890 / Down 435.760–435.790. Beacon 435.750 MHz. Re-entered.' }]
  },

  // ── APRS / Data ────────────────────────────────────────────────────────────
  {
    keys: ['DUCHIFAT-1'],
    displayName: 'DUCHIFAT-1',
    type: SAT_TYPE.DATA,
    freqs: [{ downHz: 145980000, upHz: 0, mode: 'CW/Data', notes: 'Beacon only' }]
  },
]

// Build lookup map from all keys
const LOOKUP = new Map()
for (const entry of DB) {
  for (const key of entry.keys) {
    LOOKUP.set(key.toUpperCase(), entry)
  }
}

export function getSatInfo(satName) {
  if (!satName) return null
  const upper = satName.toUpperCase().trim()

  // 1. Exact key match
  if (LOOKUP.has(upper)) return LOOKUP.get(upper)

  // 2. Longest key that is fully contained in the sat name (or vice versa)
  let best = null
  let bestLen = 0
  for (const [key, entry] of LOOKUP) {
    const inUpper = upper === key
      || upper.startsWith(key + ' ') || upper.startsWith(key + '(') || upper.startsWith(key + '-')
      || upper.endsWith(' ' + key) || upper.endsWith('(' + key) || upper.endsWith('-' + key)
      || upper.includes(' ' + key + ' ') || upper.includes('(' + key + ')')
    const inKey = key === upper
      || key.startsWith(upper + ' ') || key.startsWith(upper + '(') || key.startsWith(upper + '-')
      || key.endsWith(' ' + upper) || key.endsWith('(' + upper) || key.endsWith('-' + upper)
      || key.includes(' ' + upper + ' ')

    if ((inUpper || inKey) && key.length > bestLen) {
      best = entry
      bestLen = key.length
    }
  }

  return best
}

export function getFrequencies(satName) {
  return getSatInfo(satName)?.freqs || null
}

export function getPrimaryFrequency(satName) {
  return getFrequencies(satName)?.[0] || null
}

export function getSatType(satName) {
  return getSatInfo(satName)?.type || null
}

export function getDisplayName(satName) {
  return getSatInfo(satName)?.displayName || satName
}