// ScheduleGrid.jsx — TeamBridge app inside a lo-fi browser window.
// Browser chrome + icon rail + wireframe left nav + weekly schedule (the action).
// All inner content uses LOCAL coords (0,0 = window content top-left).

// Browser window placement on the 1920x1080 canvas (full width; agent cards float over it)
const WIN = { x: 130, y: 100, w: 1660, h: 880 };

// Local layout (inside the window)
const L = { CHROME: 46, RAIL: 56, NAV: 236, HEADER: 58, FILTER: 50, DAYHDR: 54, LEFT: 300, WINW: 1660, WINH: 880 };
L.MAIN_X = L.RAIL + L.NAV;                 // 292
L.MAIN_W = L.WINW - L.MAIN_X;              // 1516
L.APP_TOP = L.CHROME;                      // 46
L.ROWS_TOP = L.APP_TOP + L.HEADER + L.FILTER + L.DAYHDR; // 208
L.ROWS = 6;
L.ROW_H = (L.WINH - L.ROWS_TOP) / L.ROWS;  // ~130.7
L.DAY_X0 = L.MAIN_X + L.LEFT;              // 592
L.DAY_W = (L.MAIN_W - L.LEFT) / 7;         // ~173.7
const cellX = (d) => L.DAY_X0 + d * L.DAY_W;
const cellY = (r) => L.ROWS_TOP + r * L.ROW_H;
const cellCenterL = (r, d) => ({ x: cellX(d) + L.DAY_W / 2, y: cellY(r) + L.ROW_H / 2 });

const FOCAL_DAY = 2, CANCEL_ROW = 3, TARGET_ROW = 1, TODAY = 2;

// canvas-space focal points (for the camera in HeroAnimation)
const GRID = {
  WIN,
  colXCanvas: WIN.x + cellCenterL(0, FOCAL_DAY).x,            // ~1082
  cancelYCanvas: WIN.y + cellCenterL(CANCEL_ROW, FOCAL_DAY).y, // ~707
  targetYCanvas: WIN.y + cellCenterL(TARGET_ROW, FOCAL_DAY).y, // ~443
};
GRID.midYCanvas = (GRID.cancelYCanvas + GRID.targetYCanvas) / 2;
window.GRID = GRID;

const DAYS = [
  { d: 'Sun', n: 31 }, { d: 'Mon', n: 1 }, { d: 'Tue', n: 2 },
  { d: 'Wed', n: 3 }, { d: 'Thu', n: 4 }, { d: 'Fri', n: 5 }, { d: 'Sat', n: 6 },
];

// shift color system — role tag + location tag (colorful but still wireframe text)
const ROLE_COLORS = [
  { bg: '#E4EEFB', bar: '#3F82C9' }, // blue
  { bg: '#ECE8F7', bar: '#7B5BB2' }, // purple
  { bg: '#FBE7E7', bar: '#CF4B4A' }, // red
  { bg: '#E9EAF7', bar: '#5B63C4' }, // indigo
];
const PRIORITY_COLOR = { bg: '#E0F2E4', bar: '#2E9E3C' };
const LOC_COLORS = {
  green: { bg: '#E7F4EA', bar: '#2E9E3C' },
  coral: { bg: '#FBEBE3', bar: '#C76A48' },
  amber: { bg: '#FAF1DD', bar: '#C68A2C' },
  teal:  { bg: '#E2F2F1', bar: '#2E9690' },
};
const FAC_LOC = {
  'Birchwood Park': 'green', 'Portside Health': 'coral',
  'Northern Cardinal': 'amber', 'Nansemond Pointe': 'teal',
  'Autumn Care': 'coral',
};
function _hash(s){ let h=0; for(let i=0;i<(s||'').length;i++) h=(h*31+s.charCodeAt(i))>>>0; return h; }

