// components.jsx — shared UI primitives for Idea Pipeline

const { useState, useRef, useEffect } = React;
const { USERS, ME, STAGES, PRIORITIES, IDEA_STATUS } = window.SEED;

const userById = (id) => USERS.find((u) => u.id === id);

// ---- tiny line-icon set (standard UI glyphs) ----
function Icon({ name, size = 18, stroke = 1.8, style }) {
  const p = {
    plus:      <><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></>,
    search:    <><circle cx="11" cy="11" r="7" /><line x1="16.5" y1="16.5" x2="21" y2="21" /></>,
    grid:      <><rect x="3" y="3" width="7" height="7" rx="1.5" /><rect x="14" y="3" width="7" height="7" rx="1.5" /><rect x="3" y="14" width="7" height="7" rx="1.5" /><rect x="14" y="14" width="7" height="7" rx="1.5" /></>,
    board:     <><rect x="3" y="4" width="5" height="16" rx="1.5" /><rect x="10" y="4" width="5" height="11" rx="1.5" /><rect x="17" y="4" width="4" height="14" rx="1.5" /></>,
    check:     <polyline points="4 12 9 17 20 6" />,
    user:      <><circle cx="12" cy="8" r="4" /><path d="M4 21c0-4 4-6 8-6s8 2 8 6" /></>,
    users:     <><circle cx="9" cy="8" r="3.5" /><path d="M2.5 20c0-3.5 3-5 6.5-5s6.5 1.5 6.5 5" /><path d="M16 5.5a3.5 3.5 0 0 1 0 7" /><path d="M17 15.2c2.7.4 4.5 1.8 4.5 4.8" /></>,
    bolt:      <polygon points="13 2 4 14 11 14 10 22 20 9 13 9" />,
    chevron:   <polyline points="9 6 15 12 9 18" />,
    chevronL:  <polyline points="15 6 9 12 15 18" />,
    clock:     <><circle cx="12" cy="12" r="9" /><polyline points="12 7 12 12 16 14" /></>,
    flag:      <><line x1="5" y1="3" x2="5" y2="21" /><path d="M5 4h12l-2.5 4L17 12H5" /></>,
    dots:      <><circle cx="5" cy="12" r="1.4" /><circle cx="12" cy="12" r="1.4" /><circle cx="19" cy="12" r="1.4" /></>,
    x:         <><line x1="6" y1="6" x2="18" y2="18" /><line x1="18" y1="6" x2="6" y2="18" /></>,
    calendar:  <><rect x="3" y="5" width="18" height="16" rx="2" /><line x1="3" y1="9" x2="21" y2="9" /><line x1="8" y1="3" x2="8" y2="6" /><line x1="16" y1="3" x2="16" y2="6" /></>,
    spark:     <path d="M12 3l1.8 5.6L19.5 10l-5.7 1.4L12 17l-1.8-5.6L4.5 10l5.7-1.4z" />,
    arrow:     <><line x1="4" y1="12" x2="20" y2="12" /><polyline points="14 6 20 12 14 18" /></>,
    wallet:    <><rect x="3" y="6" width="18" height="13" rx="2.5" /><path d="M3 9h13a2 2 0 0 1 2 2v2a2 2 0 0 1-2 2H3" /><circle cx="16.5" cy="12.5" r="1" fill="currentColor" stroke="none" /></>,
    receipt:   <><path d="M5 3v18l2-1.4 2 1.4 2-1.4 2 1.4 2-1.4 2 1.4V3l-2 1.4L15 3l-2 1.4L11 3 9 4.4 7 3z" /><line x1="8" y1="9" x2="16" y2="9" /><line x1="8" y1="13" x2="13" y2="13" /></>,
    pie:       <><path d="M12 3a9 9 0 1 0 9 9h-9z" /><path d="M12 3v9h9a9 9 0 0 0-9-9z" /></>,
    download:  <><path d="M12 3v12" /><polyline points="7 11 12 16 17 11" /><path d="M4 20h16" /></>,
    comment:   <path d="M21 12a8 8 0 0 1-11.5 7.2L4 20l1-4.2A8 8 0 1 1 21 12z" />,
    send:      <><line x1="11" y1="13" x2="21" y2="3" /><polygon points="21 3 14.5 21 11 13 3 9.5 21 3" /></>,
    logout:    <><path d="M15 4h3a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1h-3" /><polyline points="10 8 14 12 10 16" /><line x1="14" y1="12" x2="4" y2="12" /></>,
  }[name];
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
      stroke="currentColor" strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round"
      style={style} aria-hidden="true">{p}</svg>
  );
}

