/* global React, DATA */
const YtdFlightTable = ({ title, d25, d26, keyName, label }) => {
  const m26 = {};
  (d26 || []).forEach(r => m26[r[keyName]] = r);
  const rows = (d25 || []).slice(0, 12).map(r => {
    const c = m26[r[keyName]] || {};
    const yoyRev = r.revenue ? ((c.revenue || 0) / r.revenue - 1) * 100 : null;
    return {
      key: r[keyName],
      pax25: r.sold || 0,
      pax26: c.sold || 0,
      lf25: r.load_factor || 0,
      lf26: c.load_factor || 0,
      rev25: r.revenue || 0,
      rev26: c.revenue || 0,
      yoyRev,
    };
  });
  return React.createElement(Panel, { title, sub: '2025 vs 2026 YTD', flush: true },
    React.createElement('div', { style: { maxHeight: 360, overflowY: 'auto' } },
      React.createElement('table', { className: 'tbl' },
        React.createElement('thead', null, React.createElement('tr', null,
          React.createElement('th', null, label),
          React.createElement('th', { className: 'num' }, '25 Yolcu'),
          React.createElement('th', { className: 'num' }, '26 Yolcu'),
          React.createElement('th', { className: 'num' }, '25 LF'),
          React.createElement('th', { className: 'num' }, '26 LF'),
          React.createElement('th', { className: 'num' }, 'YoY Gelir'))),
        React.createElement('tbody', null,
          rows.map((r, i) => React.createElement('tr', { key: i },
            React.createElement('td', { style: { fontWeight: 500 } }, r.key),
            React.createElement('td', { className: 'num text-muted' }, fmtK(r.pax25)),
            React.createElement('td', { className: 'num' }, fmtK(r.pax26)),
            React.createElement('td', { className: 'num text-muted' }, r.lf25 + '%'),
            React.createElement('td', { className: 'num' }, r.lf26 + '%'),
            React.createElement('td', { className: 'num' },
              r.yoyRev != null ? React.createElement(Delta, { value: r.yoyRev }) : React.createElement('span', { className: 'text-faint' }, '—'))
          )))
      )));
};

