import {
  Navigate,
  Route,
  Routes
} from "react-router-dom";

/* =========================================================
   ADMIN COMPONENTS
   ========================================================= */

import AdminProtectedRoute from "./components/admin/AdminProtectedRoute";

import AdminLayout from "./layouts/AdminLayout";

import AdminLogin from "./pages/admin/AdminLogin";
import AdminDashboard from "./pages/admin/AdminDashboard";
import AdminSubmissions from "./pages/admin/AdminSubmissions";
import AdminSubmissionReview from "./pages/admin/AdminSubmissionReview";
import AdminReviewQueue from "./pages/admin/AdminReviewQueue";
import AdminReviewAssignments from "./pages/admin/AdminReviewAssignments";
import AdminUsers from "./pages/admin/AdminUsers";
import AdminAuditLog from "./pages/admin/AdminAuditLog";

/* =========================================================
   STAFF PORTAL LAYOUTS
   ========================================================= */

import PublicPortalLayout from "./layouts/PublicPortalLayout";
import StaffFormLayout from "./layouts/StaffFormLayout";

/* =========================================================
   STAFF ACCESS PAGES
   ========================================================= */

import StaffAccess from "./pages/public/StaffAccess";
import StaffIdentification from "./pages/public/StaffIdentification";
import StaffFormWelcome from "./pages/public/StaffFormWelcome";

/* =========================================================
   STAFF FORM PAGES
   ========================================================= */

import PersonalDetails from "./pages/public/forms/PersonalDetails";
import EmploymentDetails from "./pages/public/forms/EmploymentDetails";
import NextOfKin from "./pages/public/forms/NextOfKin";
import Dependents from "./pages/public/forms/Dependents";
import Qualifications from "./pages/public/forms/Qualifications";
import EmergencyContact from "./pages/public/forms/EmergencyContact";
import Documents from "./pages/public/forms/Documents";
import Declaration from "./pages/public/forms/Declaration";
import SubmissionSuccess from "./pages/public/SubmissionSuccess";
/* =========================================================
   APPLICATION
   ========================================================= */

const App = () => {
  return (
    <Routes>
      {/* =====================================================
          ROOT
          ===================================================== */}

      <Route
        path="/"
        element={
          <Navigate
            to="/staff/access"
            replace
          />
        }
      />

     {/* =====================================================
    PUBLIC STAFF PORTAL
    ===================================================== */}

<Route
  path="/staff"
  element={
    <PublicPortalLayout />
  }
>
  <Route
    index
    element={
      <Navigate
        to="access"
        replace
      />
    }
  />

  <Route
    path="access"
    element={
      <StaffAccess />
    }
  />

  <Route
    path="identify"
    element={
      <StaffIdentification />
    }
  />

  <Route
    path="welcome"
    element={
      <StaffFormWelcome />
    }
  />

  {/* ===================================================
      STAFF RECORD FORM

      The URLs remain:
      /staff/form/personal-details
      /staff/form/employment-details
      etc.
      =================================================== */}

  <Route
    path="form"
    element={
      <StaffFormLayout />
    }
  >
    <Route
      index
      element={
        <Navigate
          to="personal-details"
          replace
        />
      }
    />

    <Route
      path="personal-details"
      element={
        <PersonalDetails />
      }
    />

    <Route
      path="employment-details"
      element={
        <EmploymentDetails />
      }
    />

    <Route
      path="next-of-kin"
      element={
        <NextOfKin />
      }
    />

    <Route
      path="dependents"
      element={
        <Dependents />
      }
    />

    <Route
      path="qualifications"
      element={
        <Qualifications />
      }
    />

    <Route
      path="emergency-contact"
      element={
        <EmergencyContact />
      }
    />

    <Route
      path="documents"
      element={
        <Documents />
      }
    />

    <Route
      path="declaration"
      element={
        <Declaration />
      }
    />

    <Route
      path="*"
      element={
        <Navigate
          to="personal-details"
          replace
        />
      }
    />
  </Route>

<Route
  path="submission-success"
  element={
    <SubmissionSuccess />
  }
/>

  <Route
    path="*"
    element={
      <Navigate
        to="access"
        replace
      />
    }
  />
</Route>
      {/* =====================================================
          ADMINISTRATOR LOGIN
          ===================================================== */}

      <Route
        path="/admin/login"
        element={
          <AdminLogin />
        }
      />

      {/* =====================================================
          PROTECTED ADMINISTRATOR PORTAL
          ===================================================== */}

      <Route
        path="/admin"
        element={
          <AdminProtectedRoute>
            <AdminLayout />
          </AdminProtectedRoute>
        }
      >
        <Route
          index
          element={
            <Navigate
              to="dashboard"
              replace
            />
          }
        />

        <Route
          path="dashboard"
          element={
            <AdminDashboard />
          }
        />

        <Route
          path="submissions"
          element={
            <AdminSubmissions />
          }
        />

        <Route
          path="review"
          element={
            <AdminReviewQueue />
          }
        />

        <Route
          path="reviewers"
          element={
            <AdminProtectedRoute
              allowedRoles={[
                "super_admin",
                "hr_admin"
              ]}
            >
              <AdminReviewAssignments />
            </AdminProtectedRoute>
          }
        />

        <Route
          path="submissions/:submissionId"
          element={
            <AdminSubmissionReview />
          }
        />

        <Route
          path="users"
          element={
            <AdminProtectedRoute
              allowedRoles={[
                "super_admin"
              ]}
            >
              <AdminUsers />
            </AdminProtectedRoute>
          }
        />

        <Route
          path="audit-log"
          element={
            <AdminProtectedRoute
              allowedRoles={[
                "super_admin",
                "hr_admin"
              ]}
            >
              <AdminAuditLog />
            </AdminProtectedRoute>
          }
        />

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

      {/* =====================================================
          UNKNOWN APPLICATION ROUTES
          ===================================================== */}

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

export default App;