// ui.jsx — shared UI primitives for BHCAC Attendance
// Buttons, cards, icons, avatars, eyebrows — all in BHCAC visual language

// ─── Lucide-style icon set (2px stroke, rounded) ───
function Icon({ name, size = 20, color = 'currentColor', strokeWidth = 2, style = {} }) {
  const paths = {
    'check': <polyline points="20 6 9 17 4 12"/>,
    'x': <><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></>,
    'chevron-right': <polyline points="9 6 15 12 9 18"/>,
    'chevron-left': <polyline points="15 6 9 12 15 18"/>,
    'chevron-down': <polyline points="6 9 12 15 18 9"/>,
    '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="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></>,
    'home': <><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2h-4a2 2 0 0 1-2-2v-6h-2v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/></>,
    'users': <><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></>,
    'user': <><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></>,
    'calendar': <><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></>,
    'clock': <><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></>,
    'map-pin': <><path d="M20 10c0 7-8 13-8 13s-8-6-8-13a8 8 0 0 1 16 0Z"/><circle cx="12" cy="10" r="3"/></>,
    'church': <><path d="M10 9h4"/><path d="M12 7v5"/><path d="M14 22v-4a2 2 0 0 0-4 0v4"/><path d="M18 22V5.618a1 1 0 0 0-.553-.894l-4.553-2.277a2 2 0 0 0-1.788 0L6.553 4.724A1 1 0 0 0 6 5.618V22"/><path d="m18 7 3.447 1.724a1 1 0 0 1 .553.894V20a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V9.618a1 1 0 0 1 .553-.894L6 7"/></>,
    'qr': <><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><line x1="14" y1="14" x2="14" y2="17"/><line x1="17" y1="14" x2="17" y2="14"/><line x1="14" y1="17" x2="17" y2="17"/><line x1="20" y1="14" x2="20" y2="14"/><line x1="20" y1="17" x2="20" y2="20"/><line x1="17" y1="20" x2="17" y2="20"/><line x1="14" y1="20" x2="14" y2="20"/></>,
    'phone': <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"/>,
    'mail': <><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></>,
    'menu': <><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="18" x2="21" y2="18"/></>,
    'settings': <><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 1 1-4 0v-.09a1.65 1.65 0 0 0-1-1.51 1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 1 1 0-4h.09a1.65 1.65 0 0 0 1.51-1 1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33h0a1.65 1.65 0 0 0 1-1.51V3a2 2 0 1 1 4 0v.09a1.65 1.65 0 0 0 1 1.51h0a1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82v0a1.65 1.65 0 0 0 1.51 1H21a2 2 0 1 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></>,
    'bar-chart': <><line x1="12" y1="20" x2="12" y2="10"/><line x1="18" y1="20" x2="18" y2="4"/><line x1="6" y1="20" x2="6" y2="16"/></>,
    'download': <><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></>,
    'sparkles': <><path d="M12 3l1.5 4.5L18 9l-4.5 1.5L12 15l-1.5-4.5L6 9l4.5-1.5L12 3z"/></>,
    'gift': <><polyline points="20 12 20 22 4 22 4 12"/><rect x="2" y="7" width="20" height="5"/><line x1="12" y1="22" x2="12" y2="7"/><path d="M12 7H7.5a2.5 2.5 0 0 1 0-5C11 2 12 7 12 7z"/><path d="M12 7h4.5a2.5 2.5 0 0 0 0-5C13 2 12 7 12 7z"/></>,
    'sun': <><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/></>,
    'log-out': <><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></>,
    'edit': <><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></>,
    'arrow-right': <><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></>,
    'arrow-left': <><line x1="19" y1="12" x2="5" y2="12"/><polyline points="12 19 5 12 12 5"/></>,
    'apple': <path d="M16.365 1.43c0 1.14-.493 2.27-1.177 3.08-.744.9-1.99 1.57-2.987 1.57-.12 0-.23-.02-.3-.03-.01-.06-.04-.22-.04-.39 0-1.15.572-2.27 1.206-2.98.804-.94 2.142-1.64 3.248-1.68.03.13.05.28.05.43zm4.565 15.71c-.03.07-.463 1.58-1.518 3.12-.945 1.34-1.94 2.71-3.43 2.71-1.517 0-1.9-.88-3.63-.88-1.698 0-2.302.91-3.67.91-1.377 0-2.332-1.26-3.428-2.8-1.287-1.82-2.323-4.63-2.323-7.28 0-4.28 2.797-6.55 5.552-6.55 1.448 0 2.675.95 3.6.95.865 0 2.222-1 3.902-1 .613 0 2.886.06 4.374 2.19-.13.09-2.383 1.37-2.383 4.19 0 3.26 2.854 4.42 2.955 4.44z"/>,
    'google': <><path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z" fill="#4285F4" stroke="none"/><path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.99.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" fill="#34A853" stroke="none"/><path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" fill="#FBBC05" stroke="none"/><path d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z" fill="#EA4335" stroke="none"/></>,
    'sticker': <><path d="M15.5 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V8.5L15.5 3z"/><path d="M15 3v6h6"/></>,
    'circle-check': <><circle cx="12" cy="12" r="10"/><polyline points="9 12 11 14 15 10"/></>,
    'bell': <><path d="M18 8a6 6 0 0 0-12 0c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 0 1-3.46 0"/></>,
    'star': <polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/>,
    'heart': <path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"/>,
    'key': <><circle cx="7.5" cy="15.5" r="5.5"/><path d="m21 2-9.6 9.6"/><path d="m15.5 7.5 3 3L22 7l-3-3"/></>,
    'fingerprint': <><path d="M12 11v2a14 14 0 0 0 2.5 8"/><path d="M5 7c0-3.3 2.7-6 6-6s6 2.7 6 6"/><path d="M5 14c0-1.7 1.3-3 3-3s3 1.3 3 3"/><path d="M11 17a13 13 0 0 1-7 6"/><path d="M17 22a18 18 0 0 0-1-12"/></>,
  };
  const p = paths[name];
  if (!p) return null;
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round" style={{ flexShrink: 0, ...style }}>
      {p}
    </svg>
  );
}

