/* ServiceBridge — Timeline Planning · shared data + helpers */

/* The four time-estimate phases every deliverable moves through.
   `key` maps to the estimate fields on each deliverable; `color`
   is a brand-blue sequential ramp (light → navy). */
const PHASES = [
  { key: 'initial',  short: 'Initial',        label: 'Initial design',                 color: '#09B0ED' },
  { key: 'revisedA', short: 'Rev · align',    label: 'Revised — post dev & UX align',  color: '#688DAF' },
  { key: 'revisedB', short: 'Rev · final',    label: 'Revised design (final)',         color: '#275C8D' },
  { key: 'dev',      short: 'Dev build',      label: 'Handed off to dev team',         color: '#142E47' },
];

/* status → antd Tag color + label */
const STATUS = {
  done:    { color: 'green',  label: 'Done' },
  active:  { color: 'blue',   label: 'In progress' },
  next:    { color: 'gold',   label: 'Up next' },
  tbd:     { color: 'purple', label: 'TBD' },
  planned: { color: null,     label: 'Not started' },
};

/* The estimate table is split into two parts:
   - components : reusable UI building blocks, built first
   - features   : product features & flows that assemble those components */
const GROUPS = [
  { key: 'components', label: 'UI Components',            accent: '#09B0ED',
    caption: 'Reusable building blocks — built once, shared across every screen.' },
  { key: 'features',   label: 'Product Features, Functionalities & User Flows', accent: '#275C8D',
    caption: 'End-to-end surfaces that assemble the components into the product.' },
];

/* Estimates are in working days. start/span are in weeks from Jun 1
   (22-week axis → end of Oct) used to lay out the Gantt cascade. */
const DELIVERABLES = [
  /* ---- UI components ---- */
  { id: 6, group: 'components', name: 'Product Wide - Top Nav Bar', owner: 'UX', status: 'planned', month: 'Jul', start: 5, span: 3, initial: 4, revisedA: 2, revisedB: 1, dev: 3,
    note: 'Global top navigation bar shared across the whole product.' },
  { id: 7, group: 'components', name: 'Map Specific - Top And Secondary Piece Of Map', owner: 'UX', status: 'planned', month: 'Aug', start: 6, span: 3, initial: 5, revisedA: 2, revisedB: 1, dev: 4,
    note: 'Map-specific top bar and secondary map control piece.' },
  { id: 1, group: 'components', name: 'Left Nav - Collapse And Expand States', owner: 'UX', status: 'active', hold: 'Waiting for Lisa to give finalized feedback on word choice for navigating to classic Titan product.', month: 'Jun', start: 0,   span: 2.5, initial: 4, revisedA: 2, revisedB: 1, dev: 3,
    note: 'Left-hand navigation rail — collapse and expand states across the app.' },
  { id: 2, group: 'components', name: 'Left Item List (Of Things In Groups)', owner: 'UX', status: 'active', month: 'Jun', start: 1,   span: 3,   initial: 5, revisedA: 2, revisedB: 1, dev: 4,
    note: 'Grouped list of items shown in the left drawer.' },
  { id: 3, group: 'components', name: 'Drawer Details - Display', owner: 'UX', status: 'planned', month: 'Jul', start: 4, span: 3, initial: 5, revisedA: 2, revisedB: 1, dev: 4,
    note: 'Detail drawer layout for an individual item — full record and actions.' },
  { id: 4, group: 'components', name: 'Drawer Details - Editing Mode', owner: 'UX', status: 'planned', month: 'Jul', start: 5, span: 3, initial: 5, revisedA: 2, revisedB: 1, dev: 4,
    note: 'Editable state of the detail drawer — inline edit, validation and save.' },
  { id: 5, group: 'components', name: 'Map Controls', owner: 'UX', status: 'planned', month: 'Jul', start: 4, span: 3, initial: 4, revisedA: 2, revisedB: 1, dev: 3,
    note: 'Zoom, layers, filters and map view controls.' },

  /* ---- product features & flows ---- */
  { id: 8, group: 'features', name: 'Creation Flows', owner: 'UX', status: 'planned', month: 'Sep', start: 13, span: 3.5, initial: 6, revisedA: 3, revisedB: 2, dev: 5,
    note: 'End-to-end flows for creating new items, groups and records.' },
  { id: 9, group: 'features', name: 'Routing & Dispatch Feature', owner: 'UX', status: 'planned', month: 'Sep', start: 14, span: 4, initial: 6, revisedA: 3, revisedB: 2, dev: 6,
    note: 'Route planning, assignment and dispatch to assets in the field.' },
  { id: 10, group: 'features', name: 'History Timeline (Implement The Ability To Pull Specific Reports Per Timeline Data)', owner: 'UX', status: 'planned', month: 'Oct', start: 16, span: 4, initial: 6, revisedA: 3, revisedB: 2, dev: 6,
    note: 'Timeline view of historical activity, with report pulls scoped to a selected time range.' },
];

