import { useEffect, useState } from 'react';
import { DashboardTab } from './components/DashboardTab';
import { ClassesTab } from './components/ClassesTab';
import { StudentsTab } from './components/StudentsTab';
import { LearningTab } from './components/LearningTab';
import { ExamsTab } from './components/ExamsTab';
import { ExamRoomWorkspace } from './components/ExamRoomWorkspace';
import { SystemTab } from './components/SystemTab';
import { MyAccountTab } from './components/AccountsTab';
import { StudentAffairsTab } from './components/StudentAffairsTab';
import { ChatWidget } from './components/ChatWidget';
import { StaffChatSocket } from './hooks/useStaffChatSocket';
import { ClassWorkspace } from './components/ClassWorkspace';
import { NavHistoryBar, useRoute } from './components/NavHistoryBar';
import {
  goBack,
  navigate,
  navigateSystem,
  parseRoute,
  pushNav,
  SYSTEM_SECTION_LABELS,
  TAB_LABELS,
  type SystemSection,
  type TabId,
} from './navigation';
import { useAuth } from './auth/AuthContext';

const IconDashboard = () => (
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <rect x="3" y="3" width="7" height="7" rx="1" /><rect x="14" y="3" width="7" height="7" rx="1" />
    <rect x="3" y="14" width="7" height="7" rx="1" /><rect x="14" y="14" width="7" height="7" rx="1" />
  </svg>
);

const IconClass = () => (
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" />
    <polyline points="9 22 9 12 15 12 15 22" />
  </svg>
);

const IconStudent = () => (
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <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>
);

const IconLearning = () => (
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z" />
    <path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z" />
  </svg>
);

const IconExam = () => (
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <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" />
  </svg>
);

const IconSystem = () => (
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <circle cx="12" cy="12" r="3" />
    <path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42" />
  </svg>
);

const IconStudentAffairs = () => (
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <path d="M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z" />
  </svg>
);

const IconMenu = () => (
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <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" />
  </svg>
);

const SYSTEM_NAV: { section: SystemSection }[] = [
  { section: 'organization' },
  { section: 'network' },
  { section: 'templates' },
];

