// ===== Shared data and helpers for the memorial site =====

const KENNZAHLEN = [
  { value: 993,       label: "Flüge",                 suffix: "",        note: "vom ersten bis zum letzten" },
  { value: 2946346,   label: "Kilometer geflogen",    suffix: " km",     note: "Erde, Mond, fast wieder zurück" },
  { value: 4090,      label: "Stunden in der Luft",   suffix: " h",      note: "etwa 170 Tage ohne Landung" },
  { value: 83,        label: "Ziele",                 suffix: "",        note: "86 Flughäfen insgesamt" },
  { value: 46,        label: "Länder",                suffix: "",        note: "auf 5 Kontinenten" },
  { value: 73.5,      label: "Mal um die Erde",       suffix: "×",       note: "am Äquator gemessen", decimals: 1 },
];

// Yearly figures. 2005 is intentionally absent — "keine Aufzeichnungen".
const JAHRE = [
  { jahr: 2001, fluege: 115, km: 221211, hinweis: "Einstellung 1. April · B747 & A320" },
  { jahr: 2002, fluege: 122, km: 255052, hinweis: "Wechsel auf A340 & A330" },
  { jahr: 2003, fluege:  32, km:  87024, hinweis: "kleines Flugjahr" },
  { jahr: 2004, fluege: 144, km: 468356, hinweis: "viel Langstrecke" },
  { jahr: 2005, fluege: null, km: null,  hinweis: "keine Aufzeichnungen" },
  { jahr: 2006, fluege: 136, km: 512376, hinweis: "stärkstes km‑Jahr" },
  { jahr: 2007, fluege: 111, km: 555681, hinweis: "Spitzenjahr Langstrecke" },
  { jahr: 2008, fluege:  91, km: 278134, hinweis: "Beförderung zum Purser, 1. September" },
  { jahr: 2009, fluege: 118, km: 229755, hinweis: "ruhigeres Jahr" },
  { jahr: 2010, fluege:  27, km:  96661, hinweis: "kurzes Flugjahr" },
  { jahr: 2011, fluege:  97, km: 242096, hinweis: "letzter Flug am 14. Dezember" },
];

const MONATE = [
  { m: "Jan", n: 81 }, { m: "Feb", n: 61 }, { m: "Mrz", n: 58 },
  { m: "Apr", n: 63 }, { m: "Mai", n: 75 }, { m: "Jun", n: 112 },
  { m: "Jul", n: 119 }, { m: "Aug", n: 68 }, { m: "Sep", n: 71 },
  { m: "Okt", n: 93 }, { m: "Nov", n: 93 }, { m: "Dez", n: 99 },
];

// Top destinations. lat/lon used by the world map too.
const ZIELE = [
  { stadt: "London",         land: "Vereinigtes Königreich", code: "LHR", anzahl: 30, lat: 51.47,  lon:  -0.45 },
  { stadt: "Chicago",        land: "USA",                    code: "ORD", anzahl: 20, lat: 41.98,  lon: -87.91 },
  { stadt: "Charlotte",      land: "USA",                    code: "CLT", anzahl: 14, lat: 35.21,  lon: -80.94 },
  { stadt: "Boston",         land: "USA",                    code: "BOS", anzahl: 13, lat: 42.36,  lon: -71.01 },
  { stadt: "San Francisco",  land: "USA",                    code: "SFO", anzahl: 12, lat: 37.62,  lon:-122.38 },
  { stadt: "New York",       land: "USA",                    code: "JFK", anzahl: 12, lat: 40.64,  lon: -73.78 },
  { stadt: "Barcelona",      land: "Spanien",                code: "BCN", anzahl: 11, lat: 41.30,  lon:   2.08 },
  { stadt: "Stockholm",      land: "Schweden",               code: "ARN", anzahl: 11, lat: 59.65,  lon:  17.92 },
  { stadt: "Rom",            land: "Italien",                code: "FCO", anzahl: 10, lat: 41.80,  lon:  12.25 },
  { stadt: "Hongkong",       land: "China",                  code: "HKG", anzahl: 10, lat: 22.31,  lon: 113.92 },
  { stadt: "Athen",          land: "Griechenland",           code: "ATH", anzahl: 10, lat: 37.94,  lon:  23.94 },
  { stadt: "Istanbul",       land: "Türkei",                 code: "IST", anzahl:  9, lat: 41.28,  lon:  28.74 },
];