// ---- avatar ----
function Avatar({ id, size = 28, ring = false }) {
  const u = userById(id);
  if (!u) return null;
  return (
    <span title={u.name} style={{
      width: size, height: size, borderRadius: '50%', flex: 'none',
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
      fontSize: size * 0.36, fontWeight: 700, letterSpacing: '.01em',
      color: `oklch(0.42 0.12 ${u.hue})`,
      background: `oklch(0.92 0.06 ${u.hue})`,
      boxShadow: ring ? '0 0 0 2px var(--surface)' : 'none',
      fontFamily: 'var(--font-ui)',
    }}>{u.initials}</span>
  );
}

function AvatarStack({ ids, size = 26, max = 4 }) {
  const shown = ids.slice(0, max);
  const extra = ids.length - shown.length;
  return (
    <div style={{ display: 'flex', alignItems: 'center' }}>
      <div style={{ display: 'flex' }}>
        {shown.map((id, i) => (
          <span key={id} style={{ marginLeft: i === 0 ? 0 : -size * 0.32 }}>
            <Avatar id={id} size={size} ring />
          </span>
        ))}
      </div>
      {extra > 0 && (
        <span style={{
          marginLeft: 6, fontSize: 12, fontWeight: 600, color: 'var(--text-dim)',
          fontFamily: 'var(--font-mono)',
        }}>+{extra}</span>
      )}
    </div>
  );
}

// ---- pills ----
function StatusPill({ status }) {
  const s = IDEA_STATUS[status];
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 6,
      padding: '3px 10px 3px 8px', borderRadius: 999, fontSize: 12, fontWeight: 650,
      fontFamily: 'var(--font-ui)',
      color: `oklch(0.4 0.13 ${s.hue})`, background: `oklch(0.95 0.05 ${s.hue})`,
    }}>
      <span style={{ width: 6, height: 6, borderRadius: '50%', background: `oklch(0.55 0.16 ${s.hue})` }} />
      {s.label}
    </span>
  );
}

function PriorityTag({ level }) {
  const p = PRIORITIES[level];
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 5,
      padding: '2px 8px', borderRadius: 6, fontSize: 11.5, fontWeight: 650,
      fontFamily: 'var(--font-mono)', textTransform: 'uppercase', letterSpacing: '.03em',
      color: `oklch(0.45 0.15 ${p.hue})`, background: `oklch(0.95 0.045 ${p.hue})`,
    }}>
      <Icon name="flag" size={11} stroke={2.4} />{p.label}
    </span>
  );
}

// ---- progress bar ----
function Progress({ value, total, hue = 'var(--primary-h)' }) {
  const pct = total ? Math.round((value / total) * 100) : 0;
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 10, width: '100%' }}>
      <div style={{ flex: 1, height: 6, borderRadius: 99, background: 'var(--track)', overflow: 'hidden' }}>
        <div style={{
          width: pct + '%', height: '100%', borderRadius: 99,
          background: 'var(--primary)', transition: 'width .5s cubic-bezier(.2,.8,.2,1)',
        }} />
      </div>
      <span style={{ fontSize: 12, fontWeight: 600, color: 'var(--text-dim)', fontFamily: 'var(--font-mono)', minWidth: 34, textAlign: 'right' }}>{pct}%</span>
    </div>
  );
}

Object.assign(window, { Icon, Avatar, AvatarStack, StatusPill, PriorityTag, Progress, userById, fmtMoney });

function fmtMoney(n, opts = {}) {
  const dp = opts.dp ?? 0;
  const s = (n || 0).toLocaleString('en-US', { minimumFractionDigits: dp, maximumFractionDigits: dp });
  return opts.bare ? s : 'Rs\u202f' + s;
}

const _MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
function fmtDate(iso) {
  if (!iso) return null;
  const [y, m, d] = iso.split('-').map(Number);
  if (!y || !m || !d) return null;
  return `${d} ${_MONTHS[m - 1]} ${y}`;
}
Object.assign(window, { fmtDate });
