/* global React */
/* ============================================================
   BAJops Mobile — shared data, status system, icons, helpers
   ============================================================ */

/* ---------- MONEY ---------- */
const fmtMoney = (n) => '$' + Number(n).toLocaleString('en-US', { minimumFractionDigits: 0, maximumFractionDigits: 0 });

/* ---------- AIRCRAFT YEAR (deterministic per aircraft name) ---------- */
const acYear = (ac) => { let h = 5381; for (const c of (ac || '')) h = ((h << 5) + h + c.charCodeAt(0)) & 0x7fffffff; return 2010 + (h % 14); };
const fmtMoneyD = (n) => '$' + Number(n).toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });

/* ---------- AIRPORTS ---------- */
const AIRPORTS = {
  KPDK: { city: 'Atlanta',        st: 'GA' },
  KBHM: { city: 'Birmingham',     st: 'AL' },
  KFLL: { city: 'Fort Lauderdale',st: 'FL' },
  KCON: { city: 'Concord',        st: 'NH' },
  KTMB: { city: 'Miami',          st: 'FL' },
  KAVL: { city: 'Asheville',      st: 'NC' },
  KVNY: { city: 'Van Nuys',       st: 'CA' },
  KLAS: { city: 'Las Vegas',      st: 'NV' },
  KTEB: { city: 'Teterboro',      st: 'NJ' },
  KPBI: { city: 'West Palm Beach',st: 'FL' },
};

/* ---------- STATUS SYSTEM (9 states) ---------- */
const STATUS = {
  pending:    { label: 'Pending',          bg: '#F3A617', fg: '#141829', dot: '#F3A617' },
  submitted:  { label: 'Submitted',        bg: '#19253C', fg: '#ffffff', dot: '#19253C' },
  confirmed:  { label: 'Avail. Confirmed', bg: '#2E8B57', fg: '#ffffff', dot: '#2E8B57' },
  selected:   { label: 'Selected',         bg: '#2687C2', fg: '#ffffff', dot: '#2687C2' },
  contracted: { label: 'Contracted',       bg: '#6B4FA0', fg: '#ffffff', dot: '#6B4FA0' },
  requested:  { label: 'Avail. Requested', bg: '#41506A', fg: '#ffffff', dot: '#41506A' },
  unavail:    { label: 'Aircraft Unavail.', bg: '#9AA5B1', fg: '#ffffff', dot: '#9AA5B1' },
  declined:   { label: 'Declined',         bg: '#B43A3A', fg: '#ffffff', dot: '#B43A3A' },
  closed:     { label: 'Closed',           bg: 'transparent', fg: '#6A7585', dot: '#C7CFDA', border: '#C7CFDA' },
};
const STATUS_ORDER = ['pending','submitted','confirmed','selected','contracted','requested','unavail','declined','closed'];

/* ---------- COMPETITOR LADDERS ----------
   Each entry: rank price relative to YOUR price → delta auto-derived in UI.
   `you:true` marks the demo operator quote. */