// Geographic extremes (called out separately on the map)
const ECKPUNKTE = [
  { stadt: "Tokio",         label: "östlichster Punkt",   code: "NRT", lat: 35.77,  lon: 140.39 },
  { stadt: "Vancouver",     label: "westlichster Punkt",  code: "YVR", lat: 49.19,  lon:-123.18 },
  { stadt: "Helsinki",      label: "nördlichster Punkt",  code: "HEL", lat: 60.32,  lon:  24.96 },
  { stadt: "Johannesburg",  label: "südlichster Punkt",   code: "JNB", lat: -26.13, lon:  28.24 },
];

// Hubs
const HUBS = [
  { code: "MUC", stadt: "München",   lat: 48.35, lon: 11.79 },
  { code: "FRA", stadt: "Frankfurt", lat: 50.04, lon:  8.56 },
];

// Routes for map. Weighting is a relative "thickness" hint (1..5).
// Origin alternates between MUC and FRA based on typical fleet/era.
const ROUTEN = [
  // Long-haul heavy
  { from: "FRA", to: "ORD", n: 20, w: 5 },
  { from: "MUC", to: "ORD", n:  4, w: 1.5 },
  { from: "MUC", to: "CLT", n: 14, w: 4 },
  { from: "MUC", to: "BOS", n: 13, w: 4 },
  { from: "FRA", to: "SFO", n: 12, w: 4 },
  { from: "MUC", to: "JFK", n: 12, w: 4 },
  { from: "FRA", to: "HKG", n: 10, w: 3.5 },
  // Medium-haul Europe & near-East
  { from: "MUC", to: "LHR", n: 30, w: 5 },
  { from: "MUC", to: "BCN", n: 11, w: 3 },
  { from: "MUC", to: "ARN", n: 11, w: 3 },
  { from: "MUC", to: "FCO", n: 10, w: 3 },
  { from: "MUC", to: "ATH", n: 10, w: 3 },
  { from: "MUC", to: "IST", n:  9, w: 3 },
  // Extreme points
  { from: "FRA", to: "NRT", n:  6, w: 2.5, extrem: true },
  { from: "FRA", to: "YVR", n:  5, w: 2.5, extrem: true },
  { from: "MUC", to: "HEL", n:  7, w: 2.5, extrem: true },
  { from: "FRA", to: "JNB", n:  4, w: 2.5, extrem: true },
];

const FLOTTE = [
  { typ: "A340 / A330", fluege: 283, km: 2176100, era: "ab 2002, Langstrecke",
    hinweis: "die Welt wird größer" },
  { typ: "A320",        fluege: 694, km:  629274, era: "Kurz‑ und Mittelstrecke",
    hinweis: "das tägliche Brot" },
  { typ: "B747",        fluege:  16, km:  140972, era: "nur 2001",
    hinweis: "das Königsfluggerät, kurze Romanze" },
];

const ZEITSTRAHL = [
  {
    datum: "1. April 2001",
    jahr: 2001,
    titel: "Einstellung als Flugbegleiter",
    text: "Mit 19 Jahren das erste Mal in Uniform. Erster Eindruck: viel zu früh aufstehen, riecht aber gut nach Kerosin.",
    kennzahl: { fluege: 115, km: 221211 },
    typ: "start"
  },
  {
    datum: "2001",
    jahr: 2001,
    titel: "Erste Flotte: B747 und A320",
    text: "Die kurze, glorreiche B747‑Phase. Sechzehn Flüge auf dem Königsmuster, dann Wechsel auf den Alltag.",
    typ: "fleet"
  },
  {
    datum: "2002",
    jahr: 2002,
    titel: "Wechsel auf A340 und A330",
    text: "Langstrecke. Die Welt wird auf einmal ein Liniennetz: Chicago, Boston, San Francisco, Hongkong.",
    kennzahl: { fluege: 122, km: 255052 },
    typ: "fleet"
  },
  {
    datum: "1. September 2008",
    jahr: 2008,
    titel: "Beförderung zum Purser",
    text: "Mit 26 die Verantwortung für die Kabine. Sieben Jahre nach dem ersten Tag, vier Streifen auf der Schulter.",
    kennzahl: { fluege: 91, km: 278134 },
    typ: "promo"
  },
  {
    datum: "14. Dezember 2011",
    jahr: 2011,
    titel: "Letzter Flug, LH0791",
    text: "Singapur nach München, 10.058 Kilometer. Der weiteste Flug war zugleich der letzte — die längste Heimkehr.",
    kennzahl: { fluege: 97, km: 242096 },
    typ: "ende"
  },
];