// ─── Eyebrow ───
function Eyebrow({ children, color = 'var(--brand-coral)', style = {}, className }) {
  return <div className={className} style={{ fontSize: 11, fontWeight: 700, textTransform: 'uppercase', letterSpacing: '0.12em', color, ...style }}>{children}</div>;
}

// ─── Bilingual text helper ───
function Bi({ en, zh, primary = 'en', enStyle = {}, zhStyle = {}, gap = 2, style = {} }) {
  if (primary === 'en') {
    return (
      <div style={{ display: 'flex', flexDirection: 'column', gap, ...style }}>
        <span style={{ ...enStyle }}>{en}</span>
        <span style={{ fontFamily: "'Noto Sans TC', 'PingFang TC', sans-serif", color: 'var(--fg-3)', fontSize: '0.86em', ...zhStyle }}>{zh}</span>
      </div>
    );
  }
  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap, ...style }}>
      <span style={{ fontFamily: "'Noto Sans TC', 'PingFang TC', sans-serif", ...zhStyle }}>{zh}</span>
      <span style={{ color: 'var(--fg-3)', fontSize: '0.86em', ...enStyle }}>{en}</span>
    </div>
  );
}

// ─── Avatar ───
function Avatar({ member, size = 40, ring = false, ringColor = 'var(--brand-coral)' }) {
  const initials = member.en.split(' ').map(s => s[0]).slice(0, 2).join('');
  return (
    <div style={{
      width: size, height: size, borderRadius: '50%',
      background: member.avatar || 'var(--navy-100)',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      color: 'var(--brand-navy)', fontWeight: 700, fontSize: size * 0.36,
      fontFamily: 'Inter, sans-serif',
      flexShrink: 0,
      boxShadow: ring ? `0 0 0 3px var(--bg-1), 0 0 0 ${size > 50 ? 5 : 4}px ${ringColor}` : 'none',
      letterSpacing: '-0.01em',
    }}>{initials}</div>
  );
}

