/* Shared atoms / icons for MedFlow */

const Logo = ({ tone = 'dark', size = 'md' }) => {
  const big = size === 'lg';
  const dark = tone === 'dark';
  return (
    <div className="flex items-center gap-2.5">
      <div className={`${big ? 'h-9 w-9' : 'h-7 w-7'} rounded-[9px] bg-midnight-800 grid place-items-center relative overflow-hidden`}>
        <div className="absolute inset-0 bg-gradient-to-br from-midnight-700 to-midnight-900" />
        <svg viewBox="0 0 24 24" className={`${big ? 'h-5 w-5' : 'h-4 w-4'} relative`} fill="none">
          <path d="M5 12h3l2-5 4 10 2-5h3" stroke="#7FD1B0" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
        </svg>
      </div>
      <div className={`${big ? 'text-[20px]' : 'text-[16px]'} font-semibold tracking-tight ${dark ? 'text-midnight-900' : 'text-white'}`}>
        Med<span className="text-mint-500">Flow</span>
      </div>
    </div>
  );
};

const I = {
  search: (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><circle cx="11" cy="11" r="7" stroke="currentColor" strokeWidth="1.6"/><path d="m20 20-3.5-3.5" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/></svg>,
  chevL: (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="m14 6-6 6 6 6" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  chevR: (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="m10 6 6 6-6 6" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  chevD: (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="m6 9 6 6 6-6" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  check: (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="m5 12 5 5 9-11" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  plus:  (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="M12 5v14M5 12h14" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/></svg>,
  bell:  (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="M6 9a6 6 0 0 1 12 0c0 5 2 6 2 7H4c0-1 2-2 2-7Z" stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/><path d="M10 19a2 2 0 0 0 4 0" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/></svg>,
  clock: (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="9" stroke="currentColor" strokeWidth="1.6"/><path d="M12 7v5l3 2" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/></svg>,
  pin:   (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="M12 21s-7-6.2-7-12a7 7 0 1 1 14 0c0 5.8-7 12-7 12Z" stroke="currentColor" strokeWidth="1.6"/><circle cx="12" cy="9" r="2.5" stroke="currentColor" strokeWidth="1.6"/></svg>,
  video: (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><rect x="3" y="6" width="13" height="12" rx="2" stroke="currentColor" strokeWidth="1.6"/><path d="m16 10 5-3v10l-5-3" stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/></svg>,
  user:  (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><circle cx="12" cy="8" r="4" stroke="currentColor" strokeWidth="1.6"/><path d="M4 20a8 8 0 0 1 16 0" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/></svg>,
  grid:  (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><rect x="4" y="4" width="7" height="7" rx="1.5" stroke="currentColor" strokeWidth="1.6"/><rect x="13" y="4" width="7" height="7" rx="1.5" stroke="currentColor" strokeWidth="1.6"/><rect x="4" y="13" width="7" height="7" rx="1.5" stroke="currentColor" strokeWidth="1.6"/><rect x="13" y="13" width="7" height="7" rx="1.5" stroke="currentColor" strokeWidth="1.6"/></svg>,
  cal:   (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><rect x="3" y="5" width="18" height="16" rx="2" stroke="currentColor" strokeWidth="1.6"/><path d="M3 9h18M8 3v4M16 3v4" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/></svg>,
  chart: (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="M4 20V8M10 20v-6M16 20V4M22 20H2" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/></svg>,
  users: (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><circle cx="9" cy="8" r="3.5" stroke="currentColor" strokeWidth="1.6"/><path d="M3 19a6 6 0 0 1 12 0" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/><path d="M16 11a3 3 0 1 0 0-6M21 19a5 5 0 0 0-4-4.9" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/></svg>,
  cog:   (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="3" stroke="currentColor" strokeWidth="1.6"/><path d="M19.4 15a1.6 1.6 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.6 1.6 0 0 0-1.8-.3 1.6 1.6 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.6 1.6 0 0 0-1-1.5 1.6 1.6 0 0 0-1.8.3l-.1.1A2 2 0 1 1 4.4 17l.1-.1a1.6 1.6 0 0 0 .3-1.8 1.6 1.6 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.6 1.6 0 0 0 1.5-1 1.6 1.6 0 0 0-.3-1.8l-.1-.1A2 2 0 1 1 7 4.4l.1.1a1.6 1.6 0 0 0 1.8.3H9a1.6 1.6 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.6 1.6 0 0 0 1 1.5 1.6 1.6 0 0 0 1.8-.3l.1-.1A2 2 0 1 1 19.6 7l-.1.1a1.6 1.6 0 0 0-.3 1.8V9a1.6 1.6 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.6 1.6 0 0 0-1.5 1Z" stroke="currentColor" strokeWidth="1.4"/></svg>,
  arrowUp: (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="M12 19V5M6 11l6-6 6 6" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  arrowDn: (p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="M12 5v14M6 13l6 6 6-6" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  dot:   (p) => <svg {...p} viewBox="0 0 24 24" fill="currentColor"><circle cx="12" cy="12" r="3"/></svg>,
  more:  (p) => <svg {...p} viewBox="0 0 24 24" fill="currentColor"><circle cx="6" cy="12" r="1.6"/><circle cx="12" cy="12" r="1.6"/><circle cx="18" cy="12" r="1.6"/></svg>,
  shield:(p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="M12 3 4 6v6c0 5 3.5 8 8 9 4.5-1 8-4 8-9V6l-8-3Z" stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/></svg>,
  download:(p)=> <svg {...p} viewBox="0 0 24 24" fill="none"><path d="M12 4v12m0 0-4-4m4 4 4-4M4 20h16" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  filter:(p) => <svg {...p} viewBox="0 0 24 24" fill="none"><path d="M4 6h16M7 12h10M10 18h4" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/></svg>,
};

const Badge = ({ tone='neutral', children }) => {
  const tones = {
    neutral: 'bg-slate-100 text-slate-700 ring-slate-200',
    mint:    'bg-mint-100 text-mint-700 ring-mint-200',
    midnight:'bg-midnight-50 text-midnight-700 ring-midnight-100',
    amber:   'bg-amber-50 text-amber-700 ring-amber-200',
    rose:    'bg-rose-50 text-rose-700 ring-rose-200',
  };
  return (
    <span className={`inline-flex items-center gap-1.5 rounded-full px-2 py-0.5 text-[11px] font-medium ring-1 ring-inset ${tones[tone]}`}>
      {children}
    </span>
  );
};

const StatusDot = ({ tone='mint' }) => {
  const c = { mint:'bg-mint-500', amber:'bg-amber-500', rose:'bg-rose-500', slate:'bg-slate-400', midnight:'bg-midnight-600' }[tone];
  return <span className={`inline-block h-1.5 w-1.5 rounded-full ${c}`} />;
};

const NavItem = ({ icon, label, active, badge, onClick }) => (
  <a onClick={onClick} className={`flex items-center gap-3 h-10 px-3 rounded-lg text-[13px] font-medium cursor-pointer transition
      ${active
        ? 'bg-white/10 text-white'
        : 'text-white/55 hover:bg-white/5 hover:text-white/80'}`}>
    <span className={`h-4 w-4 ${active ? 'text-mint-300' : ''}`}>{icon}</span>
    <span className="flex-1">{label}</span>
    {badge && <span className="text-[10.5px] tnum rounded-full bg-mint-400/20 text-mint-300 px-1.5 py-0.5">{badge}</span>}
  </a>
);

/* ── TopNav: barra superior reutilizable para vistas de empleado ── */
const TopNav = ({ user, currentView, onNavigate, onLogout }) => {
  if (!user) return null;
  const initials = user.full_name.split(' ').map(n => n[0]).slice(0,2).join('').toUpperCase();
  const firstName = user.full_name.split(' ')[0];

  const links = [
    { key: 'patient',       label: 'Agendar',       path: '/agendar' },
    { key: 'employee',      label: 'Mis citas',     path: '/mis-citas' },
    { key: 'beneficiaries', label: 'Beneficiarios', path: '/beneficiarios' },
  ];

  return (
    <header className="bg-white/80 backdrop-blur border-b border-line sticky top-0 z-40">
      <div className="max-w-[1200px] mx-auto px-10 h-16 flex items-center justify-between">
        <Logo tone="dark"/>
        <nav className="hidden md:flex items-center gap-7 text-[13.5px] text-slate-600">
          {links.map(l => (
            <a key={l.key}
              onClick={() => onNavigate && onNavigate(l.key)}
              className={`cursor-pointer transition ${currentView === l.key ? 'text-midnight-900 font-medium' : 'hover:text-midnight-900'}`}>
              {l.label}
            </a>
          ))}
          {(user.role === 'rrhh' || user.role === 'admin') && (
            <a onClick={() => onNavigate && onNavigate('rrhh')}
              className={`cursor-pointer transition inline-flex items-center gap-1.5 ${currentView === 'rrhh' ? 'text-midnight-900 font-medium' : 'hover:text-midnight-900'}`}>
              <I.chart className="h-3.5 w-3.5"/>RRHH
            </a>
          )}
          {user.role === 'admin' && (
            <a onClick={() => onNavigate && onNavigate('admin')}
              className={`cursor-pointer transition inline-flex items-center gap-1.5 ${currentView === 'admin' ? 'text-midnight-900 font-medium' : 'hover:text-midnight-900'}`}>
              <I.grid className="h-3.5 w-3.5"/>Panel admin
            </a>
          )}
        </nav>
        <div className="flex items-center gap-3">
          <div className="flex items-center gap-2.5 pl-3 border-l border-line">
            <div className="h-8 w-8 rounded-full bg-gradient-to-br from-mint-300 to-mint-500 grid place-items-center text-white text-[12px] font-semibold">{initials}</div>
            <div className="leading-tight">
              <div className="text-[12.5px] font-medium text-midnight-900">{firstName}</div>
              <div className="text-[11px] text-slate-500 tnum">{user.code}</div>
            </div>
            <button onClick={onLogout} className="ml-2 text-[11px] text-slate-400 hover:text-rose-600" title="Cerrar sesion">Salir</button>
          </div>
        </div>
      </div>
    </header>
  );
};

Object.assign(window, { Logo, I, Badge, StatusDot, NavItem, TopNav });