const ladderTrip1 = {
  trip: [
    { ac: 'King Air 350i',          cat: 'Turboprop',      type: 'Trip', legs: 2, price: 183, refurbInt: 2021, refurbExt: 2022 },
    { ac: 'Cessna Citation CJ3',    cat: 'Light Jet',      type: 'Trip', legs: 2, price: 240, refurbInt: 2021, refurbExt: 2021, you: true },
    { ac: 'Cessna Citation Mustang',cat: 'Very Light Jet', type: 'Trip', legs: 2, price: 241, refurbInt: 2020, refurbExt: 2022 },
    { ac: 'Cessna Citation CJ2',    cat: 'Very Light Jet', type: 'Trip', legs: 2, price: 284, refurbInt: 2007, refurbExt: 2007 },
    { ac: 'Beechjet 400A',          cat: 'Light Jet',      type: 'Trip', legs: 2, price: 288, refurbInt: 2010, refurbExt: 2013 },
    { ac: 'Citation XLS+',          cat: 'Mid Jet',        type: 'Trip', legs: 2, price: 301, refurbInt: 2011, refurbExt: 2009 },
  ],
  leg1: [
    { ac: 'Phenom 300',             cat: 'Light Jet',      type: 'Leg', legs: 1, price: 96,  refurbInt: 2022, refurbExt: 2022 },
    { ac: 'Cessna Citation CJ3',    cat: 'Light Jet',      type: 'Leg', legs: 1, price: 108, refurbInt: 2021, refurbExt: 2021, you: true },
    { ac: 'King Air 350i',          cat: 'Turboprop',      type: 'Leg', legs: 1, price: 121, refurbInt: 2021, refurbExt: 2022 },
    { ac: 'Beechjet 400A',          cat: 'Light Jet',      type: 'Leg', legs: 1, price: 132, refurbInt: 2010, refurbExt: 2013 },
  ],
  leg2: [
    { ac: 'Cessna Citation CJ3',    cat: 'Light Jet',      type: 'Leg', legs: 1, price: 132, refurbInt: 2021, refurbExt: 2021, you: true },
    { ac: 'Citation Mustang',       cat: 'Very Light Jet', type: 'Leg', legs: 1, price: 134, refurbInt: 2020, refurbExt: 2022 },
    { ac: 'Citation XLS+',          cat: 'Mid Jet',        type: 'Leg', legs: 1, price: 169, refurbInt: 2011, refurbExt: 2009 },
  ],
};

