// Pivot Parking — pass data, brand tokens, helpers
// Real codes pulled from the user's uploaded sample sheet (RobertEason_1349025.pdf)

window.PIVOT = {
  // Brand colors lifted from pivotparking.com + logo
  navy: '#0C2340',
  navyDeep: '#081A30',
  navyLight: '#16365C',
  cyan: '#0BA5C7',         // logo cyan
  cyanBright: '#10B8DC',
  cyanDeep: '#0884A0',
  amber: '#F5A623',        // 'Our Services' accent on hero
  ink: '#0F172A',
  paper: '#F4F7FB',
  line: '#E5ECF3',
  muted: '#64748B',
  white: '#FFFFFF',
};

// ── Sample passes from the uploaded PDF ──────────────────────────────────
// Each pass: 10-char hex barcode, location, address, validator, expiration.
// First 30 from sheet are real; we mark a few used/shared so the history demo
// has content out of the box.
const RAW_CODES = [
  'EDE5BBAFE2','6FE39C7112','12194EE9D0','A1B995EA34','37D887E81C',
  'DC9AED72DB','A890EDAACA','18B3D7E5BC','56A1A9F913','C972EFD06A',
  'CB96A79298','C5DF46F8FA','05C448AEEB','4193D3471B','8C1F729F2D',
  '7DC51DFA29','A07622AC86','22A655F7E4','AFE23C4797','B98D47A6B2',
  '61FDDF67C2','17927D44FB','5120E884DD','8D91542180','89274ABB24',
  '78D847AEC1','B6FB4D6A84','05447D5C08','F21D4272F5','931FC721B2',
  'A4D1008225','7A521435B7','D19A2F64B4','B88D9CB6B1','18507EE8A0',
  '60211EAA2E','D4847ADEEC','6793470EC8','A1122BE7B2','7F359A7C93',
];

// Build pass objects with a sprinkle of historical state for the history view
window.SAMPLE_PASSES = RAW_CODES.map((code, i) => {
  const base = {
    id: code,
    code,
    location: 'The Reed / Residential',
    address: '401 S Graham',
    validator: 'Reed Residents',
    expires: '5/29/2026',
    status: 'available',     // available | used | shared
    uploadedAt: '2026-04-28T10:14:00Z',
    usedAt: null,
    sharedWith: null,
  };
  // First 7 codes get marked as historical activity
  if (i === 0) return { ...base, status: 'used',   usedAt: '2026-04-30T18:42:00Z' };
  if (i === 1) return { ...base, status: 'shared', sharedWith: 'Sam Patel',   usedAt: '2026-04-30T08:11:00Z' };
  if (i === 2) return { ...base, status: 'shared', sharedWith: 'Mom',         usedAt: '2026-04-29T19:05:00Z' };
  if (i === 3) return { ...base, status: 'used',   usedAt: '2026-04-29T12:30:00Z' };
  if (i === 4) return { ...base, status: 'shared', sharedWith: 'Jordan B.',   usedAt: '2026-04-28T22:18:00Z' };
  if (i === 5) return { ...base, status: 'used',   usedAt: '2026-04-28T14:00:00Z' };
  if (i === 6) return { ...base, status: 'shared', sharedWith: '+1 (704) 555-0182', usedAt: '2026-04-28T09:47:00Z' };
  return base;
});

// ── Helpers ─────────────────────────────────────────────────────────────
window.formatTime = (iso) => {
  if (!iso) return '';
  const d = new Date(iso);
  const now = new Date('2026-05-01T11:30:00Z');
  const diffMin = Math.floor((now - d) / 60000);
  if (diffMin < 1) return 'just now';
  if (diffMin < 60) return `${diffMin} min ago`;
  if (diffMin < 60 * 24) return `${Math.floor(diffMin / 60)}h ago`;
  const days = Math.floor(diffMin / 60 / 24);
  if (days === 1) return 'yesterday';
  if (days < 7) return `${days} days ago`;
  return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
};

window.formatTimeFull = (iso) => {
  if (!iso) return '';
  const d = new Date(iso);
  return d.toLocaleString('en-US', {
    month: 'short', day: 'numeric',
    hour: 'numeric', minute: '2-digit'
  });
};

// ── Code 128 barcode (subset B) renderer ─────────────────────────────────
// Real Code 128 widths so the barcode actually looks like Pivot's sheet.
// Bars are encoded as 6-element width patterns from the spec.
window.CODE128 = (function () {
  const PATTERNS = [
    '212222','222122','222221','121223','121322','131222','122213','122312','132212','221213',
    '221312','231212','112232','122132','122231','113222','123122','123221','223211','221132',
    '221231','213212','223112','312131','311222','321122','321221','312212','322112','322211',
    '212123','212321','232121','111323','131123','131321','112313','132113','132311','211313',
    '231113','231311','112133','112331','132131','113123','113321','133121','313121','211331',
    '231131','213113','213311','213131','311123','311321','331121','312113','312311','332111',
    '314111','221411','431111','111224','111422','121124','121421','141122','141221','112214',
    '112412','122114','122411','142112','142211','241211','221114','413111','241112','134111',
    '111242','121142','121241','114212','124112','124211','411212','421112','421211','212141',
    '214121','412121','111143','111341','131141','114113','114311','411113','411311','113141',
    '114131','311141','411131','211412','211214','211232','2331112',
  ];
  const START_B = 104;
  const STOP    = 106;
  // Map ASCII to subset-B code value
  const valOf = (ch) => ch.charCodeAt(0) - 32;
  return function encode(text) {
    const codes = [START_B, ...text.split('').map(valOf)];
    let checksum = codes[0];
    codes.slice(1).forEach((c, i) => { checksum += c * (i + 1); });
    codes.push(checksum % 103);
    codes.push(STOP);
    // Convert to bar/space sequence
    const widths = [];
    codes.forEach((c, idx) => {
      const p = PATTERNS[c];
      for (let i = 0; i < p.length; i++) {
        widths.push({ bar: i % 2 === 0, w: parseInt(p[i], 10) });
      }
    });
    return widths;
  };
})();
