import { Toaster } from "@/components/ui/toaster";
import {
  BrowserRouter as Router,
  Route,
  Routes,
  Navigate,
} from "react-router-dom";

import Layout from "./components/Layout";
import ProtectedRoute from "./components/ProtectedRoute";
import PageNotFound from "./lib/PageNotFound";

/* PUBLIC / MASYARAKAT */
import DashboardMasyarakat from "./pages/masyarakat/DashboardMasyarakat";
import Login from "./pages/Login";
import DaftarSPPG from "./pages/DaftarSPPG";
import DetailSPPG from "./pages/DetailSPPG";
import Statistik from "./pages/Statistik";
import Laporan from "./pages/Laporan";
import Pengaduan from "./pages/Pengaduan";
import FAQ from "./pages/FAQ";
import TrackingPublik from "./pages/TrackingPublik";

/* ADMIN */
import AdminDashboard from "./pages/admin/AdminDashboard";
import ManajemenUser from "./pages/admin/ManajemenUser";
import AuditLogPage from "./pages/admin/AuditLogPage";
import TambahSPPG from "./pages/admin/TambahSPPG";
import EditSPPG from "./pages/admin/EditSPPG";
import BackupData from "./pages/admin/BackupData";
import AdminPengiriman from "./pages/admin/AdminPengiriman";

/* SPPG */
import SPPGSaya from "./pages/sppg/SPPGSaya";
import SPPGPengiriman from "./pages/sppg/SPPGPengiriman";

/* VALIDATOR */
import ValidasiData from "./pages/validator/ValidasiData";
import SekolahPengiriman from "./pages/validator/SekolahPengiriman";
import ValidasiPengaduan from "./pages/validator/ValidasiPengaduan";

/* PEMERINTAH */
import DashboardNasional from "./pages/pemerintah/DashboardNasional";
import AktivitasSPPG from "./pages/pemerintah/AktivitasSPPG";
import PemerintahPengiriman from "./pages/pemerintah/PemerintahPengiriman";

/* TEST */
import TestUsers from "./pages/TestUsers";

function App() {
  return (
    <>
      <Router>
        <Routes>
          {/* LOGIN TANPA LAYOUT */}
          <Route path="/login" element={<Login />} />

          {/* MAIN LAYOUT */}
          <Route element={<Layout />}>
            {/* MASYARAKAT / PUBLIC TANPA LOGIN */}
            <Route path="/" element={<DashboardMasyarakat />} />
            <Route path="/masyarakat" element={<DashboardMasyarakat />} />
            <Route path="/daftar-sppg" element={<DaftarSPPG />} />
            <Route path="/masyarakat/sppg" element={<DaftarSPPG />} />
            <Route path="/detail-sppg/:id" element={<DetailSPPG />} />
            <Route path="/statistik" element={<Statistik />} />

            <Route
              path="/laporan"
              element={
                <ProtectedRoute allowedRoles={["admin", "pemerintah"]}>
                  <Laporan />
                </ProtectedRoute>
              }
            />

            <Route path="/masyarakat/laporan" element={<Navigate to="/" replace />} />
            <Route path="/pengaduan" element={<Pengaduan />} />
            <Route path="/masyarakat/pengaduan" element={<Pengaduan />} />
            <Route path="/faq" element={<FAQ />} />
            <Route path="/tracking" element={<TrackingPublik />} />
            <Route path="/komparasi" element={<Navigate to="/" replace />} />

            {/* TEST */}
            <Route path="/test-users" element={<TestUsers />} />

            {/* ADMIN */}
            <Route
              path="/admin"
              element={
                <ProtectedRoute allowedRoles={["admin"]}>
                  <AdminDashboard />
                </ProtectedRoute>
              }
            />

            <Route
              path="/admin/sppg"
              element={
                <ProtectedRoute allowedRoles={["admin"]}>
                  <DaftarSPPG />
                </ProtectedRoute>
              }
            />

            <Route
              path="/admin/sppg/tambah"
              element={
                <ProtectedRoute allowedRoles={["admin"]}>
                  <TambahSPPG />
                </ProtectedRoute>
              }
            />

            <Route
              path="/admin/sppg/edit/:id"
              element={
                <ProtectedRoute allowedRoles={["admin"]}>
                  <EditSPPG />
                </ProtectedRoute>
              }
            />

            <Route
              path="/admin/pengaduan"
              element={
                <ProtectedRoute allowedRoles={["admin"]}>
                  <Pengaduan />
                </ProtectedRoute>
              }
            />

            <Route
              path="/admin/users"
              element={
                <ProtectedRoute allowedRoles={["admin"]}>
                  <ManajemenUser />
                </ProtectedRoute>
              }
            />

            <Route
              path="/admin/audit-log"
              element={
                <ProtectedRoute allowedRoles={["admin"]}>
                  <AuditLogPage />
                </ProtectedRoute>
              }
            />

            <Route
              path="/admin/pengiriman"
              element={
                <ProtectedRoute allowedRoles={["admin"]}>
                  <AdminPengiriman />
                </ProtectedRoute>
              }
            />

            <Route
              path="/admin/backup"
              element={
                <ProtectedRoute allowedRoles={["admin"]}>
                  <BackupData />
                </ProtectedRoute>
              }
            />

            {/* SPPG */}
            <Route path="/sppg" element={<Navigate to="/sppg/saya" replace />} />

            <Route
              path="/sppg/saya"
              element={
                <ProtectedRoute allowedRoles={["sppg"]}>
                  <SPPGSaya />
                </ProtectedRoute>
              }
            />

            <Route
              path="/sppg/pengiriman"
              element={
                <ProtectedRoute allowedRoles={["sppg"]}>
                  <SPPGPengiriman />
                </ProtectedRoute>
              }
            />

            {/* VALIDATOR */}
            <Route
              path="/validator"
              element={<Navigate to="/validator/validasi" replace />}
            />

            <Route
              path="/validator/validasi"
              element={
                <ProtectedRoute allowedRoles={["validator"]}>
                  <ValidasiData />
                </ProtectedRoute>
              }
            />

            <Route
              path="/validator/pengaduan"
              element={
                <ProtectedRoute allowedRoles={["validator"]}>
                  <ValidasiPengaduan />
                </ProtectedRoute>
              }
            />

            <Route
              path="/validator/pengiriman"
              element={
                <ProtectedRoute allowedRoles={["validator"]}>
                  <SekolahPengiriman />
                </ProtectedRoute>
              }
            />

            {/* PEMERINTAH */}
            <Route
              path="/pemerintah"
              element={<Navigate to="/pemerintah/dashboard" replace />}
            />

            <Route
              path="/pemerintah/dashboard"
              element={
                <ProtectedRoute allowedRoles={["pemerintah"]}>
                  <DashboardNasional />
                </ProtectedRoute>
              }
            />

            <Route
              path="/pemerintah/aktivitas"
              element={
                <ProtectedRoute allowedRoles={["pemerintah"]}>
                  <AktivitasSPPG />
                </ProtectedRoute>
              }
            />

            <Route
              path="/pemerintah/pengiriman"
              element={
                <ProtectedRoute allowedRoles={["pemerintah"]}>
                  <PemerintahPengiriman />
                </ProtectedRoute>
              }
            />

            {/* 404 */}
            <Route path="*" element={<PageNotFound />} />
          </Route>
        </Routes>
      </Router>

      <Toaster />
    </>
  );
}

export default App;