// hq-shell.jsx — Muyassir HQ cockpit chrome: icons, topbar, read-only primitives, charts.

// ── Icons ────────────────────────────────────────────────────────────────
function HqIcon({ name, size = 20, color = 'currentColor', stroke = 1.6 }) {
  const p = { fill: 'none', stroke: color, strokeWidth: stroke, strokeLinecap: 'round', strokeLinejoin: 'round' };
  const paths = {
    grid:     <><rect x="3.5" y="3.5" width="7" height="7" rx="1.5" {...p}/><rect x="13.5" y="3.5" width="7" height="7" rx="1.5" {...p}/><rect x="3.5" y="13.5" width="7" height="7" rx="1.5" {...p}/><rect x="13.5" y="13.5" width="7" height="7" rx="1.5" {...p}/></>,
    building: <><path d="M4 21V6a1 1 0 0 1 1-1h7a1 1 0 0 1 1 1v15" {...p}/><path d="M13 10h6a1 1 0 0 1 1 1v10" {...p}/><path d="M4 21h17" {...p}/><path d="M7.5 9h2M7.5 13h2M7.5 17h2M16 14h1M16 17.5h1" {...p} strokeWidth={stroke*0.85}/></>,
    users:    <><circle cx="9" cy="8" r="3" {...p}/><circle cx="17" cy="9" r="2.2" {...p}/><path d="M3 19c0-3 2.7-5 6-5s6 2 6 5" {...p}/><path d="M15 19c0-2 1.5-3.5 4-3.5S22 17 22 19" {...p}/></>,
    card:     <><rect x="3" y="6" width="18" height="12" rx="2" {...p}/><path d="M3 10h18" {...p}/><path d="M7 14.5h3" {...p}/></>,
    search:   <><circle cx="11" cy="11" r="6" {...p}/><path d="M16 16l4 4" {...p}/></>,
    bell:     <><path d="M6 9a6 6 0 1 1 12 0c0 4 1.5 6 1.5 6h-15S6 13 6 9z" {...p}/><path d="M10 19a2 2 0 0 0 4 0" {...p}/></>,
    external: <><path d="M14 5h5v5M19 5l-7 7" {...p}/><path d="M19 14v4a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1h4" {...p}/></>,
    chevronD: <path d="M6 9l6 6 6-6" {...p}/>,
    arrowR:   <><path d="M5 12h14M13 6l6 6-6 6" {...p}/></>,
    check:    <path d="M5 12.5l4.5 4.5L19 7" {...p}/>,
    x:        <path d="M6 6l12 12M18 6L6 18" {...p}/>,
    trendUp:  <><path d="M3 17l6-6 4 4 8-8" {...p}/><path d="M16 7h5v5" {...p}/></>,
    trendDown:<><path d="M3 7l6 6 4-4 8 8" {...p}/><path d="M16 17h5v-5" {...p}/></>,
    alert:    <><path d="M12 4l9 16H3l9-16z" {...p}/><path d="M12 10v4M12 17v.4" {...p}/></>,
    clock:    <><circle cx="12" cy="12" r="9" {...p}/><path d="M12 7v5l3 2" {...p}/></>,
    bolt:     <path d="M13 3L5 13h5l-1 8 8-10h-5l1-8z" {...p}/>,
    lock:     <><rect x="5" y="11" width="14" height="9" rx="2" {...p}/><path d="M8 11V8a4 4 0 0 1 8 0v3" {...p}/></>,
    refresh:  <><path d="M20 11a8 8 0 0 0-14-5L4 8M4 4v4h4" {...p}/><path d="M4 13a8 8 0 0 0 14 5l2-2M20 20v-4h-4" {...p}/></>,
    sun:      <><circle cx="12" cy="12" r="4" {...p}/><path d="M12 3v2M12 19v2M3 12h2M19 12h2M5.6 5.6l1.4 1.4M17 17l1.4 1.4M5.6 18.4L7 17M17 7l1.4-1.4" {...p}/></>,
    star8:    <path d="M12 3l1.6 5.1 5.1.2-4 3.2 1.4 4.9L12 13.7 7.9 16.6l1.4-4.9-4-3.2 5.1-.2L12 3z" {...p}/>,
  };
  return <svg viewBox="0 0 24 24" width={size} height={size} style={{ display: 'block', flexShrink: 0 }}>{paths[name] || paths.grid}</svg>;
}