const FUN_FACTS = [
  { kopf: "73,5×",        text: "um die Erde — am Äquator entlang gemessen",                          klein: "≈ 2,95 Mio km" },
  { kopf: "7,66×",        text: "die Strecke bis zum Mond — also fast 4 Hin‑ und Rückreisen",         klein: "Erde ↔ Mond, ca. 384.400 km" },
  { kopf: "170 Tage",     text: "am Stück in der Luft — fast ein halbes Jahr ohne Landung",          klein: "4.090 Stunden, addiert"  },
  { kopf: "58.900×",      text: "die kleine Strecke Bensheim ↔ Frankfurt",                            klein: "etwa 50 km pro Runde" },
  { kopf: "≈ 383 t",      text: "CO₂, sehr grob geschätzt — Augenzwinkern inklusive",                  klein: "Schätzwert, nicht offiziell" },
  { kopf: "10.058 km",    text: "der längste Flug: Singapur → München",                                klein: "auch der allerletzte" },
  { kopf: "136 km",       text: "der kürzeste Flug: Frankfurt → Köln",                                  klein: "kürzer als manche S‑Bahn‑Schicht" },
  { kopf: "5 Kontinente", text: "Europa, Nordamerika, Asien, Afrika, mit Anflug auf den Rest",         klein: "Antarktis bleibt für Version 2" },
];

const JETLAG = {
  zeitzonenstunden: 2170,
  ost: 266,
  west: 256,
  ueber5h: 267,
  groessterSprung: { h: 9, von: "München", nach: "San Francisco" },
};

// ----- Helper: great-circle path interpolation -----
function greatCirclePoints(lat1, lon1, lat2, lon2, steps = 96) {
  const toRad = (d) => (d * Math.PI) / 180;
  const toDeg = (r) => (r * 180) / Math.PI;
  const φ1 = toRad(lat1), λ1 = toRad(lon1);
  const φ2 = toRad(lat2), λ2 = toRad(lon2);
  const d = 2 * Math.asin(
    Math.sqrt(
      Math.sin((φ2 - φ1) / 2) ** 2 +
      Math.cos(φ1) * Math.cos(φ2) * Math.sin((λ2 - λ1) / 2) ** 2
    )
  );
  const pts = [];
  if (d === 0) return [[lat1, lon1], [lat2, lon2]];
  for (let i = 0; i <= steps; i++) {
    const f = i / steps;
    const A = Math.sin((1 - f) * d) / Math.sin(d);
    const B = Math.sin(f * d) / Math.sin(d);
    const x = A * Math.cos(φ1) * Math.cos(λ1) + B * Math.cos(φ2) * Math.cos(λ2);
    const y = A * Math.cos(φ1) * Math.sin(λ1) + B * Math.cos(φ2) * Math.sin(λ2);
    const z = A * Math.sin(φ1) + B * Math.sin(φ2);
    const φ = Math.atan2(z, Math.sqrt(x * x + y * y));
    const λ = Math.atan2(y, x);
    pts.push([toDeg(φ), toDeg(λ)]);
  }
  return pts;
}

// Equirectangular projection helper
function project(lat, lon, w, h) {
  const x = ((lon + 180) / 360) * w;
  const y = ((90 - lat) / 180) * h;
  return [x, y];
}

// Build an SVG path that handles antimeridian wrap by splitting on big jumps
function gcSvgPath(lat1, lon1, lat2, lon2, w, h) {
  const pts = greatCirclePoints(lat1, lon1, lat2, lon2);
  let d = "";
  let prevX = null;
  pts.forEach(([la, lo], i) => {
    const [x, y] = project(la, lo, w, h);
    if (i === 0) { d += `M ${x.toFixed(1)} ${y.toFixed(1)}`; prevX = x; return; }
    // detect antimeridian crossing: huge x jump
    if (prevX !== null && Math.abs(x - prevX) > w * 0.5) {
      d += ` M ${x.toFixed(1)} ${y.toFixed(1)}`;
    } else {
      d += ` L ${x.toFixed(1)} ${y.toFixed(1)}`;
    }
    prevX = x;
  });
  return d;
}

const ALL_AIRPORTS = [...HUBS, ...ZIELE, ...ECKPUNKTE];
function airport(code) {
  return ALL_AIRPORTS.find((a) => a.code === code);
}

// Expose to other scripts
Object.assign(window, {
  KENNZAHLEN, JAHRE, MONATE, ZIELE, ECKPUNKTE, HUBS, ROUTEN, FLOTTE,
  ZEITSTRAHL, FUN_FACTS, JETLAG,
  greatCirclePoints, project, gcSvgPath, airport, ALL_AIRPORTS
});