/* Pending trips still show a competitor field (others have quoted, you haven't) */
const ladderPending2 = {
  trip: [
    { ac: 'Phenom 100',             cat: 'Very Light Jet', type: 'Trip', legs: 1, price: 8450,  refurbInt: 2019, refurbExt: 2019 },
    { ac: 'Citation CJ1',           cat: 'Light Jet',      type: 'Trip', legs: 1, price: 9100,  refurbInt: 2016, refurbExt: 2018 },
    { ac: 'Learjet 45',             cat: 'Mid Jet',        type: 'Trip', legs: 1, price: 10750, refurbInt: 2014, refurbExt: 2015 },
    { ac: 'Hawker 800XP',           cat: 'Mid Jet',        type: 'Trip', legs: 1, price: 11200, refurbInt: 2012, refurbExt: 2016 },
  ],
  leg1: [
    { ac: 'Phenom 100',             cat: 'Very Light Jet', type: 'Leg',  legs: 1, price: 8300,  refurbInt: 2019, refurbExt: 2019 },
    { ac: 'Citation CJ1',           cat: 'Light Jet',      type: 'Leg',  legs: 1, price: 8900,  refurbInt: 2016, refurbExt: 2018 },
    { ac: 'Learjet 45',             cat: 'Mid Jet',        type: 'Leg',  legs: 1, price: 10500, refurbInt: 2014, refurbExt: 2015 },
    { ac: 'Hawker 800XP',           cat: 'Mid Jet',        type: 'Leg',  legs: 1, price: 10950, refurbInt: 2012, refurbExt: 2016 },
  ],
};
const ladderPending3 = {
  trip: [
    { ac: 'Citation CJ4',           cat: 'Light Jet',      type: 'Trip', legs: 2, price: 18900, refurbInt: 2020, refurbExt: 2021 },
    { ac: 'Phenom 300E',            cat: 'Light Jet',      type: 'Trip', legs: 2, price: 19400, refurbInt: 2022, refurbExt: 2022 },
    { ac: 'Learjet 60XR',           cat: 'Mid Jet',        type: 'Trip', legs: 2, price: 21250, refurbInt: 2013, refurbExt: 2014 },
    { ac: 'Hawker 900XP',           cat: 'Mid Jet',        type: 'Trip', legs: 2, price: 22800, refurbInt: 2011, refurbExt: 2015 },
    { ac: 'Challenger 300',         cat: 'Super Mid',      type: 'Trip', legs: 2, price: 26500, refurbInt: 2016, refurbExt: 2017 },
    { ac: 'Citation Sovereign',     cat: 'Super Mid',      type: 'Trip', legs: 2, price: 27900, refurbInt: 2015, refurbExt: 2016 },
  ],
  leg1: [
    { ac: 'Citation CJ4',           cat: 'Light Jet',      type: 'Leg',  legs: 1, price: 9200,  refurbInt: 2020, refurbExt: 2021 },
    { ac: 'Phenom 300E',            cat: 'Light Jet',      type: 'Leg',  legs: 1, price: 9700,  refurbInt: 2022, refurbExt: 2022 },
    { ac: 'Learjet 60XR',           cat: 'Mid Jet',        type: 'Leg',  legs: 1, price: 10400, refurbInt: 2013, refurbExt: 2014 },
    { ac: 'Hawker 900XP',           cat: 'Mid Jet',        type: 'Leg',  legs: 1, price: 11100, refurbInt: 2011, refurbExt: 2015 },
    { ac: 'Challenger 300',         cat: 'Super Mid',      type: 'Leg',  legs: 1, price: 13000, refurbInt: 2016, refurbExt: 2017 },
  ],
  leg2: [
    { ac: 'Phenom 300E',            cat: 'Light Jet',      type: 'Leg',  legs: 1, price: 8900,  refurbInt: 2022, refurbExt: 2022 },
    { ac: 'Citation CJ4',           cat: 'Light Jet',      type: 'Leg',  legs: 1, price: 9500,  refurbInt: 2020, refurbExt: 2021 },
    { ac: 'Learjet 60XR',           cat: 'Mid Jet',        type: 'Leg',  legs: 1, price: 10800, refurbInt: 2013, refurbExt: 2014 },
    { ac: 'Citation Sovereign',     cat: 'Super Mid',      type: 'Leg',  legs: 1, price: 13600, refurbInt: 2015, refurbExt: 2016 },
  ],
};
const ladderWin4 = {
  trip: [
    { ac: 'Citation CJ3',           cat: 'Light Jet',      type: 'Trip', legs: 1, price: 6200, refurbInt: 2021, refurbExt: 2021, you: true },
    { ac: 'Phenom 300',             cat: 'Light Jet',      type: 'Trip', legs: 1, price: 6480, refurbInt: 2022, refurbExt: 2022 },
    { ac: 'Citation CJ2',           cat: 'Very Light Jet', type: 'Trip', legs: 1, price: 6900, refurbInt: 2007, refurbExt: 2007 },
    { ac: 'Learjet 45',             cat: 'Mid Jet',        type: 'Trip', legs: 1, price: 7350, refurbInt: 2014, refurbExt: 2015 },
  ],
  leg1: [
    { ac: 'Phenom 300',             cat: 'Light Jet',      type: 'Leg',  legs: 1, price: 6350, refurbInt: 2022, refurbExt: 2022 },
    { ac: 'Citation CJ2',           cat: 'Very Light Jet', type: 'Leg',  legs: 1, price: 6750, refurbInt: 2007, refurbExt: 2007 },
    { ac: 'Learjet 45',             cat: 'Mid Jet',        type: 'Leg',  legs: 1, price: 7200, refurbInt: 2014, refurbExt: 2015 },
    { ac: 'Hawker 400XP',           cat: 'Light Jet',      type: 'Leg',  legs: 1, price: 7600, refurbInt: 2018, refurbExt: 2019 },
  ],
};
const ladderWon5 = {
  trip: [
    { ac: 'Hawker 900XP',           cat: 'Mid Jet',        type: 'Trip', legs: 1, price: 19200, refurbInt: 2011, refurbExt: 2015, you: true },
    { ac: 'Citation Sovereign',     cat: 'Super Mid',      type: 'Trip', legs: 1, price: 19900, refurbInt: 2015, refurbExt: 2016 },
    { ac: 'Learjet 60XR',           cat: 'Mid Jet',        type: 'Trip', legs: 1, price: 21400, refurbInt: 2013, refurbExt: 2014 },
    { ac: 'Challenger 300',         cat: 'Super Mid',      type: 'Trip', legs: 1, price: 24200, refurbInt: 2016, refurbExt: 2017 },
  ],
  leg1: [
    { ac: 'Hawker 900XP',           cat: 'Mid Jet',        type: 'Leg',  legs: 1, price: 18900, refurbInt: 2011, refurbExt: 2015, you: true },
    { ac: 'Citation Sovereign',     cat: 'Super Mid',      type: 'Leg',  legs: 1, price: 19600, refurbInt: 2015, refurbExt: 2016 },
    { ac: 'Learjet 60XR',           cat: 'Mid Jet',        type: 'Leg',  legs: 1, price: 21100, refurbInt: 2013, refurbExt: 2014 },
    { ac: 'Challenger 300',         cat: 'Super Mid',      type: 'Leg',  legs: 1, price: 23800, refurbInt: 2016, refurbExt: 2017 },
  ],
};