// ── Topbar ───────────────────────────────────────────────────────────────
function HqTopbar() {
  return (
    <header style={{
      height: 64, flexShrink: 0, background: 'var(--ug-card)', borderBottom: '1px solid var(--ug-hairline)',
      display: 'flex', alignItems: 'center', gap: 16, padding: '0 26px', position: 'sticky', top: 0, zIndex: 20,
    }}>
      {/* Brand */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 11 }}>
        <div style={{ width: 32, height: 32, borderRadius: 9, background: 'var(--ug-primary)', color: 'var(--ug-gold)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
          <HqIcon name="star8" size={20} stroke={1.4} />
        </div>
        <div>
          <div style={{ fontFamily: 'var(--ug-display)', fontSize: 17, fontWeight: 'var(--ug-display-weight)', letterSpacing: 'var(--ug-display-letter)', color: 'var(--ug-ink)', lineHeight: 1 }}>Muyassir</div>
          <div style={{ fontSize: 9.5, color: 'var(--ug-muted)', letterSpacing: '0.18em', textTransform: 'uppercase', marginTop: 3, fontWeight: 700 }}>HQ · Internal</div>
        </div>
      </div>

      <div style={{ width: 1, height: 26, background: 'var(--ug-hairline)', margin: '0 4px' }} />
      <div style={{ fontSize: 13.5, fontWeight: 600, color: 'var(--ug-ink-soft)' }}>Operator cockpit</div>

      <div style={{ flex: 1 }} />

      <button style={hqIconBtn} aria-label="Notifications">
        <HqIcon name="bell" size={18} color="var(--ug-ink-soft)" />
        <span style={{ position: 'absolute', top: 8, right: 9, width: 7, height: 7, borderRadius: 4, background: 'var(--ug-danger)', border: '1.5px solid var(--ug-card)' }} />
      </button>

      <div style={{ display: 'flex', alignItems: 'center', gap: 9, paddingLeft: 4 }}>
        <div style={{ width: 32, height: 32, borderRadius: 16, background: 'var(--ug-gold)', color: 'var(--ug-primary-dark)', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 12, fontWeight: 700 }}>{HQ_USER.initials}</div>
        <div style={{ lineHeight: 1.25 }}>
          <div style={{ fontSize: 12.5, fontWeight: 600, color: 'var(--ug-ink)' }}>{HQ_USER.name}</div>
          <div style={{ fontSize: 10.5, color: 'var(--ug-muted)' }}>{HQ_USER.role}</div>
        </div>
      </div>
    </header>
  );
}
const hqIconBtn = {
  width: 38, height: 38, borderRadius: 9, border: '1px solid var(--ug-hairline)', background: 'var(--ug-card)',
  cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', position: 'relative', flexShrink: 0,
};

// ── Primitives ───────────────────────────────────────────────────────────
function HqCard({ children, style, pad = 20 }) {
  return <div style={{ background: 'var(--ug-card)', border: '1px solid var(--ug-hairline)', borderRadius: 14, padding: pad, ...style }}>{children}</div>;
}
function HqLabel({ children, style }) {
  return <div style={{ fontSize: 11, letterSpacing: '0.15em', textTransform: 'uppercase', color: 'var(--ug-muted)', fontWeight: 600, ...style }}>{children}</div>;
}
function HqBadge({ label, color }) {
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 5, fontSize: 11.5, fontWeight: 600, letterSpacing: '0.01em',
      padding: '3px 9px 3px 7px', borderRadius: 7, whiteSpace: 'nowrap',
      color: color, background: `color-mix(in srgb, ${color} 13%, transparent)`,
    }}>
      <span style={{ width: 6, height: 6, borderRadius: 3, background: color }} />
      {label}
    </span>
  );
}

// Plan chip — Base / Premium / Trial.
function HqPlanChip({ plan }) {
  const pl = HQ_PLANS[plan] || HQ_PLANS.base;
  const premium = plan === 'premium', trial = plan === 'trial';
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 5, fontSize: 11.5, fontWeight: 600, padding: '3px 9px',
      borderRadius: 7, whiteSpace: 'nowrap',
      color: premium ? 'var(--ug-primary)' : trial ? '#5B7A9A' : 'var(--ug-ink-soft)',
      border: `1px solid ${premium ? 'color-mix(in srgb, var(--ug-primary) 35%, transparent)' : trial ? 'color-mix(in srgb, #5B7A9A 35%, transparent)' : 'var(--ug-hairline)'}`,
      background: premium ? 'color-mix(in srgb, var(--ug-primary) 8%, transparent)' : trial ? 'color-mix(in srgb, #5B7A9A 8%, transparent)' : 'var(--ug-surface)',
    }}>
      {premium && <span style={{ width: 5, height: 5, borderRadius: 3, background: 'var(--ug-gold)' }} />}
      {pl.label}
    </span>
  );
}

