/* perla dashboard · Home · shared primitives + icons */
const { useState, useEffect, useMemo, useRef } = React;

/* ── Icons (lucide-compatible, stroke 1.5) ───────────────────────── */
const Icon = ({ size = 22, children, stroke, ...rest }) => (
  <svg viewBox="0 0 24 24" width={size} height={size} fill="none" stroke={stroke || 'currentColor'} strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" {...rest}>{children}</svg>
);

const Icons = {
  home: () => <Icon><path d="M3 9l9-7 9 7v11a2 2 0 01-2 2H5a2 2 0 01-2-2z"/><polyline points="9 22 9 12 15 12 15 22"/></Icon>,
  calendar: () => <Icon><rect x="3" y="4" width="18" height="18" rx="3"/><path d="M16 2v4M8 2v4M3 10h18"/></Icon>,
  users: () => <Icon><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 00-3-3.87M16 3.13a4 4 0 010 7.75"/></Icon>,
  message: () => <Icon><path d="M21 11.5a8.38 8.38 0 01-.9 3.8 8.5 8.5 0 01-7.6 4.7 8.38 8.38 0 01-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 01-.9-3.8 8.5 8.5 0 014.7-7.6 8.38 8.38 0 013.8-.9h.5a8.48 8.48 0 018 8v.5z"/></Icon>,
  settings: () => <Icon><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 01-2.83 2.83l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 01-4 0v-.09A1.65 1.65 0 009 19.4a1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 01-2.83-2.83l.06-.06A1.65 1.65 0 004.68 15a1.65 1.65 0 00-1.51-1H3a2 2 0 010-4h.09A1.65 1.65 0 004.6 9a1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 012.83-2.83l.06.06A1.65 1.65 0 009 4.68 1.65 1.65 0 0010 3.17V3a2 2 0 014 0v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 012.83 2.83l-.06.06a1.65 1.65 0 00-.33 1.82V10a1.65 1.65 0 001.51 1H21a2 2 0 010 4h-.09a1.65 1.65 0 00-1.51 1z"/></Icon>,
  plus: ({ size = 20 }) => <Icon size={size}><path d="M12 5v14M5 12h14"/></Icon>,
  phone: ({ size = 14 }) => <Icon size={size}><path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07A19.5 19.5 0 013.07 9.81 19.79 19.79 0 01.1 1.18 2 2 0 012.1 0h3a2 2 0 012 1.72c.128.96.36 1.9.7 2.81a2 2 0 01-.45 2.11L6.11 7.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45c.91.34 1.85.572 2.81.7A2 2 0 0122 16.92z"/></Icon>,
  search: ({ size = 16 }) => <Icon size={size}><circle cx="11" cy="11" r="7"/><path d="M21 21l-4.3-4.3"/></Icon>,
  chev: ({ size = 14 }) => <Icon size={size}><path d="M9 18l6-6-6-6"/></Icon>,
  chevDown: ({ size = 14 }) => <Icon size={size}><path d="M6 9l6 6 6-6"/></Icon>,
  arrowUp: ({ size = 12 }) => <Icon size={size}><path d="M12 19V5M5 12l7-7 7 7"/></Icon>,
  arrowDown: ({ size = 12 }) => <Icon size={size}><path d="M12 5v14M5 12l7 7 7-7"/></Icon>,
  back: ({ size = 18 }) => <Icon size={size}><path d="M15 18l-6-6 6-6"/></Icon>,
  x: ({ size = 14 }) => <Icon size={size}><path d="M18 6L6 18M6 6l12 12"/></Icon>,
  clock: ({ size = 15 }) => <Icon size={size}><circle cx="12" cy="12" r="10"/><path d="M12 6v6l4 2"/></Icon>,
  check: ({ size = 15 }) => <Icon size={size}><path d="M20 6L9 17l-5-5"/></Icon>,
  checkCircle: ({ size = 15 }) => <Icon size={size}><path d="M22 11.08V12a10 10 0 11-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></Icon>,
  user: ({ size = 15 }) => <Icon size={size}><path d="M20 21v-2a4 4 0 00-4-4H8a4 4 0 00-4 4v2"/><circle cx="12" cy="7" r="4"/></Icon>,
  sparkle: ({ size = 15 }) => <Icon size={size}><path d="M12 3l2 5 5 2-5 2-2 5-2-5-5-2 5-2 2-5z"/></Icon>,
  alert: ({ size = 16 }) => <Icon size={size}><path d="M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></Icon>,
  trend: ({ size = 14 }) => <Icon size={size}><polyline points="23 6 13.5 15.5 8.5 10.5 1 18"/><polyline points="17 6 23 6 23 12"/></Icon>,
  scissors: ({ size = 14 }) => <Icon size={size}><circle cx="6" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><line x1="20" y1="4" x2="8.12" y2="15.88"/><line x1="14.47" y1="14.48" x2="20" y2="20"/><line x1="8.12" y1="8.12" x2="12" y2="12"/></Icon>,
};

