// =============================================================
// Icons + utility components
// =============================================================

const Icon = {
  search: (p) => (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <circle cx="7" cy="7" r="5"></circle>
      <path d="M11 11l3 3"></path>
    </svg>
  ),
  chevDown: (p) => (
    <svg width="10" height="10" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M4 6l4 4 4-4"></path>
    </svg>
  ),
  chevLeft: (p) => (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M10 4l-4 4 4 4"></path>
    </svg>
  ),
  chevRight: (p) => (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M6 4l4 4-4 4"></path>
    </svg>
  ),
  close: (p) => (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M4 4l8 8M12 4l-8 8"></path>
    </svg>
  ),
  refresh: (p) => (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M2 3v4h4"></path>
      <path d="M13 9a5 5 0 1 1-1.5-3.5L14 7"></path>
    </svg>
  ),
  settings: (p) => (
    <svg width="15" height="15" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <circle cx="8" cy="8" r="1.6"></circle>
      <path d="M13 8a5 5 0 0 0-.1-1l1.1-.8-1-1.8-1.3.4a5 5 0 0 0-1.7-1l-.2-1.4h-2l-.2 1.4a5 5 0 0 0-1.7 1l-1.3-.4-1 1.8 1.1.8a5 5 0 0 0 0 2L2.6 10l1 1.8 1.3-.4a5 5 0 0 0 1.7 1l.2 1.4h2l.2-1.4a5 5 0 0 0 1.7-1l1.3.4 1-1.8L13 9a5 5 0 0 0 .1-1z"></path>
    </svg>
  ),
  external: (p) => (
    <svg width="12" height="12" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M6 3H3v10h10V10"></path>
      <path d="M9 3h4v4"></path>
      <path d="M13 3l-5 5"></path>
    </svg>
  ),
  letter: (p) => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="2" y="4" width="12" height="9" rx="1"></rect>
      <path d="M2 5l6 4 6-4"></path>
    </svg>
  ),
  check: (p) => (
    <svg width="12" height="12" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 8l3 3 7-7"></path>
    </svg>
  ),
  archive: (p) => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="2" y="3" width="12" height="3" rx="0.5"></rect>
      <path d="M3 6v6a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V6"></path>
      <path d="M6.5 9h3"></path>
    </svg>
  ),
  filter: (p) => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M2 3h12l-4.5 6v4l-3 1V9z"></path>
    </svg>
  ),
  pin: (p) => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M8 1v6"></path>
      <path d="M4 7h8v3l-4 4-4-4z"></path>
    </svg>
  ),
  briefcase: (p) => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="2" y="5" width="12" height="9" rx="1"></rect>
      <path d="M6 5V3h4v2"></path>
    </svg>
  ),
  location: (p) => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M8 14s-5-4-5-8a5 5 0 0 1 10 0c0 4-5 8-5 8z"></path>
      <circle cx="8" cy="6" r="1.6"></circle>
    </svg>
  ),
  cash: (p) => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <rect x="2" y="4" width="12" height="8" rx="1"></rect>
      <circle cx="8" cy="8" r="1.5"></circle>
    </svg>
  ),
  trash: (p) => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 4h10"></path>
      <path d="M5 4V3a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v1"></path>
      <path d="M4 4l1 9a1 1 0 0 0 1 1h4a1 1 0 0 0 1-1l1-9"></path>
    </svg>
  ),
  more: (p) => (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor" {...p}>
      <circle cx="3.5" cy="8" r="1.2"></circle>
      <circle cx="8" cy="8" r="1.2"></circle>
      <circle cx="12.5" cy="8" r="1.2"></circle>
    </svg>
  ),
  warning: (p) => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M8 2L1.5 13h13z"></path>
      <path d="M8 7v3"></path>
      <circle cx="8" cy="12" r="0.5" fill="currentColor"></circle>
    </svg>
  ),
  arrow: (p) => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M3 8h10"></path>
      <path d="M9 4l4 4-4 4"></path>
    </svg>
  ),
  star: (p) => (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M8 2l1.8 3.7 4 .6-2.9 2.9.7 4-3.6-1.9-3.6 1.9.7-4L2.2 6.3l4-.6z"></path>
    </svg>
  ),
  starFilled: (p) => (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor" {...p}>
      <path d="M8 2l1.8 3.7 4 .6-2.9 2.9.7 4-3.6-1.9-3.6 1.9.7-4L2.2 6.3l4-.6z"></path>
    </svg>
  ),
};

