// admin-ui-2.jsx — More primitives: section, drawer, employee chip,
// timecard status pill, severity dot, browser-window chrome, sidebar nav.

// ── Section ──────────────────────────────────────────────────
function AdminSection({ title, action, children, style }) {
  return (
    <section style={{ marginBottom: 28, ...style }}>
      <div style={{
        display: 'flex', alignItems: 'baseline', justifyContent: 'space-between',
        marginBottom: 12,
      }}>
        <h2 style={{ margin: 0, fontSize: 15, fontWeight: 700, letterSpacing: -0.2 }}>{title}</h2>
        {action && <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>{action}</div>}
      </div>
      {children}
    </section>
  );
}

// ── Employee inline chip ─────────────────────────────────────
function EmpChip({ empId, withRole, onClick }) {
  const { employees, setEmployeeDrawer } = useAdmin();
  const e = employees.find(x => x.id === empId);
  if (!e) return <span style={{ color: 'var(--muted)' }}>—</span>;
  return (
    <button onClick={() => onClick ? onClick(e) : setEmployeeDrawer(e)} style={{
      background: 'transparent', border: 'none', padding: 0, cursor: 'pointer',
      display: 'inline-flex', alignItems: 'center', gap: 8, textAlign: 'left',
    }}>
      <div style={{
        width: 26, height: 26, borderRadius: '50%',
        background: 'var(--panel-3)', color: 'var(--text-2)',
        display: 'grid', placeItems: 'center', fontSize: 10.5, fontWeight: 700,
        flexShrink: 0,
      }}>{e.avatar}</div>
      <div style={{ minWidth: 0 }}>
        <div style={{ fontSize: 13, fontWeight: 600 }}>{e.name}</div>
        {withRole && <div style={{ fontSize: 11, color: 'var(--muted)' }}>{e.role} · <span className="mono">{e.badge}</span></div>}
      </div>
    </button>
  );
}

// ── Timecard status pill (workflow-aware) ────────────────────
const TC_STATUS = {
  'draft':        { label: 'Draft',           tone: 'neutral' },
  'pending-ops':  { label: 'Ops review',      tone: 'warn'   },
  'pending-acct': { label: 'Accounting',      tone: 'info'   },
  'approved':     { label: 'Approved',        tone: 'good'   },
  'rejected':     { label: 'Rejected',        tone: 'bad'    },
  'paid':         { label: 'Paid',            tone: 'dark'   },
};
function TimecardStatusPill({ status }) {
  const s = TC_STATUS[status] || TC_STATUS.draft;
  return <Pill tone={s.tone}>{s.label}</Pill>;
}

// ── Severity dot ─────────────────────────────────────────────
function SeverityDot({ severity }) {
  const c = { critical: 'var(--bad)', high: 'var(--bad)', med: 'var(--warn)', low: 'var(--muted)' }[severity] || 'var(--muted)';
  return <span style={{ display: 'inline-block', width: 8, height: 8, borderRadius: 4, background: c, marginRight: 8, verticalAlign: 'middle' }} />;
}

// ── Browser window chrome ────────────────────────────────────
function BrowserWindow({ url = 'console.railflagspro.com', children, scaleToFit = true }) {
  const W = 1600, H = 1000;
  const [scale, setScale] = useState(1);

  useEffect(() => {
    if (!scaleToFit) return;
    const compute = () => {
      const vw = window.innerWidth - 32;
      const vh = window.innerHeight - 32;
      setScale(Math.min(vw / W, vh / H, 1));
    };
    compute();
    window.addEventListener('resize', compute);
    return () => window.removeEventListener('resize', compute);
  }, [scaleToFit]);

  return (
    <div style={{
      width: W * scale, height: H * scale,
      borderRadius: 14, overflow: 'hidden',
      boxShadow: '0 50px 100px rgba(0,0,0,0.5), 0 0 0 1px rgba(255,255,255,0.05)',
    }}>
      <div style={{
        transform: `scale(${scale})`, transformOrigin: 'top left',
        width: W, height: H,
        display: 'flex', flexDirection: 'column',
        background: 'var(--bg)',
      }}>
        {/* Title bar */}
        <div style={{
          height: 38, flexShrink: 0,
          background: '#E9EAEC', borderBottom: '1px solid #D4D6DA',
          display: 'flex', alignItems: 'center', padding: '0 14px', gap: 14,
        }}>
          <div style={{ display: 'flex', gap: 7 }}>
            <span style={{ width: 12, height: 12, borderRadius: '50%', background: '#FF5F57' }} />
            <span style={{ width: 12, height: 12, borderRadius: '50%', background: '#FEBC2E' }} />
            <span style={{ width: 12, height: 12, borderRadius: '50%', background: '#28C840' }} />
          </div>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8, flex: 1 }}>
            <IconChevronLeft size={14} style={{ color: '#6B7280' }} />
            <IconChevronRight size={14} style={{ color: '#6B7280' }} />
            <div style={{
              flex: 1, maxWidth: 520, margin: '0 auto',
              padding: '5px 14px', borderRadius: 6,
              background: '#fff', border: '1px solid #D4D6DA',
              fontFamily: 'var(--font-mono)', fontSize: 12,
              color: 'var(--text-2)',
              display: 'flex', alignItems: 'center', gap: 8,
            }}>
              <span style={{
                width: 12, height: 12, display: 'inline-grid', placeItems: 'center',
                color: 'var(--good)',
              }}>
                <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4">
                  <rect x="3" y="11" width="18" height="11" rx="2" />
                  <path d="M7 11V7a5 5 0 0 1 10 0v4" />
                </svg>
              </span>
              {url}
            </div>
          </div>
          <div style={{ width: 30 }} />
        </div>
        {/* Tab bar */}
        <div style={{
          height: 32, flexShrink: 0,
          background: '#DBDDE1', borderBottom: '1px solid #C7CACF',
          display: 'flex', alignItems: 'flex-end', padding: '0 12px', gap: 4,
        }}>
          <div style={{
            background: 'var(--bg)', padding: '6px 14px',
            borderRadius: '8px 8px 0 0', fontSize: 12.5, fontWeight: 500,
            display: 'flex', alignItems: 'center', gap: 8,
            color: 'var(--text-2)',
            border: '1px solid #C7CACF', borderBottom: 'none',
          }}>
            <span style={{ width: 14, height: 14, borderRadius: 3, background: 'var(--accent)' }} />
            Operations Console
          </div>
          <div style={{ background: 'transparent', padding: '4px 12px', fontSize: 12.5, color: 'var(--muted)' }}>+ New Tab</div>
        </div>
        {/* Body */}
        <div style={{ flex: 1, display: 'flex', overflow: 'hidden' }}>
          {children}
        </div>
      </div>
    </div>
  );
}