// ─── Button ───
function Btn({ children, variant = 'primary', size = 'md', icon, trailingIcon, onClick, type, style = {}, full = false, disabled = false }) {
  const base = {
    // `flex` (not inline-flex) when full so the button's width: 100% is
    // actually honoured on iOS Safari/Brave — inline-flex lets the content
    // dictate min-width and overflows the parent on long bilingual labels.
    display: full ? 'flex' : 'inline-flex',
    alignItems: 'center', justifyContent: 'center', gap: 8,
    fontFamily: 'Inter, sans-serif', fontWeight: 600,
    border: 'none', cursor: disabled ? 'not-allowed' : 'pointer',
    transition: 'all 200ms var(--ease-out)',
    borderRadius: 9999,
    width: full ? '100%' : 'auto',
    maxWidth: '100%',
    minWidth: 0,
    boxSizing: 'border-box',
    opacity: disabled ? 0.5 : 1,
  };
  const sizes = {
    sm: { padding: '8px 14px', fontSize: 13 },
    md: { padding: '12px 18px', fontSize: 14 },
    lg: { padding: '14px 20px', fontSize: 15 },
  };
  const variants = {
    primary: { background: 'var(--brand-coral)', color: '#fff', boxShadow: '0 4px 12px -2px rgba(244,63,94,0.4)' },
    secondary: { background: 'var(--brand-navy)', color: '#fff' },
    teal: { background: 'var(--brand-teal)', color: '#fff' },
    ghost: { background: 'transparent', color: 'var(--brand-navy)' },
    outline: { background: 'transparent', color: 'var(--brand-navy)', boxShadow: 'inset 0 0 0 1.5px var(--border-strong)' },
    onDark: { background: 'rgba(255,255,255,0.12)', color: '#fff', backdropFilter: 'blur(8px)', boxShadow: 'inset 0 0 0 1px rgba(255,255,255,0.25)' },
  };
  return (
    <button type={type} onClick={disabled ? undefined : onClick} style={{ ...base, ...sizes[size], ...variants[variant], ...style }}>
      {icon && <Icon name={icon} size={size === 'lg' ? 18 : 16} />}
      <span style={{ minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'normal', textAlign: 'center', lineHeight: 1.25 }}>{children}</span>
      {trailingIcon && <Icon name={trailingIcon} size={size === 'lg' ? 18 : 16} />}
    </button>
  );
}

// ─── Card ───
function Card({ children, accent, style = {}, padding = 20, onClick, interactive = false }) {
  return (
    <div onClick={onClick}
      style={{
        background: 'var(--bg-1)',
        borderRadius: 'var(--radius-xl)',
        boxShadow: 'var(--shadow-md)',
        border: '1px solid var(--border-subtle)',
        borderTop: accent ? `4px solid ${accent}` : '1px solid var(--border-subtle)',
        padding,
        cursor: interactive || onClick ? 'pointer' : 'default',
        transition: 'transform 200ms var(--ease-out), box-shadow 200ms var(--ease-out)',
        ...style,
      }}
      onMouseOver={interactive ? (e) => { e.currentTarget.style.transform = 'translateY(-2px)'; e.currentTarget.style.boxShadow = 'var(--shadow-lg)'; } : undefined}
      onMouseOut={interactive ? (e) => { e.currentTarget.style.transform = 'none'; e.currentTarget.style.boxShadow = 'var(--shadow-md)'; } : undefined}
    >{children}</div>
  );
}

// ─── Pill (small status tag) ───
function Pill({ children, tone = 'navy', icon, style = {} }) {
  const tones = {
    navy:   { bg: 'rgba(15,23,42,0.06)', fg: 'var(--brand-navy)' },
    coral:  { bg: 'rgba(244,63,94,0.1)', fg: 'var(--coral-700)' },
    teal:   { bg: 'rgba(13,148,136,0.1)', fg: 'var(--teal-700)' },
    warm:   { bg: 'var(--brand-warm)', fg: '#9a3412' },
    success:{ bg: 'rgba(13,148,136,0.12)', fg: 'var(--teal-700)' },
    paper:  { bg: '#fef3c7', fg: '#92400e' },
    online: { bg: 'rgba(13,148,136,0.1)', fg: 'var(--teal-700)' },
    muted:  { bg: 'var(--brand-light)', fg: 'var(--fg-3)' },
  };
  const t = tones[tone] || tones.navy;
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 4,
      fontSize: 11, fontWeight: 700, letterSpacing: '0.04em',
      padding: '4px 10px', borderRadius: 9999,
      background: t.bg, color: t.fg,
      ...style,
    }}>
      {icon && <Icon name={icon} size={11} strokeWidth={2.5} />}
      {children}
    </span>
  );
}

Object.assign(window, { Icon, Eyebrow, Bi, Avatar, Btn, Card, Pill });