// =============================================================
// Score visualization — three styles, switched by tweak
// =============================================================

function ScoreViz({ cv, match, variant = 'dots' }) {
  if (variant === 'quadrant') {
    // CV on X, Match on Y. (Lower-left = bad, upper-right = ideal)
    const x = Math.max(0, Math.min(100, cv));
    const y = Math.max(0, Math.min(100, match));
    return (
      <div className="quadrant-viz" title={`CV ${cv} · Match ${match}`}>
        <div className="axis-h"></div>
        <div className="axis-v"></div>
        <div className="marker" style={{ left: `${x}%`, bottom: `${y}%` }}></div>
      </div>
    );
  }
  if (variant === 'bars') {
    // Hidden — we always render the text bars next to viz.
    return null;
  }
  // Default: 5x5 dot matrix. Rows = match score (top = high), cols = CV fitness (left = low)
  const cvCol = Math.min(4, Math.floor(cv / 20));
  const matchRow = 4 - Math.min(4, Math.floor(match / 20));
  return (
    <div className="dot-matrix" title={`CV ${cv} · Match ${match}`}>
      {Array.from({ length: 25 }).map((_, i) => {
        const r = Math.floor(i / 5);
        const c = i % 5;
        let cls = 'dot';
        if (r === matchRow && c === cvCol) cls += ' active';
        else if (c === cvCol) cls += ' in-cv';
        else if (r === matchRow) cls += ' in-match';
        return <div key={i} className={cls}></div>;
      })}
    </div>
  );
}

// =============================================================
// Filter popover (generic)
// =============================================================

function Popover({ open, onClose, children, anchor }) {
  if (!open) return null;
  React.useEffect(() => {
    const onDoc = (e) => {
      if (anchor.current && !anchor.current.contains(e.target)) onClose();
    };
    setTimeout(() => document.addEventListener('mousedown', onDoc), 0);
    return () => document.removeEventListener('mousedown', onDoc);
  }, [onClose, anchor]);
  return <div className="popover" onClick={(e) => e.stopPropagation()}>{children}</div>;
}

// =============================================================
// Dual range slider
// =============================================================
function DualRange({ min, max, value, step = 1, onChange }) {
  const [lo, hi] = value;
  const set = (newLo, newHi) => {
    if (newLo > newHi - step) return;
    onChange([newLo, newHi]);
  };
  const pctLo = ((lo - min) / (max - min)) * 100;
  const pctHi = ((hi - min) / (max - min)) * 100;
  return (
    <div className="dual-range">
      <div className="track">
        <div className="fill" style={{ left: `${pctLo}%`, right: `${100 - pctHi}%` }}></div>
      </div>
      <input type="range" min={min} max={max} step={step} value={lo}
        onChange={(e) => set(+e.target.value, hi)} />
      <input type="range" min={min} max={max} step={step} value={hi}
        onChange={(e) => set(lo, +e.target.value)} />
    </div>
  );
}

// =============================================================
// Filter Chip (with popover)
// =============================================================
function FilterChip({ label, active, value, onClear, children, popoverWidth }) {
  const [open, setOpen] = React.useState(false);
  const ref = React.useRef(null);
  return (
    <div ref={ref} style={{ position: 'relative' }}>
      <button className={`filter-chip${active ? ' active' : ''}`}
        onClick={() => setOpen((v) => !v)}>
        <span>{label}</span>
        {active && value ? <span className="value">{value}</span> : null}
        {active ? (
          <span className="x" onClick={(e) => { e.stopPropagation(); onClear(); }}>
            <Icon.close />
          </span>
        ) : <Icon.chevDown />}
      </button>
      <Popover open={open} onClose={() => setOpen(false)} anchor={ref}>
        <div style={{ minWidth: popoverWidth || 220 }}>
          {typeof children === 'function' ? children(() => setOpen(false)) : children}
        </div>
      </Popover>
    </div>
  );
}

Object.assign(window, { Icon, ScoreViz, Popover, DualRange, FilterChip });
