// Shared UI atoms for Sugar Tracker

// traffic-light color based on pct of goal (added sugar)
function statusColor(pct, palette) {
  if (pct < 0.67) return palette.green;
  if (pct < 1.0)  return palette.amber;
  return palette.red;
}

function statusLabel(pct) {
  if (pct < 0.5)  return 'On track';
  if (pct < 0.85) return 'Watch it';
  if (pct < 1.0)  return 'Close to limit';
  return 'Over limit';
}

// Inline SVG icons — hairline, 1.5 stroke
const Icon = {
  search: (c = 'currentColor') => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round">
      <circle cx="11" cy="11" r="7"/><path d="M20 20l-4-4"/>
    </svg>
  ),
  plus: (c = 'currentColor') => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="2" strokeLinecap="round">
      <path d="M12 5v14M5 12h14"/>
    </svg>
  ),
  close: (c = 'currentColor') => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="2" strokeLinecap="round">
      <path d="M6 6l12 12M18 6L6 18"/>
    </svg>
  ),
  chevron: (c = 'currentColor') => (
    <svg width="8" height="14" viewBox="0 0 8 14" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M1 1l6 6-6 6"/>
    </svg>
  ),
  back: (c = 'currentColor') => (
    <svg width="11" height="18" viewBox="0 0 11 18" fill="none" stroke={c} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M9 1L2 9l7 8"/>
    </svg>
  ),
  home: (c = 'currentColor', fill = false) => (
    <svg width="22" height="22" viewBox="0 0 24 24" fill={fill ? c : 'none'} stroke={c} strokeWidth="1.8" strokeLinejoin="round">
      <path d="M3 11l9-7 9 7v9a1 1 0 01-1 1h-5v-6h-6v6H4a1 1 0 01-1-1v-9z"/>
    </svg>
  ),
  chart: (c = 'currentColor', fill = false) => (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round">
      <path d="M4 20V10M10 20V4M16 20v-7M22 20H2"/>
    </svg>
  ),
  logIcon: (c = 'currentColor') => (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M6 3h12a1 1 0 011 1v16l-4-2-3 2-3-2-4 2V4a1 1 0 011-1z"/>
      <path d="M9 9h6M9 13h4"/>
    </svg>
  ),
  clock: (c = 'currentColor') => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round">
      <circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/>
    </svg>
  ),
  trash: (c = 'currentColor') => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M4 7h16M10 11v6M14 11v6M6 7l1 13a2 2 0 002 2h6a2 2 0 002-2l1-13M9 7V4a1 1 0 011-1h4a1 1 0 011 1v3"/>
    </svg>
  ),
  info: (c = 'currentColor') => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round">
      <circle cx="12" cy="12" r="9"/><path d="M12 8h.01M11 12h1v5h1"/>
    </svg>
  ),
};

// Food category icon (tiny colored square with letter)
function FoodBadge({ cat, palette }) {
  const map = {
    Fruit:      { bg: '#FFF1E8', fg: '#C2410C', letter: 'F' },
    Dairy:      { bg: '#EEF4FB', fg: '#1E4E8C', letter: 'D' },
    Drinks:     { bg: '#F3EEF8', fg: '#6B21A8', letter: 'B' },
    Snacks:     { bg: '#FEF3C7', fg: '#92400E', letter: 'S' },
    Breakfast:  { bg: '#FEF9C3', fg: '#854D0E', letter: 'B' },
    Condiments: { bg: '#FEE2E2', fg: '#991B1B', letter: 'C' },
    Meals:      { bg: '#DCFCE7', fg: '#166534', letter: 'M' },
  };
  const c = map[cat] || map.Fruit;
  return (
    <div style={{
      width: 36, height: 36, borderRadius: 10,
      background: c.bg, color: c.fg,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      fontFamily: 'var(--font-mono)', fontSize: 14, fontWeight: 600,
      flexShrink: 0,
    }}>{c.letter}</div>
  );
}

// Rounded-corner stacked bar used in ring and charts
function StatPill({ label, value, unit, accent, small }) {
  return (
    <div style={{
      flex: 1,
      display: 'flex', flexDirection: 'column', gap: 2,
      padding: small ? '8px 12px' : '10px 14px',
      borderRadius: 12,
      background: 'var(--surface-2)',
    }}>
      <div style={{
        display: 'flex', alignItems: 'center', gap: 6,
        fontSize: 11, fontWeight: 500, letterSpacing: 0.3,
        color: 'var(--text-2)', textTransform: 'uppercase',
      }}>
        {accent && <span style={{
          width: 8, height: 8, borderRadius: 2, background: accent, display: 'inline-block',
        }}/>}
        {label}
      </div>
      <div style={{ display: 'flex', alignItems: 'baseline', gap: 3 }}>
        <span style={{
          fontFamily: 'var(--font-mono)', fontSize: small ? 18 : 22,
          fontWeight: 600, color: 'var(--text-1)', letterSpacing: -0.5,
        }}>{value}</span>
        <span style={{ fontSize: 11, color: 'var(--text-2)' }}>{unit}</span>
      </div>
    </div>
  );
}

window.statusColor = statusColor;
window.statusLabel = statusLabel;
window.Icon = Icon;
window.FoodBadge = FoodBadge;
window.StatPill = StatPill;