/* ---------- FLEET ---------- */
const FLEET = [
  { tail: 'N123DJ', ac: 'Cessna Citation CJ3',  cat: 'Light Jet',  refurbInt: 2022, refurbExt: 2021 },
  { tail: 'N550FR', ac: 'Hawker 400XP',          cat: 'Light Jet',  refurbInt: 2019, refurbExt: 2020 },
  { tail: 'N418DJ', ac: 'Cessna Citation CJ3',   cat: 'Light Jet',  refurbInt: 2021, refurbExt: 2022 },
  { tail: 'N902DJ', ac: 'Hawker 900XP',          cat: 'Mid Jet',    refurbInt: 2018, refurbExt: 2019 },
  { tail: 'N245DJ', ac: 'Phenom 300E',           cat: 'Light Jet',  refurbInt: 2023, refurbExt: 2022 },
  { tail: 'N387DJ', ac: 'Citation Sovereign',    cat: 'Super Mid',  refurbInt: 2020, refurbExt: 2021 },
];

/* ---------- TRIPS ---------- */
const TRIPS = [
  {
    id: 't1', ref: 'DY260511868925MA', date: '5/15/2026', from: 'KPDK', to: 'KBHM',
    pax: 1, legCount: 2, distance: 134, bucket: 'live',
    yourRank: 2, ladder: ladderTrip1,
    quotes: [
      { id: 'q1a', tail: 'N123DJ', ac: 'Cessna Citation CJ3', cat: 'Light Jet', type: 'Trip', legs: 2, status: 'submitted', requested: null, amount: 240, you: true },
      { id: 'q1b', tail: 'N550FR', ac: 'Hawker 400XP',        cat: 'Light Jet', type: 'Leg',  legNum: 1, legs: 1, status: 'submitted', requested: null, amount: 12344 },
      { id: 'q1c', tail: 'N550FR', ac: 'Hawker 400XP',        cat: 'Light Jet', type: 'Leg',  legNum: 2, legs: 1, status: 'submitted', requested: null, amount: 12314 },
      { id: 'q1d', tail: 'N550FR', ac: 'Hawker 400XP',        cat: 'Light Jet', type: 'Trip', legs: 2, status: 'submitted', requested: null, amount: 12344 },
      { id: 'q1e', tail: 'N123DJ', ac: 'Cessna Citation CJ3', cat: 'Light Jet', type: 'Trip', legs: 2, status: 'submitted', requested: null, amount: 23424 },
    ],
  },
  {
    id: 't2', ref: 'MS260512725111', date: '5/14/2026', from: 'KFLL', to: 'KCON',
    pax: 1, legCount: 1, distance: 1198, bucket: 'pending',
    competitorsIn: 4, ladder: ladderPending2,
    quotes: [],
  },
  {
    id: 't3', ref: 'DY260423194858', date: '5/15/2026', from: 'KTMB', to: 'KAVL',
    pax: 3, legCount: 2, distance: 612, bucket: 'pending',
    competitorsIn: 6, ladder: ladderPending3,
    quotes: [],
  },
  {
    id: 't4', ref: 'XK260319101308MA', date: '5/16/2026', from: 'KVNY', to: 'KLAS',
    pax: 2, legCount: 1, distance: 236, bucket: 'live',
    yourRank: 1, ladder: ladderWin4,
    quotes: [
      { id: 'q4a', tail: 'N418DJ', ac: 'Cessna Citation CJ3', cat: 'Light Jet', type: 'Trip', legs: 1, status: 'confirmed',  requested: 6500,  amount: 6200,  you: true },
      { id: 'q4b', tail: 'N245DJ', ac: 'Phenom 300E',          cat: 'Light Jet', type: 'Trip', legs: 1, status: 'submitted',  requested: null,  amount: 6480  },
      { id: 'q4c', tail: 'N123DJ', ac: 'Cessna Citation CJ3', cat: 'Light Jet', type: 'Trip', legs: 1, status: 'submitted',  requested: null,  amount: 5900  },
      { id: 'q4d', tail: 'N550FR', ac: 'Hawker 400XP',         cat: 'Light Jet', type: 'Trip', legs: 1, status: 'submitted',  requested: null,  amount: 7100  },
    ],
  },
  {
    id: 't5', ref: 'BJ260208441207', date: '5/12/2026', from: 'KTEB', to: 'KPBI',
    pax: 6, legCount: 1, distance: 1029, bucket: 'won',
    yourRank: 1, ladder: ladderWon5,
    quotes: [
      { id: 'q5a', tail: 'N902DJ', ac: 'Hawker 900XP',         cat: 'Mid Jet',  type: 'Trip', legs: 1, status: 'selected',  requested: 19800, amount: 19200, you: true },
      { id: 'q5b', tail: 'N550FR', ac: 'Hawker 400XP',         cat: 'Light Jet',type: 'Trip', legs: 1, status: 'submitted', requested: null,  amount: 20100 },
      { id: 'q5c', tail: 'N245DJ', ac: 'Phenom 300E',          cat: 'Light Jet',type: 'Trip', legs: 1, status: 'submitted', requested: null,  amount: 18700 },
      { id: 'q5d', tail: 'N387DJ', ac: 'Citation Sovereign',   cat: 'Super Mid',type: 'Trip', legs: 1, status: 'declined',  requested: null,  amount: 24500 },
    ],
  },
];