/* derived helpers — all accept an optional deliverable subset */
const rowTotal   = (d) => PHASES.reduce((s, p) => s + d[p.key], 0);
const byGroup    = (g) => DELIVERABLES.filter((d) => d.group === g);
const phaseSum   = (key, list = DELIVERABLES) => list.reduce((s, d) => s + d[key], 0);
const grandTotal = (list = DELIVERABLES) => list.reduce((s, d) => s + rowTotal(d), 0);
const designSum  = (list = DELIVERABLES) => phaseSum('initial', list) + phaseSum('revisedA', list) + phaseSum('revisedB', list);

const MONTHS = ['Jun', 'Jul', 'Aug', 'Sep', 'Oct'];
const AXIS_WEEKS = 22;

/* ---- live project countdown ---- */
const PROJECT_START = new Date(2026, 4, 15);   /* Mid May 2026 */
const PROJECT_END   = new Date(2026, 10, 15);  /* Mid November 2026 */
/* whole weeks left until the project end, recomputed every render */
function weeksRemaining() {
  const now = new Date();
  const ms = PROJECT_END.getTime() - now.getTime();
  if (ms <= 0) return 0;
  return Math.ceil(ms / (7 * 24 * 60 * 60 * 1000));
}

/* ---- calendar / schedule helpers ---- */
const CAL_BASE  = new Date(2026, 5, 1);  /* Mon, Jun 1 2026 — week-0 anchor */
const CAL_TODAY = new Date(2026, 5, 9);  /* demo “today” */

/* add N business days (skip Sat/Sun) */
function addWorkdays(date, n) {
  const dt = new Date(date.getTime());
  let added = 0;
  while (added < n) {
    dt.setDate(dt.getDate() + 1);
    const w = dt.getDay();
    if (w !== 0 && w !== 6) added++;
  }
  return dt;
}

/* concrete dates for a deliverable, derived from week offset + day estimates */
function schedule(d) {
  const start = addWorkdays(CAL_BASE, Math.round(d.start * 5));
  const designDays = d.initial + d.revisedA + d.revisedB;
  const designDone = addWorkdays(start, designDays);
  const devDone = addWorkdays(designDone, d.dev);
  return { start, designDone, devDone, designDays, devDays: d.dev };
}
/* month band boundaries (weeks from Jun 1) for the Gantt axis */
const MONTH_BANDS = [
  { m: 'Jun', from: 0,    to: 4.3 },
  { m: 'Jul', from: 4.3,  to: 8.7 },
  { m: 'Aug', from: 8.7,  to: 13 },
  { m: 'Sep', from: 13,   to: 17.4 },
  { m: 'Oct', from: 17.4, to: 22 },
];

Object.assign(window, {
  PHASES, STATUS, GROUPS, DELIVERABLES, MONTHS, AXIS_WEEKS, MONTH_BANDS,
  CAL_BASE, CAL_TODAY, addWorkdays, schedule,
  PROJECT_START, PROJECT_END, weeksRemaining,
  rowTotal, byGroup, phaseSum, grandTotal, designSum,
});