const ROWS = [
  { name: 'Aisha Armour',   initials: 'AA', hours: '29.9 hrs', tone: '#D6CFC4',
    shifts: { 0: { t: '7:25a–6:49p', f: 'Birchwood Park',  c: 'green' },
              4: { t: '7:00a–3:00p', f: 'Portside Health', c: 'purple' },
              5: { t: '7:00a–7:00p', f: 'Portside Health', c: 'purple' } } },
  { name: 'Maya Okafor',    initials: 'MO', hours: '22.5 hrs', tone: '#E0B9A6', candidate: true,
    shifts: { 1: { t: '7:00a–3:00p', f: 'Northern Cardinal', c: 'purple' },
              3: { t: '7:00a–3:00p', f: 'Northern Cardinal', c: 'purple' } } },
  { name: 'Anna Bradshaw',  initials: 'AB', hours: '46.5 hrs', tone: '#B9C7B0',
    shifts: { 0: { t: '6:58a–7:30p', f: 'Portside Health', c: 'green' },
              2: { t: '6:56a–7:00p', f: 'Portside Health', c: 'green' },
              3: { t: '7:00a–7:00p', f: 'Northern Cardinal', c: 'purple' },
              5: { t: '7:00a–7:00p', f: 'Portside Health', c: 'green' } } },
  { name: 'Camren Branch',  initials: 'CB', hours: '7.5 hrs',  tone: '#C9B6D6',
    shifts: { } },
  { name: 'Deon Carter',    initials: 'DC', hours: '31.2 hrs', tone: '#A9C2D6', candidate: true,
    shifts: { 1: { t: '7:13p–7:52a', f: 'Nansemond Pointe', c: 'green' },
              4: { t: '7:00p–7:00a', f: 'Nansemond Pointe', c: 'green' },
              6: { t: '7:00p–7:00a', f: 'Nansemond Pointe', c: 'green' } } },
  { name: 'Luka Petrov',    initials: 'LP', hours: '18.0 hrs', tone: '#C7C2B4', candidate: true,
    shifts: { 0: { t: '7:00p–7:00a', f: 'Nansemond Pointe', c: 'green' },
              5: { t: '7:00a–3:00p', f: 'Portside Health', c: 'purple' } } },
];

// Colorful-but-wireframe shift chip: grey time bar + colored role / priority / location tags.
function ChipTag({ c, w }) {
  return (
    <div style={{ height: 16, padding: '0 7px', borderRadius: 5, background: '#ffffffcc', display: 'inline-flex', alignItems: 'center' }}>
      <div style={{ height: 5, width: w, borderRadius: 3, background: c.bar }}></div>
    </div>
  );
}
function ShiftChip({ shift, w }) {
  const role = ROLE_COLORS[_hash(shift.t) % ROLE_COLORS.length];
  const loc = LOC_COLORS[FAC_LOC[shift.f] || 'green'];
  const priority = (_hash((shift.f || '') + (shift.t || '')) % 4) === 0;
  return (
    <div style={{
      position: 'absolute', left: 12, top: 12, width: w - 24, height: L.ROW_H - 24,
      background: role.bg, borderRadius: 9, padding: '11px 11px',
      boxSizing: 'border-box', overflow: 'hidden',
      border: `1px solid ${role.bar}22`,
      display: 'flex', flexDirection: 'column', gap: 9,
    }}>
      <div style={{ height: 7, width: '64%', borderRadius: 4, background: `${role.bar}55` }}></div>
      <div style={{ display: 'flex', gap: 5 }}>
        {priority && <ChipTag c={PRIORITY_COLOR} w={18} />}
        <ChipTag c={role} w={16} />
      </div>
      <ChipTag c={loc} w={Math.max(28, (_hash(shift.f) % 22) + 30)} />
    </div>
  );
}

// ── wireframe nav helpers ────────────────────────────────────────────────────
function NavItem({ icon, label, active, indent = 0, faded }) {
  const TB = window.TB;
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 11, height: 34, padding: `0 10px 0 ${10 + indent}px`,
      borderRadius: 8, background: active ? 'rgba(28,34,42,0.1)' : 'transparent',
    }}>
      <div style={{
        width: 17, height: 17, borderRadius: icon === 'o' ? 9 : 5, flexShrink: 0,
        background: active ? TB.coral : '#A8AFBA',
      }}></div>
      <div style={{
        height: 9, borderRadius: 5, flex: label === 'full' ? 1 : 'none', width: typeof label === 'number' ? label : 'auto',
        background: active ? TB.coral : '#C2C8D2', opacity: faded ? 0.7 : 1,
      }}></div>
    </div>
  );
}
function NavSection({ w }) {
  return <div style={{ height: 7, width: w, borderRadius: 4, background: '#D2D7DE', margin: '16px 10px 8px' }}></div>;
}