/* ── Color palette for avatars (only from brand palette) ────────── */
const AVATAR_COLORS = ['#2D7A7A', '#A88760', '#5B4B7A', '#4F7C80', '#4C5A8C', '#6B4F99'];
const avatarBg = (name) => AVATAR_COLORS[name.charCodeAt(0) % AVATAR_COLORS.length];
const initials = (name) => name.split(' ').slice(0,2).map(s => s[0]).join('').toUpperCase();

/* ── Brand ──────────────────────────────────────────────────────── */
const BrandMark = () => (
  <div className="brand">
    <span className="brand-wm">perla</span>
    <span className="brand-dot"></span>
  </div>
);

/* ── Status bar (purely visual) ─────────────────────────────────── */
const StatusBar = () => {
  const [now, setNow] = useState(() => {
    const d = new Date();
    return `${String(d.getHours()).padStart(2,'0')}:${String(d.getMinutes()).padStart(2,'0')}`;
  });
  useEffect(() => {
    const id = setInterval(() => {
      const d = new Date();
      setNow(`${String(d.getHours()).padStart(2,'0')}:${String(d.getMinutes()).padStart(2,'0')}`);
    }, 30000);
    return () => clearInterval(id);
  }, []);
  return (
    <div className="status-bar">
      <span>{now}</span>
      <div style={{display:'flex',gap:5,alignItems:'center',color:'#1A2A2E'}}>
        <svg width="16" height="10" viewBox="0 0 16 10" fill="currentColor"><rect x="0" y="7" width="2" height="3" rx="0.5"/><rect x="4" y="5" width="2" height="5" rx="0.5"/><rect x="8" y="3" width="2" height="7" rx="0.5"/><rect x="12" y="1" width="2" height="9" rx="0.5"/></svg>
        <svg width="14" height="10" viewBox="0 0 14 10" fill="currentColor"><path d="M7 1c3.3 0 6 2.2 6 4l-6 5L1 5c0-1.8 2.7-4 6-4z" opacity="0.4"/><path d="M7 4c1.7 0 3 1 3 2l-3 3-3-3c0-1 1.3-2 3-2z"/></svg>
        <span style={{display:'inline-block',width:22,height:10,border:'1px solid currentColor',borderRadius:3,position:'relative',padding:1}}>
          <span style={{display:'block',width:'78%',height:'100%',background:'currentColor',borderRadius:1}}></span>
        </span>
      </div>
    </div>
  );
};

/* ── Top bar with global "Nuevo turno" ──────────────────────────── */
const TopBar = ({ onNewTurn }) => (
  <div className="top-bar">
    <BrandMark/>
    <button className="new-turn-btn" onClick={onNewTurn} aria-label="Nuevo turno">
      <Icons.plus size={16}/>
      <span>Nuevo turno</span>
    </button>
  </div>
);

/* ── Bottom nav — label only on active ──────────────────────────── */
const NAV = [
  { id:'home', icon:'home', label:'Home', href:'index.html' },
  { id:'agenda', icon:'calendar', label:'Agenda', href:'Agenda.html' },
  { id:'clientes', icon:'users', label:'Clientes', href:'Clientes.html' },
  { id:'mensajes', icon:'message', label:'Mensajes', href:'Mensajes.html' },
  { id:'config', icon:'settings', label:'Configuración', href:'Configuracion.html' },
];
const BottomNav = ({ active, onSelect }) => (
  <nav className="bottom-nav" aria-label="Navegación principal">
    {NAV.map(n => {
      const Ic = Icons[n.icon];
      const isActive = active === n.id;
      return (
        <a key={n.id} href={n.href} className={'nav-btn' + (isActive ? ' active' : '')} aria-label={n.label} style={{textDecoration:'none'}}>
          <Ic/>
          {isActive && <span className="nav-label">{n.label}</span>}
        </a>
      );
    })}
  </nav>
);

/* ── Badge ──────────────────────────────────────────────────────── */
const statusClass = { confirmed: 'conf', pending: 'pend', cancelled: 'canc' };
const statusLabel = { confirmed: 'Confirmada', pending: 'Pendiente', cancelled: 'Cancelada' };
const Badge = ({ status }) => <span className={'badge ' + statusClass[status]}>{statusLabel[status]}</span>;

Object.assign(window, { Icons, avatarBg, initials, BrandMark, StatusBar, TopBar, BottomNav, NAV, Badge, statusClass, statusLabel });