// ── Right-side drawer ────────────────────────────────────────
function Drawer({ open, onClose, title, eyebrow, width = 560, children, footer }) {
  if (!open) return null;
  return (
    <div style={{ position: 'absolute', inset: 0, zIndex: 300 }}>
      <div onClick={onClose} style={{ position: 'absolute', inset: 0, background: 'rgba(15,23,42,0.45)' }} />
      <div style={{
        position: 'absolute', right: 0, top: 0, bottom: 0,
        width, background: 'var(--paper)',
        boxShadow: '-30px 0 80px rgba(0,0,0,0.3)',
        display: 'flex', flexDirection: 'column',
      }}>
        <div style={{
          padding: '18px 24px',
          borderBottom: '1px solid var(--line)',
          display: 'flex', alignItems: 'center', justifyContent: 'space-between',
          background: 'var(--panel)',
        }}>
          <div>
            {eyebrow && <div className="eyebrow" style={{ marginBottom: 3 }}>{eyebrow}</div>}
            <div style={{ fontSize: 18, fontWeight: 700, letterSpacing: -0.2 }}>{title}</div>
          </div>
          <button onClick={onClose} style={{
            width: 32, height: 32, borderRadius: 16, border: 'none', background: 'var(--panel-3)',
            display: 'grid', placeItems: 'center',
          }}><IconClose size={16} /></button>
        </div>
        <div style={{ flex: 1, overflowY: 'auto', padding: '20px 24px' }}>{children}</div>
        {footer && (
          <div style={{
            padding: '14px 24px', borderTop: '1px solid var(--line)',
            background: 'var(--panel)',
            display: 'flex', justifyContent: 'flex-end', gap: 10,
          }}>{footer}</div>
        )}
      </div>
    </div>
  );
}

// ── Generic dotted progress bar ──────────────────────────────
function ProgressBar({ value, max = 100, tone = 'good', height = 6 }) {
  const pct = Math.max(0, Math.min(100, (value / max) * 100));
  const color = { good: 'var(--good)', warn: 'var(--warn)', bad: 'var(--bad)', info: 'var(--info)', accent: 'var(--accent)' }[tone] || 'var(--text)';
  return (
    <div style={{ height, borderRadius: height / 2, background: 'var(--line)', overflow: 'hidden' }}>
      <div style={{ width: `${pct}%`, height: '100%', background: color, transition: 'width 200ms ease' }} />
    </div>
  );
}

// ── Filter chip ──────────────────────────────────────────────
function FilterChip({ active, onClick, children, count }) {
  return (
    <button onClick={onClick} style={{
      background: active ? 'var(--text)' : 'var(--panel)',
      border: `1px solid ${active ? 'var(--text)' : 'var(--line)'}`,
      color: active ? '#fff' : 'var(--text-2)',
      padding: '5px 12px', borderRadius: 100,
      fontSize: 12.5, fontWeight: 500, fontFamily: 'inherit',
      display: 'inline-flex', alignItems: 'center', gap: 6,
    }}>
      {children}
      {count != null && (
        <span style={{
          background: active ? 'rgba(255,255,255,0.2)' : 'var(--panel-3)',
          color: active ? '#fff' : 'var(--muted)',
          padding: '0 6px', borderRadius: 8, fontSize: 11, fontWeight: 600,
          fontFamily: 'var(--font-mono)',
        }}>{count}</span>
      )}
    </button>
  );
}

Object.assign(window, {
  AdminSection, EmpChip, TimecardStatusPill, TC_STATUS, SeverityDot,
  BrowserWindow, Drawer, ProgressBar, FilterChip,
});