function ScheduleGrid({ cancelP = 0, moveP = 0 }) {
  const TB = window.TB;
  const me = window.Easing.easeInOutCubic(window.clamp(moveP, 0, 1));
  const from = cellCenterL(CANCEL_ROW, FOCAL_DAY);
  const to = cellCenterL(TARGET_ROW, FOCAL_DAY);
  const travelX = from.x + (to.x - from.x) * me;
  const travelY = from.y + (to.y - from.y) * me;
  const chipBg = me < 0.5 ? '#FFE9E9' : '#E6F6E7';
  const chipBar = me < 0.5 ? '#CC1E1D' : '#1D8A28';

  const pill = { height: 32, display: 'flex', alignItems: 'center', padding: '0 13px', borderRadius: 8, border: `1px solid ${TB.line}`, background: '#fff', fontSize: 12.5, fontWeight: 600, color: TB.ink2, whiteSpace: 'nowrap', gap: 6 };

  return (
    <div style={{ position: 'absolute', inset: 0, background: TB.deskGradient, overflow: 'hidden' }}>
      {/* browser window */}
      <div style={{
        position: 'absolute', left: WIN.x, top: WIN.y, width: WIN.w, height: WIN.h,
        borderRadius: 16, overflow: 'hidden', background: '#fff',
        boxShadow: '0 28px 70px rgba(28,34,42,0.14), 0 6px 16px rgba(28,34,42,0.07)',
        fontFamily: TB.sans,
      }}>
        {/* chrome bar */}
        <div style={{ position: 'absolute', top: 0, left: 0, width: '100%', height: L.CHROME, background: '#EEF0F3', borderBottom: `1px solid ${TB.line}`, display: 'flex', alignItems: 'center', padding: '0 16px', gap: 8 }}>
          <span style={{ width: 12, height: 12, borderRadius: 6, background: '#ED6A5E' }}></span>
          <span style={{ width: 12, height: 12, borderRadius: 6, background: '#F4BF4F' }}></span>
          <span style={{ width: 12, height: 12, borderRadius: 6, background: '#61C554' }}></span>
          <div style={{ flex: 1, display: 'flex', justifyContent: 'center' }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 8, height: 28, padding: '0 18px', borderRadius: 8, background: '#fff', border: `1px solid ${TB.line}`, fontSize: 13, color: TB.ink2, fontWeight: 500 }}>
              <span style={{ width: 9, height: 11, borderRadius: 2, border: `1.5px solid ${TB.ink3}`, marginRight: 1 }}></span>
              app.teambridge.com/schedule
            </div>
          </div>
        </div>

        {/* icon rail */}
        <div style={{ position: 'absolute', top: L.CHROME, left: 0, width: L.RAIL, bottom: 0, background: '#F1F3F6', borderRight: `1px solid ${TB.line}`, display: 'flex', flexDirection: 'column', alignItems: 'center', padding: '16px 0' }}>
          <div style={{ width: 30, height: 30, borderRadius: 8, background: TB.coral, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <div style={{ width: 13, height: 13, borderRadius: 3, border: '2px solid #fff' }}></div>
          </div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 14, marginTop: 26 }}>
            {[0, 1, 2, 3, 4, 5, 6].map(i => (
              <div key={i} style={{ width: 26, height: 26, borderRadius: 7, background: i === 0 ? 'rgba(28,34,42,0.14)' : '#DCE0E6' }}></div>
            ))}
          </div>
          <div style={{ flex: 1 }}></div>
          <div style={{ width: 30, height: 30, borderRadius: 15, background: '#A8AFBA', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 11, fontWeight: 700, color: '#fff' }}>TS</div>
        </div>

        {/* wireframe left nav */}
        <div style={{ position: 'absolute', top: L.CHROME, left: L.RAIL, width: L.NAV, bottom: 0, background: '#FAFBFC', borderRight: `1px solid ${TB.line}`, padding: '20px 12px', boxSizing: 'border-box' }}>
          <div style={{ padding: '0 8px 14px' }}><div style={{ height: 14, width: 96, borderRadius: 7, background: '#C5CCD6' }}></div></div>
          <NavItem icon="s" label={84} />
          <NavItem icon="s" label={58} />
          <NavItem icon="s" label={48} />
          <NavSection w={56} />
          <NavItem icon="o" label={96} />
          <NavItem icon="s" label={52} />
          <NavItem icon="s" label={74} />
          <NavItem icon="s" label={66} />
          <NavSection w={92} />
          <div style={{ display: 'flex', alignItems: 'center', gap: 11, height: 34, padding: '0 10px', borderRadius: 8, background: 'rgba(28,34,42,0.1)', borderLeft: `3px solid ${TB.coral}` }}>
            <div style={{ width: 17, height: 17, borderRadius: 5, background: TB.coral, flexShrink: 0 }}></div>
            <div style={{ height: 9, width: 100, borderRadius: 5, background: TB.coral }}></div>
          </div>
          <NavItem icon="s" label={70} />
          <NavItem icon="s" label={108} faded />
          <NavItem icon="s" label={96} faded />
          <NavItem icon="s" label={118} faded />
          <NavItem icon="s" label={88} faded />
        </div>

        {/* ===== MAIN ===== */}
        {/* header */}
        <div style={{ position: 'absolute', top: L.APP_TOP, left: L.MAIN_X, width: L.MAIN_W, height: L.HEADER, display: 'flex', alignItems: 'center', padding: '0 22px', gap: 14, borderBottom: `1px solid ${TB.line}`, boxSizing: 'border-box' }}>
          <div style={{ width: 20, height: 20, borderRadius: 5, border: `2px solid ${TB.ink3}` }}></div>
          <div style={{ height: 11, width: 150, borderRadius: 6, background: '#C5CCD6' }}></div>
          <div style={{ flex: 1 }}></div>
          <div style={{ width: 170, height: 32, borderRadius: 8, background: '#F1F3F6', border: `1px solid ${TB.line}` }}></div>
          <div style={{ height: 9, width: 56, borderRadius: 5, background: '#DCE0E7' }}></div>
          <div style={{ height: 34, padding: '0 16px', borderRadius: 8, background: '#F1F3F6', border: `1px solid ${TB.line}`, display: 'flex', alignItems: 'center' }}><div style={{ height: 8, width: 52, borderRadius: 4, background: '#C5CCD6' }}></div></div>
          <div style={{ height: 34, padding: '0 18px', borderRadius: 8, background: TB.key, display: 'flex', alignItems: 'center' }}><div style={{ height: 8, width: 50, borderRadius: 4, background: 'rgba(255,255,255,0.55)' }}></div></div>
        </div>

        {/* filter row */}
        <div style={{ position: 'absolute', top: L.APP_TOP + L.HEADER, left: L.MAIN_X, width: L.MAIN_W, height: L.FILTER, display: 'flex', alignItems: 'center', padding: '0 22px', gap: 10, boxSizing: 'border-box' }}>
          <div style={{ ...pill, width: 96 }}><div style={{ height: 7, width: 64, borderRadius: 4, background: '#C5CCD6' }}></div></div>
          <div style={{ ...pill, width: 116 }}><div style={{ height: 7, width: 84, borderRadius: 4, background: '#DCE0E7' }}></div></div>
          <div style={{ ...pill, width: 100 }}><div style={{ height: 7, width: 68, borderRadius: 4, background: '#DCE0E7' }}></div></div>
          <div style={{ ...pill, width: 50, background: '#EDEFF3', borderColor: '#D7DBE2' }}><div style={{ height: 7, width: 24, borderRadius: 4, background: '#C5CCD6' }}></div></div>
          <div style={{ flex: 1 }}></div>
          <div style={{ ...pill, width: 34, justifyContent: 'center' }}><div style={{ width: 6, height: 6, borderRadius: 3, background: '#C5CCD6' }}></div></div>
          <div style={{ ...pill, width: 168 }}><div style={{ height: 8, width: 120, borderRadius: 4, background: '#B7BEC9' }}></div></div>
          <div style={{ ...pill, width: 34, justifyContent: 'center' }}><div style={{ width: 6, height: 6, borderRadius: 3, background: '#C5CCD6' }}></div></div>
          <div style={{ ...pill, width: 64 }}><div style={{ height: 7, width: 38, borderRadius: 4, background: '#C5CCD6' }}></div></div>
        </div>

        {/* today column highlight */}
        <div style={{ position: 'absolute', top: L.APP_TOP + L.HEADER + L.FILTER, left: cellX(TODAY), width: L.DAY_W, bottom: 0, background: 'rgba(28,34,42,0.05)', borderLeft: `1px solid ${TB.line}`, borderRight: `1px solid ${TB.line}` }}></div>

        {/* day header */}
        <div style={{ position: 'absolute', top: L.APP_TOP + L.HEADER + L.FILTER, left: L.MAIN_X, width: L.MAIN_W, height: L.DAYHDR, borderBottom: `1px solid ${TB.line}` }}>
          {DAYS.map((day, i) => {
            const today = i === TODAY;
            return (
              <div key={i} style={{ position: 'absolute', top: 0, left: cellX(i) - L.MAIN_X, width: L.DAY_W, height: L.DAYHDR, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 7 }}>
                <span style={{ fontSize: 15, fontWeight: today ? 750 : 600, color: today ? TB.ink : TB.ink2, letterSpacing: '-0.01em' }}>{day.d}</span>
                <span style={{ fontSize: 14, fontWeight: 700, color: today ? '#fff' : TB.ink2, background: today ? TB.coral : 'transparent', width: today ? 26 : 'auto', height: 26, borderRadius: 13, display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>{day.n}</span>
              </div>
            );
          })}
        </div>

        {/* grid lines */}
        {ROWS.map((_, r) => <div key={'h' + r} style={{ position: 'absolute', left: L.MAIN_X, width: L.MAIN_W, top: cellY(r), height: 1, background: TB.line }}></div>)}
        {DAYS.map((_, i) => <div key={'v' + i} style={{ position: 'absolute', top: L.APP_TOP + L.HEADER + L.FILTER, bottom: 0, left: cellX(i), width: 1, background: TB.line }}></div>)}

        {/* employee rows */}
        {ROWS.map((row, r) => (
          <div key={row.name} style={{ position: 'absolute', left: L.MAIN_X, top: cellY(r), width: L.LEFT, height: L.ROW_H, display: 'flex', alignItems: 'center', gap: 14, padding: '0 20px', boxSizing: 'border-box' }}>
            <div style={{ width: 38, height: 38, borderRadius: 19, background: '#DCE0E7', flexShrink: 0 }}></div>
            <div style={{ minWidth: 0, display: 'flex', flexDirection: 'column', gap: 8 }}>
              <div style={{ height: 9, width: 132, borderRadius: 5, background: '#C5CCD6' }}></div>
              <div style={{ height: 7, width: 72, borderRadius: 4, background: '#DCE0E7' }}></div>
            </div>
          </div>
        ))}

        {/* data-driven chips (skip focal cells) */}
        {ROWS.map((row, r) => DAYS.map((_, d) => {
          const shift = row.shifts[d];
          if (!shift) return null;
          return (
            <div key={`${r}-${d}`} style={{ position: 'absolute', left: cellX(d), top: cellY(r), width: L.DAY_W, height: L.ROW_H }}>
              <ShiftChip shift={shift} w={L.DAY_W} />
            </div>
          );
        }))}

        {/* FOCAL: Camren Tuesday chip fades on cancel */}
        <div style={{ position: 'absolute', left: cellX(FOCAL_DAY), top: cellY(CANCEL_ROW), width: L.DAY_W, height: L.ROW_H, opacity: 1 - cancelP }}>
          <ShiftChip shift={{ t: '3:00p–11:00p', f: 'Autumn Care', c: 'amber' }} w={L.DAY_W} />
        </div>

        {/* FOCAL: open slot */}
        <div style={{
          position: 'absolute', left: cellX(FOCAL_DAY) + 12, top: cellY(CANCEL_ROW) + 12,
          width: L.DAY_W - 24, height: L.ROW_H - 24, boxSizing: 'border-box',
          border: '2px dashed #FFA8A7', borderRadius: 9, background: 'rgba(255,37,36,0.05)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          opacity: cancelP * (1 - window.clamp(me * 1.4 - 0.2, 0, 1)),
        }}>
          <div style={{ fontSize: 12, fontWeight: 750, color: '#991616', letterSpacing: '0.08em', whiteSpace: 'nowrap' }}>CALLED OUT</div>
        </div>

        {/* FOCAL: travelling / filled chip */}
        {moveP > 0.001 && (
          <div style={{
            position: 'absolute', left: travelX - (L.DAY_W - 24) / 2, top: travelY - (L.ROW_H - 24) / 2,
            width: L.DAY_W - 24, height: L.ROW_H - 24, boxSizing: 'border-box',
            background: chipBg, borderRadius: 9, borderLeft: `3px solid ${chipBar}`,
            padding: '12px 12px', boxShadow: '0 16px 34px rgba(29,138,40,0.2)',
            transform: `scale(${1 + 0.06 * Math.sin(me * Math.PI)})`,
            display: 'flex', flexDirection: 'column', gap: 10,
          }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 7 }}>
              <div style={{ height: 8, width: '60%', borderRadius: 4, background: me > 0.5 ? '#9FD3A6' : '#F2A6A5' }}></div>
              {me > 0.98 && <span style={{ fontSize: 13, color: '#1D8A28', marginLeft: 'auto' }}>✓</span>}
            </div>
            <div style={{ height: 7, width: '42%', borderRadius: 4, background: me > 0.5 ? '#C3E6C8' : '#F7C9C8' }}></div>
          </div>
        )}
      </div>
    </div>
  );
}

window.ScheduleGrid = ScheduleGrid;
window.ROWS = ROWS;