/* Derived buckets */
const bucketOf = (t) => t.bucket;
const tripRoute = (t) => `${t.from} → ${t.to}`;
const tripCities = (t) => `${AIRPORTS[t.from].city} → ${AIRPORTS[t.to].city}`;

/* ---------- ICONS ---------- */
const fa = (cls) => ({ width, height, style, className, ...rest }) => (
  <i className={cls + (className ? ' ' + className : '')} style={{ fontSize: width || height || 16, lineHeight: 1, display: 'inline-block', ...style }} {...rest} />
);

const Icon = {
  takeoff: fa('fa-duotone fa-light fa-plane-departure'),
  landing: fa('fa-duotone fa-light fa-plane-arrival'),
  clock:   fa('fa-duotone fa-light fa-clock'),
  pax:     fa('fa-duotone fa-light fa-user'),
  caret:   fa('fa-solid fa-caret-down'),
  chevR:   fa('fa-light fa-chevron-right'),
  chevD:   fa('fa-light fa-chevron-down'),
  up:      fa('fa-light fa-arrow-up'),
  down:    fa('fa-light fa-arrow-down'),
  trophy:  fa('fa-duotone fa-light fa-trophy'),
  filter:  fa('fa-light fa-sliders'),
  search:  fa('fa-light fa-magnifying-glass'),
  close:   fa('fa-light fa-xmark'),
  plus:    fa('fa-light fa-plus'),
  minus:   fa('fa-light fa-minus'),
  refresh: fa('fa-light fa-rotate-right'),
  layers:  fa('fa-light fa-layer-group'),
  pin:     fa('fa-duotone fa-light fa-location-dot'),
  bolt:    fa('fa-duotone fa-light fa-bolt'),
  expand:  fa('fa-light fa-up-right-and-down-left-from-center'),
  doc:     fa('fa-light fa-file-lines'),
  pencil:  fa('fa-light fa-pen'),
  lock:    fa('fa-duotone fa-light fa-lock'),
};

/* ---------- TAB BAR icons ---------- */
const TABS = [
  { key: 'Quotes',   icon: 'fa-light fa-file-invoice' },
  { key: 'Fleet',    icon: 'fa-duotone fa-light fa-plane' },
  { key: 'Crew',     icon: 'fa-light fa-users' },
  { key: 'Contacts', icon: 'fa-light fa-address-card' },
  { key: 'Notes',    icon: 'fa-light fa-note-sticky' },
];

Object.assign(window, {
  fmtMoney, fmtMoneyD, AIRPORTS, STATUS, STATUS_ORDER, TRIPS, FLEET,
  bucketOf, tripRoute, tripCities, Icon, TABS, acYear,
});