const Flights = () => {
  const k = DATA.flights_kpi_2025;
  const k25y = DATA.flights_kpi_2025_ytd;
  const k26y = DATA.flights_kpi_2026_ytd;
  const yoy = (cur, ref) => (ref ? ((cur / ref) - 1) * 100 : 0);
  const monthly = DATA.flights_by_month.map(m => ({
    label: m.YM.slice(5),
    rev: m.revenue/1e6,
    lf: m.load_factor
  }));
  const flightsFacts = {
    ytd_donem: `1 Oca → ${DATA.ytd_cutoff || '7 Nis'}`,
    ytd: {
      ucus_2026: k26y.flights, ucus_2025: k25y.flights, ucus_yoy_pct: +yoy(k26y.flights, k25y.flights).toFixed(1),
      yolcu_2026: k26y.pax, yolcu_2025: k25y.pax, yolcu_yoy_pct: +yoy(k26y.pax, k25y.pax).toFixed(1),
      lf_2026_pct: +k26y.load_factor.toFixed(1), lf_2025_pct: +k25y.load_factor.toFixed(1),
      gelir_2026_M: +(k26y.revenue / 1e6).toFixed(1), gelir_2025_M: +(k25y.revenue / 1e6).toFixed(1),
      gelir_yoy_pct: +yoy(k26y.revenue, k25y.revenue).toFixed(1),
      koltuk_fiyati_2026: Math.round(k26y.revenue / Math.max(k26y.pax, 1)), koltuk_fiyati_2025: Math.round(k25y.revenue / Math.max(k25y.pax, 1)),
    },
    aylik: monthly.map(m => ({ ay: m.label, gelir_M: +m.rev.toFixed(1), lf_pct: +m.lf.toFixed(0) })),
    en_iyi_rotalar: (DATA.flights_by_route || []).slice(0, 6).map(r => ({ rota: r.Origin + '→' + r.Destination, lf_pct: r.load_factor, gelir_M: +(r.revenue / 1e6).toFixed(1) })),
  };
  return React.createElement('div', { style: { padding: 16, display: 'flex', flexDirection: 'column', gap: 12 } },
    React.createElement('div', { style: { fontSize: 10, letterSpacing: '0.22em', textTransform: 'uppercase',
      color: 'var(--text-muted)', fontWeight: 700, marginBottom: -4 } },
      `YTD KARŞILAŞTIRMA · 1 Oca → ${DATA.ytd_cutoff || '7 Nis'} · 2026 vs 2025`),
    React.createElement('div', { className: 'grid grid-5 gap-3' },
      React.createElement(KPI, { label: 'Uçuş YTD', value: fmtN(k26y.flights),
        sub: `2025 YTD: ${fmtN(k25y.flights)}`, delta: yoy(k26y.flights, k25y.flights) }),
      React.createElement(KPI, { label: 'Yolcu YTD', value: fmtK(k26y.pax),
        sub: `2025 YTD: ${fmtK(k25y.pax)}`, delta: yoy(k26y.pax, k25y.pax) }),
      React.createElement(KPI, { label: 'Doluluk YTD', value: '%' + k26y.load_factor.toFixed(1),
        sub: `2025 YTD: %${k25y.load_factor.toFixed(1)}`, delta: yoy(k26y.load_factor, k25y.load_factor) }),
      React.createElement(KPI, { label: 'Gelir YTD', value: fmtEurM(k26y.revenue),
        sub: `2025 YTD: ${fmtEurM(k25y.revenue)}`, delta: yoy(k26y.revenue, k25y.revenue) }),
      React.createElement(KPI, { label: 'Koltuk Fiyatı YTD',
        value: '€' + (k26y.revenue / Math.max(k26y.pax, 1)).toFixed(0),
        sub: `2025 YTD: €${(k25y.revenue / Math.max(k25y.pax, 1)).toFixed(0)}`,
        delta: yoy(k26y.revenue / Math.max(k26y.pax, 1), k25y.revenue / Math.max(k25y.pax, 1)) })
    ),
    React.createElement(Panel, {
      title: 'Aylık Gelir & Doluluk', sub: '2025'
    },
      React.createElement(ComposedChart, {
        data: monthly, height: 280,
        barKey: 'rev', lineKey: 'lf',
        barFormat: v => '€' + v.toFixed(0) + 'M',
        lineFormat: v => v.toFixed(0) + '%',
        barColor: 'color-mix(in srgb, var(--text-dim) 40%, transparent)',
        lineColor: 'var(--brand)',
        barLabel: 'Uçuş Geliri', lineLabel: 'Doluluk'
      })
    ),
    React.createElement(InsightBox, {
      label: 'Uçuş Performansı · Aylık Gelir & Doluluk',
      hint: 'Uçuş tarafı YTD durumu (uçuş/yolcu/LF/gelir/koltuk fiyatı YoY), aylık gelir ve doluluk eğrisi, en güçlü rotalar. Doluluk ve YoY tempoyu, sezonsallığı ve dikkat gereken rotaları vurgula.',
      facts: flightsFacts,
    }),
    // YTD YoY comparison panels (market, origin, destination)
    React.createElement('div', { className: 'grid grid-3 gap-3' },
      React.createElement(YtdFlightTable, {
        title: 'Kaynak Pazara Göre · YTD',
        d25: DATA.flights_by_market_2025_ytd, d26: DATA.flights_by_market_2026_ytd,
        keyName: 'SourceMarket', label: 'Pazar'
      }),
      React.createElement(YtdFlightTable, {
        title: 'Kalkış (Origin) · YTD',
        d25: DATA.flights_by_origin_2025_ytd, d26: DATA.flights_by_origin_2026_ytd,
        keyName: 'Origin', label: 'Kalkış'
      }),
      React.createElement(YtdFlightTable, {
        title: 'Varış (Destination) · YTD',
        d25: DATA.flights_by_destination_2025_ytd, d26: DATA.flights_by_destination_2026_ytd,
        keyName: 'Destination', label: 'Varış'
      })
    ),
    // Full-year rollups (kept for context)
    React.createElement('div', { className: 'grid grid-2 gap-3' },
      React.createElement(Panel, { title: 'Gelire Göre En İyi Rotalar', sub: '2025 tam yıl', flush: true },
        React.createElement('div', { style: { maxHeight: 360, overflowY: 'auto' } },
          React.createElement('table', { className: 'tbl' },
            React.createElement('thead', null, React.createElement('tr', null,
              React.createElement('th', null, 'Rota'),
              React.createElement('th', { className: 'num' }, 'Uçuş'),
              React.createElement('th', { className: 'num' }, 'LF'),
              React.createElement('th', { className: 'num' }, 'Gelir'))),
            React.createElement('tbody', null,
              DATA.flights_by_route.map((r, i) => React.createElement('tr', { key: i },
                React.createElement('td', { style: { fontWeight: 500 } }, r.Origin + ' → ' + r.Destination),
                React.createElement('td', { className: 'num text-muted' }, fmtN(r.flights)),
                React.createElement('td', { className: 'num' }, r.load_factor + '%'),
                React.createElement('td', { className: 'num' }, fmtEurM(r.revenue))
              )))))
      ),
      React.createElement(Panel, { title: 'En İyi Havayolları', sub: '2025 tam yıl', flush: true },
        React.createElement('table', { className: 'tbl' },
          React.createElement('thead', null, React.createElement('tr', null,
            React.createElement('th', null, 'Havayolu'),
            React.createElement('th', { className: 'num' }, 'Uçuş'),
            React.createElement('th', { className: 'num' }, 'LF'),
            React.createElement('th', { className: 'num' }, 'Gelir'))),
          React.createElement('tbody', null,
            DATA.flights_by_airline.map((a, i) => React.createElement('tr', { key: i },
              React.createElement('td', { style: { fontWeight: 500 } }, a.Airline),
              React.createElement('td', { className: 'num text-muted' }, fmtN(a.flights)),
              React.createElement('td', { className: 'num' }, a.load_factor + '%'),
              React.createElement('td', { className: 'num' }, fmtEurM(a.revenue))
            ))))
      )
    ),
    React.createElement(Panel, {
      title: 'Bireysel Uçuş Performansı', sub: 'Gelire göre ilk 40', flush: true
    },
      React.createElement('div', { style: { maxHeight: 460, overflowY: 'auto' } },
        React.createElement('table', { className: 'tbl' },
          React.createElement('thead', null, React.createElement('tr', null,
            React.createElement('th', null, 'Uçuş'),
            React.createElement('th', null, 'Havayolu'),
            React.createElement('th', null, 'Rota'),
            React.createElement('th', null, 'Pazar'),
            React.createElement('th', { className: 'num' }, 'Sefer'),
            React.createElement('th', { className: 'num' }, 'Yolcu'),
            React.createElement('th', { className: 'num' }, 'Kapasite'),
            React.createElement('th', { className: 'num' }, 'LF'),
            React.createElement('th', { className: 'num' }, 'Gelir'))),
          React.createElement('tbody', null,
            DATA.flights_top.map((f, i) => React.createElement('tr', { key: i },
              React.createElement('td', { className: 'mono', style: { fontWeight: 600 } }, f.FlightNo),
              React.createElement('td', { className: 'text-muted' }, f.Airline),
              React.createElement('td', null, f.Origin + ' → ' + f.Destination),
              React.createElement('td', { className: 'text-muted' }, f.SourceMarket),
              React.createElement('td', { className: 'num text-muted' }, f.flights),
              React.createElement('td', { className: 'num' }, fmtN(f.sold)),
              React.createElement('td', { className: 'num text-muted' }, fmtN(f.capacity)),
              React.createElement('td', { className: 'num',
                style: { color: f.load_factor>=85?'var(--pos)':f.load_factor>=70?'var(--warn)':'var(--text)' } },
                f.load_factor + '%'),
              React.createElement('td', { className: 'num' }, fmtEurM(f.revenue))
            ))))
      )
    )
  );
};
window.Flights = Flights;
