// frontend/src/App.jsx
import React from "react";
import { Routes, Route, Navigate } from "react-router-dom";
import LoginPage from "./pages/Login";
import StudentDashboard from "./pages/StudentDashboard";
import StudentExams from "./pages/StudentExams";
import StudentClasses from "./pages/StudentClasses";
import AdminLogin from "./pages/AdminLogin";
import AdminDashboard from "./pages/AdminDashboard";
import AdminMembers from "./pages/AdminMembers";
import AdminClasses from "./pages/AdminClasses";
import AdminExams from "./pages/AdminExams";
import InviteAcceptance from "./pages/InviteAcceptance";
import StudentProfile from "./pages/StudentProfile";
import CoachLogin from "./pages/CoachLogin";
import CoachDashboard from "./pages/CoachDashboard";

function RequireStudent({ children }) {
  const stored = localStorage.getItem("karateStudent");
  if (!stored) {
    return <Navigate to="/login" replace />;
  }
  return children;
}

function RequireAdmin({ children }) {
  const stored = sessionStorage.getItem("admin");
  if (!stored) {
    return <Navigate to="/admin/login" replace />;
  }
  return children;
}

function RequireCoach({ children }) {
  const stored = localStorage.getItem("karateCoach");
  if (!stored) {
    return <Navigate to="/coach/login" replace />;
  }
  return children;
}

function RequireAdminOrCoach({ children }) {
  const storedAdmin = sessionStorage.getItem("admin");
  const storedCoach = localStorage.getItem("karateCoach");
  if (!storedAdmin && !storedCoach) {
    return <Navigate to="/login" replace />;
  }
  return children;
}

export default function App() {
  return (
    <Routes>
      {/* Root goes to student login for now */}
      <Route path="/" element={<Navigate to="/login" replace />} />

      {/* Student auth */}
      <Route path="/login" element={<LoginPage />} />
      <Route path="/invite/:token" element={<InviteAcceptance />} />
      <Route
        path="/student"
        element={
          <RequireStudent>
            <StudentDashboard />
          </RequireStudent>
        }
      />
      <Route
        path="/student/exams"
        element={
          <RequireStudent>
            <StudentExams />
          </RequireStudent>
        }
      />
      <Route
        path="/student/classes"
        element={
          <RequireStudent>
            <StudentClasses />
          </RequireStudent>
        }
      />

      {/* Admin auth */}
      <Route path="/admin/login" element={<AdminLogin />} />
      <Route
        path="/admin"
        element={
          <RequireAdmin>
            <AdminDashboard />
          </RequireAdmin>
        }
      />
      
      {/* Coach auth */}
      <Route path="/coach/login" element={<CoachLogin />} />
      <Route
        path="/coach"
        element={
          <RequireCoach>
            <CoachDashboard />
          </RequireCoach>
        }
      />
      <Route
        path="/admin/members"
        element={
          <RequireAdmin>
            <AdminMembers />
          </RequireAdmin>
        }
      />
      <Route
        path="/admin/classes"
        element={
          <RequireAdmin>
            <AdminClasses />
          </RequireAdmin>
        }
      />
      <Route
        path="/admin/exams"
        element={
          <RequireAdmin>
            <AdminExams />
          </RequireAdmin>
        }
      />
      <Route
        path="/admin/students/:id"
        element={
          <RequireAdminOrCoach>
            <StudentProfile />
          </RequireAdminOrCoach>
        }
      />

      {/* Fallback */}
      <Route path="*" element={<Navigate to="/login" replace />} />
    </Routes>
  );
}