// Deep-link OUT button. The label states the PURPOSE; the tooltip names the tool.
function HqLinkOut({ tool, label, size = 'sm', block = false }) {
  const s = HQ_SYNC[tool];
  return (
    <a href="#" onClick={e => { e.preventDefault(); e.stopPropagation(); }} title={`${s.purpose} — opens ${s.label} in a new tab`} style={{
      display: block ? 'flex' : 'inline-flex', alignItems: 'center', justifyContent: block ? 'space-between' : 'center', gap: 7, textDecoration: 'none',
      fontFamily: 'var(--ug-body)', fontWeight: 600, cursor: 'pointer', borderRadius: 8,
      fontSize: size === 'sm' ? 12 : 13, padding: size === 'sm' ? '5px 9px' : '9px 13px',
      border: '1px solid var(--ug-hairline)', background: 'var(--ug-card)', color: 'var(--ug-ink-soft)',
      whiteSpace: 'nowrap', transition: 'background .15s, border-color .15s', width: block ? '100%' : 'auto',
    }}
      onMouseEnter={e => { e.currentTarget.style.background = 'var(--ug-surface-alt)'; e.currentTarget.style.borderColor = `color-mix(in srgb, ${s.color} 45%, var(--ug-hairline))`; }}
      onMouseLeave={e => { e.currentTarget.style.background = 'var(--ug-card)'; e.currentTarget.style.borderColor = 'var(--ug-hairline)'; }}
    >
      <span style={{ display: 'inline-flex', alignItems: 'center', gap: 7, minWidth: 0 }}>
        <span style={{ width: 7, height: 7, borderRadius: 2, background: s.color, flexShrink: 0 }} />
        {label || s.purpose}
      </span>
      <HqIcon name="external" size={size === 'sm' ? 12 : 14} color="var(--ug-muted)" stroke={1.8} />
    </a>
  );
}

// Delta chip
function HqDelta({ value, suffix = '%', up }) {
  const pos = up != null ? up : value >= 0;
  const tone = pos ? 'var(--ug-success)' : 'var(--ug-danger)';
  return (
    <span style={{ display: 'inline-flex', alignItems: 'center', gap: 2, fontSize: 12, fontWeight: 700, color: tone }}>
      <HqIcon name={pos ? 'trendUp' : 'trendDown'} size={13} color={tone} stroke={2} />
      {Math.abs(value)}{suffix}
    </span>
  );
}

// Area sparkline
function HqSpark({ data, width = 120, height = 34, color = 'var(--ug-success)' }) {
  const max = Math.max(...data), min = Math.min(...data), rng = max - min || 1;
  const pts = data.map((v, i) => [ (i / (data.length - 1)) * width, height - ((v - min) / rng) * (height - 6) - 3 ]);
  const line = pts.map((pt, i) => `${i ? 'L' : 'M'}${pt[0].toFixed(1)} ${pt[1].toFixed(1)}`).join(' ');
  const area = `${line} L${width} ${height} L0 ${height} Z`;
  const gid = 'sp' + Math.random().toString(36).slice(2, 7);
  return (
    <svg width={width} height={height} style={{ display: 'block', overflow: 'visible' }}>
      <defs><linearGradient id={gid} x1="0" y1="0" x2="0" y2="1">
        <stop offset="0%" stopColor={color} stopOpacity="0.18" /><stop offset="100%" stopColor={color} stopOpacity="0" />
      </linearGradient></defs>
      <path d={area} fill={`url(#${gid})`} />
      <path d={line} fill="none" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
      <circle cx={pts[pts.length-1][0]} cy={pts[pts.length-1][1]} r="2.4" fill={color} />
    </svg>
  );
}

// Horizontal meter
function HqMeter({ value, color = 'var(--ug-primary)', height = 6, track = 'var(--ug-surface-alt)' }) {
  return (
    <div style={{ height, borderRadius: height, background: track, overflow: 'hidden', width: '100%' }}>
      <div style={{ height: '100%', width: `${Math.max(3, value * 100)}%`, background: color, borderRadius: height }} />
    </div>
  );
}

Object.assign(window, {
  HqIcon, HqTopbar, HqCard, HqLabel, HqBadge, HqPlanChip, HqLinkOut, HqDelta, HqSpark, HqMeter, hqIconBtn,
});
