import { useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { useAuth } from '../context/AuthContext.jsx';
import UsersSection    from './admin/UsersSection.jsx';
import CreateUserSection from './admin/CreateUserSection.jsx';
import JobLogsSection  from './admin/JobLogsSection.jsx';
import IconsSection    from './admin/IconsSection.jsx';
import SmtpSection     from './admin/SmtpSection.jsx';

/* ── Icônes nav ───────────────────────────────────────────────── */
function IconUsers()    { return <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><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>; }
function IconUserPlus() { return <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M16 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="8.5" cy="7" r="4"/><line x1="20" y1="8" x2="20" y2="14"/><line x1="23" y1="11" x2="17" y2="11"/></svg>; }
function IconActivity() { return <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><polyline points="22 12 18 12 15 21 9 3 6 12 2 12"/></svg>; }
function IconImage()    { return <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>; }
function IconTax()      { return <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>; }
function IconDatabase() { return <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M21 12c0 1.66-4 3-9 3s-9-1.34-9-3"/><path d="M3 5v14c0 1.66 4 3 9 3s9-1.34 9-3V5"/></svg>; }
function IconMail()     { return <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><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"/></svg>; }

const NAV = [
  {
    group: 'Administration de la plateforme',
    items: [
      { id: 'users',    label: 'Utilisateurs',          icon: <IconUsers /> },
      { id: 'create',   label: 'Créer un utilisateur',  icon: <IconUserPlus /> },
      { id: 'job-logs', label: 'Logs des jobs',         icon: <IconActivity /> },
      { id: 'icons',    label: "Bibliothèque d'icônes", icon: <IconImage /> },
    ],
  },
  {
    group: 'Référentiels',
    items: [
      { id: 'plateformes', label: 'Plateformes', icon: <IconDatabase />, href: '/admin/plateformes' },
      { id: 'fiscalite',   label: 'Fiscalité',   icon: <IconTax />,      href: '/admin/fiscalite' },
    ],
  },
  {
    group: 'Notifications',
    items: [
      { id: 'smtp', label: 'SMTP', icon: <IconMail /> },
    ],
  },
];

export default function Admin() {
  const { search } = useLocation();
  const navigate = useNavigate();
  const [refreshKey, setRefreshKey] = useState(0);
  const { user } = useAuth();

  const section = new URLSearchParams(search).get('section') || 'users';

  if (!user) return null;

  return (
    <div className="account-layout">
      <aside className="account-sidebar">
        <h1 className="account-title">Administration</h1>
        {NAV.map(group => (
          <div key={group.group} className="account-nav-group">
            <span className="account-nav-label">{group.group}</span>
            {group.items.map(item => (
              <button
                key={item.id}
                className={`account-nav-item${section === item.id ? ' active' : ''}`}
                onClick={() => item.href ? navigate(item.href) : navigate(`/admin?section=${item.id}`, { replace: true })}
              >
                {item.icon}
                {item.label}
              </button>
            ))}
          </div>
        ))}
      </aside>
      <div className="account-content">
        {section === 'users'    && <UsersSection currentUserId={user?.id} key={refreshKey} />}
        {section === 'create'   && <CreateUserSection onCreated={() => setRefreshKey(k => k + 1)} />}
        {section === 'job-logs' && <JobLogsSection />}
        {section === 'icons'    && <IconsSection />}
        {section === 'smtp'     && <SmtpSection />}
      </div>
    </div>
  );
}