function App() {
  const route = useRoute();
  const { staff, logout } = useAuth();
  const [sidebarOpen, setSidebarOpen] = useState(false);

  useEffect(() => {
    const initial = parseRoute();
    if (initial.classId || initial.examId) {
      return;
    }
    const label =
      initial.tab === 'system'
        ? `Hệ thống · ${SYSTEM_SECTION_LABELS[initial.systemSection]}`
        : TAB_LABELS[initial.tab];
    pushNav({
      kind: 'tab',
      tab: initial.tab,
      systemSection: initial.tab === 'system' ? initial.systemSection : undefined,
      label,
    });
  }, []);

  useEffect(() => {
    setSidebarOpen(false);
  }, [route.tab, route.classId, route.examId, route.systemSection]);

  const setActiveTab = (tab: string) => {
    navigate(tab as TabId);
    setSidebarOpen(false);
  };

  const handleBackFromWorkspace = () => {
    goBack(route.tab);
  };

  const inWorkspace = Boolean(route.classId || route.examId);

  const classesActive = route.tab === 'classes';
  const learningActive = route.tab === 'learning';
  const examsActive = route.tab === 'exams' || route.examId != null;
  const systemActive = route.tab === 'system' && !inWorkspace;

  const goSystem = (section: SystemSection) => {
    navigateSystem(section);
    setSidebarOpen(false);
  };

  const navBtn = (active: boolean) => `nav-btn${active ? ' active' : ''}`;

  return (
    <>
      <button
        type="button"
        className="mobile-menu-btn"
        aria-label="Mở menu"
        onClick={() => setSidebarOpen(true)}
      >
        <IconMenu />
      </button>

      {sidebarOpen && (
        <button
          type="button"
          className="sidebar-backdrop"
          aria-label="Đóng menu"
          onClick={() => setSidebarOpen(false)}
        />
      )}

      <aside className={`sidebar${sidebarOpen ? ' sidebar--open' : ''}`}>
        <div className="brand">
          <img src="/logo.jpeg" alt="Simple Care" className="brand-logo-img" />
          <div className="brand-text">
            <span className="brand-name">Simple Care</span>
            <span className="brand-sub">Rikkei Education</span>
          </div>
        </div>

        <nav>
          <ul className="nav-links">
            <div className="nav-header">Tổng quan</div>
            <li className="nav-item">
              <button
                className={navBtn(route.tab === 'dashboard' && !inWorkspace)}
                onClick={() => setActiveTab('dashboard')}
              >
                <span className="nav-icon"><IconDashboard /></span>
                Tổng quan
              </button>
            </li>

            <div className="nav-header">CSDL liên kết</div>
            <li className="nav-item">
              <button className={navBtn(classesActive)} onClick={() => setActiveTab('classes')}>
                <span className="nav-icon"><IconClass /></span>
                Lớp học
              </button>
            </li>
            <li className="nav-item">
              <button
                className={navBtn(route.tab === 'students' && !inWorkspace)}
                onClick={() => setActiveTab('students')}
              >
                <span className="nav-icon"><IconStudent /></span>
                Sinh viên
              </button>
            </li>

            <div className="nav-header">Quản lý học tập</div>
            <li className="nav-item">
              <button className={navBtn(learningActive)} onClick={() => setActiveTab('learning')}>
                <span className="nav-icon"><IconLearning /></span>
                Phòng Học
              </button>
            </li>
            <li className="nav-item">
              <button className={navBtn(examsActive)} onClick={() => setActiveTab('exams')}>
                <span className="nav-icon"><IconExam /></span>
                Phòng thi
              </button>
            </li>

            <div className="nav-header">Công tác sinh viên</div>
            <li className="nav-item">
              <button
                className={navBtn(route.tab === 'student-affairs' && !inWorkspace)}
                onClick={() => setActiveTab('student-affairs')}
              >
                <span className="nav-icon"><IconStudentAffairs /></span>
                Công Tác Sinh Viên
              </button>
            </li>

            <div className="nav-header">Hệ thống</div>
            <li className="nav-item">
              <button
                className={navBtn(systemActive)}
                onClick={() => goSystem('organization')}
              >
                <span className="nav-icon"><IconSystem /></span>
                Quản lý hệ thống
              </button>
            </li>
            <ul className="nav-sub">
              {SYSTEM_NAV.map(({ section }) => (
                <li key={section} className="nav-item">
                  <button
                    className={navBtn(systemActive && route.systemSection === section)}
                    onClick={() => goSystem(section)}
                  >
                    {SYSTEM_SECTION_LABELS[section]}
                  </button>
                </li>
              ))}
            </ul>
          </ul>
        </nav>

        <div className="sidebar-footer">
          <button
            type="button"
            className={`sidebar-user-btn ${route.tab === 'profile' && !inWorkspace ? 'active' : ''}`}
            onClick={() => setActiveTab('profile')}
            title="Tài khoản của tôi"
          >
            <span className="sidebar-user-name">
              {staff?.fullName?.trim() || staff?.email?.split('@')[0] || 'Tài khoản'}
            </span>
            <span className="sidebar-user-email">{staff?.email}</span>
          </button>
          <button type="button" className="link-btn" onClick={logout}>
            Đăng xuất
          </button>
        </div>
      </aside>

      <main className="main-content">
        <div className="page-viewport">
          {route.classId ? (
            <ClassWorkspace
              classId={route.classId}
              sourceTab={route.tab}
              onBack={handleBackFromWorkspace}
              renderNavBar={(className) => (
                <NavHistoryBar classLabel={className} onBack={handleBackFromWorkspace} />
              )}
            />
          ) : route.examId ? (
            <ExamRoomWorkspace examId={route.examId} onBack={handleBackFromWorkspace} />
          ) : (
            <>
              {route.tab === 'dashboard' && <DashboardTab onNavigate={setActiveTab} />}
              {route.tab === 'classes' && <ClassesTab />}
              {route.tab === 'students' && <StudentsTab />}
              {route.tab === 'learning' && <LearningTab />}
              {route.tab === 'exams' && <ExamsTab />}
              {route.tab === 'system' && <SystemTab />}
              {route.tab === 'profile' && <MyAccountTab />}
              {route.tab === 'student-affairs' && <StudentAffairsTab />}
            </>
          )}
        </div>
      </main>
      <StaffChatSocket />
      <ChatWidget />
    </>
  );
}

export default App;
