import { useEffect, useRef } from 'react';
import { useAuth } from '../contexts/AuthContext.jsx';
import './NavDrawer.css';

const NAV_ICON = {
  messages: <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>,
  schedules: <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><rect x="3" y="4" width="18" height="18" rx="2" ry="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"/></svg>,
  users: <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><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"/></svg>,
  groups: <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><rect x="2" y="7" width="20" height="14" rx="2"/><path d="M16 7V5a2 2 0 0 0-2-2h-4a2 2 0 0 0-2 2v2"/><line x1="12" y1="12" x2="12" y2="16"/><line x1="10" y1="14" x2="14" y2="14"/></svg>,
  branding: <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="12" cy="12" r="3"/><path d="M12 2a10 10 0 1 0 10 10"/></svg>,
  settings: <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="12" cy="12" r="3"/><path d="M19.07 4.93l-1.41 1.41M5.34 18.66l-1.41 1.41M12 2v2M12 20v2M4.93 4.93l1.41 1.41M18.66 18.66l1.41 1.41M2 12h2M20 12h2"/></svg>,
};

export default function NavDrawer({ open, onClose, onMessages, onGroupManager, onBranding, onSettings, onUsers, features = {} }) {
  const { user } = useAuth();
  const drawerRef = useRef(null);
  const isAdmin = user?.role === 'admin';

  // Close on outside click
  useEffect(() => {
    if (!open) return;
    const handler = (e) => {
      if (drawerRef.current && !drawerRef.current.contains(e.target)) onClose();
    };
    document.addEventListener('mousedown', handler);
    return () => document.removeEventListener('mousedown', handler);
  }, [open, onClose]);

  // Close on Escape
  useEffect(() => {
    if (!open) return;
    const handler = (e) => { if (e.key === 'Escape') onClose(); };
    window.addEventListener('keydown', handler);
    return () => window.removeEventListener('keydown', handler);
  }, [open, onClose]);

  const item = (icon, label, onClick, disabled = false) => (
    <button
      className={`nav-drawer-item${disabled ? ' disabled' : ''}`}
      onClick={disabled ? undefined : () => { onClose(); onClick(); }}
      disabled={disabled}
    >
      {icon}
      <span>{label}</span>
      {disabled && <span className="nav-drawer-badge">Coming soon</span>}
    </button>
  );

  return (
    <>
      {/* Backdrop */}
      <div className={`nav-drawer-backdrop${open ? ' open' : ''}`} onClick={onClose} />
      {/* Drawer */}
      <div ref={drawerRef} className={`nav-drawer${open ? ' open' : ''}`}>
        <div className="nav-drawer-section-label">Menu</div>
        {item(NAV_ICON.messages,  'Messages',  onMessages)}
        {item(NAV_ICON.schedules, 'Schedules', () => {}, true)}

        {isAdmin && (
          <>
            <div className="nav-drawer-section-label admin">Admin</div>
            {item(NAV_ICON.users,   'User Manager',  onUsers)}
            {features.groupManager && item(NAV_ICON.groups,   'Group Manager',  onGroupManager)}
            {features.branding     && item(NAV_ICON.branding, 'Branding',       onBranding)}
            {item(NAV_ICON.settings, 'Settings', onSettings)}
          </>
        )}
      </div>
    </>
  );
}
