import { BrowserRouter as Router, Route, Routes, useLocation } from 'react-router-dom';

// Auth components
import ProtectedRoute from '@/components/auth/ProtectedRoute';
import PublicRoute from '@/components/auth/PublicRoute';

// Layout and pages
import Layout from "./Layout.jsx";
import Home from "./Home";
import Login from "./Login";
import Register from "./Register";
import Dashboard from "./Dashboard";
import StaffDirectory from "./StaffDirectory";
import AddStaff from "./AddStaff";
import EditStaff from "./EditStaff";
import Events from "./Events";
import CreateEvent from "./CreateEvent";
import EditEvent from "./EditEvent";
import EventDetail from "./EventDetail";
import Business from "./Business";
import Invoices from "./Invoices";
import Payroll from "./Payroll";
import Certification from "./Certification";
import Support from "./Support";
import Reports from "./Reports";
import Settings from "./Settings";
import ActivityLog from "./ActivityLog";
import AddBusiness from "./AddBusiness";
import EditBusiness from "./EditBusiness";
import ProcurementDashboard from "./ProcurementDashboard";
import OperatorDashboard from "./OperatorDashboard";
import VendorDashboard from "./VendorDashboard";
import WorkforceDashboard from "./WorkforceDashboard";
import Messages from "./Messages";
import ClientDashboard from "./ClientDashboard";
import Onboarding from "./Onboarding";
import ClientOrders from "./ClientOrders";
import ClientInvoices from "./ClientInvoices";
import VendorOrders from "./VendorOrders";
import VendorStaff from "./VendorStaff";
import VendorInvoices from "./VendorInvoices";
import VendorPerformance from "./VendorPerformance";
import WorkforceShifts from "./WorkforceShifts";
import WorkforceEarnings from "./WorkforceEarnings";
import WorkforceProfile from "./WorkforceProfile";
import UserManagement from "./UserManagement";
import VendorRateCard from "./VendorRateCard";
import Permissions from "./Permissions";
import WorkforceCompliance from "./WorkforceCompliance";
import Teams from "./Teams";
import CreateTeam from "./CreateTeam";
import TeamDetails from "./TeamDetails";
import VendorManagement from "./VendorManagement";
import PartnerManagement from "./PartnerManagement";
import EnterpriseManagement from "./EnterpriseManagement";
import VendorOnboarding from "./VendorOnboarding";
import SectorManagement from "./SectorManagement";
import AddEnterprise from "./AddEnterprise";
import AddSector from "./AddSector";
import AddPartner from "./AddPartner";
import EditVendor from "./EditVendor";
import SmartVendorOnboarding from "./SmartVendorOnboarding";
import InviteVendor from "./InviteVendor";
import VendorCompliance from "./VendorCompliance";
import EditPartner from "./EditPartner";
import EditSector from "./EditSector";
import EditEnterprise from "./EditEnterprise";
import VendorRates from "./VendorRates";
import VendorDocumentReview from "./VendorDocumentReview";
import VendorMarketplace from "./VendorMarketplace";
import RapidOrder from "./RapidOrder";
import SmartScheduler from "./SmartScheduler";
import StaffOnboarding from "./StaffOnboarding";
import NotificationSettings from "./NotificationSettings";
import TaskBoard from "./TaskBoard";
import InvoiceDetail from "./InvoiceDetail";
import InvoiceEditor from "./InvoiceEditor";
import Tutorials from "./Tutorials";
import Schedule from "./Schedule";
import StaffAvailability from "./StaffAvailability";
import WorkerShiftProposals from "./WorkerShiftProposals";

const PAGES = {
    Dashboard,
    StaffDirectory,
    AddStaff,
    EditStaff,
    Events,
    CreateEvent,
    EditEvent,
    EventDetail,
    Business,
    Invoices,
    Payroll,
    Certification,
    Support,
    Reports,
    Settings,
    ActivityLog,
    AddBusiness,
    EditBusiness,
    ProcurementDashboard,
    OperatorDashboard,
    VendorDashboard,
    WorkforceDashboard,
    Messages,
    ClientDashboard,
    Onboarding,
    ClientOrders,
    ClientInvoices,
    VendorOrders,
    VendorStaff,
    VendorInvoices,
    VendorPerformance,
    WorkforceShifts,
    WorkforceEarnings,
    WorkforceProfile,
    UserManagement,
    Home,
    VendorRateCard,
    Permissions,
    WorkforceCompliance,
    Teams,
    CreateTeam,
    TeamDetails,
    VendorManagement,
    PartnerManagement,
    EnterpriseManagement,
    VendorOnboarding,
    SectorManagement,
    AddEnterprise,
    AddSector,
    AddPartner,
    EditVendor,
    SmartVendorOnboarding,
    InviteVendor,
    VendorCompliance,
    EditPartner,
    EditSector,
    EditEnterprise,
    VendorRates,
    VendorDocumentReview,
    VendorMarketplace,
    RapidOrder,
    SmartScheduler,
    StaffOnboarding,
    NotificationSettings,
    TaskBoard,
    InvoiceDetail,
    InvoiceEditor,
    Tutorials,
    Schedule,
    StaffAvailability,
    WorkerShiftProposals,
};

function _getCurrentPage(url) {
    if (url.endsWith('/')) url = url.slice(0, -1);
    let last = url.split('/').pop();
    if (last.includes('?')) last = last.split('?')[0];
    const pageName = Object.keys(PAGES).find(p => p.toLowerCase() === last.toLowerCase());
    return pageName || 'Home'; // Default to Home
}


function AppRoutes() {
    const location = useLocation();
    const currentPage = _getCurrentPage(location.pathname);

    return (
        <Routes>
            {/* Public Routes */}
            <Route path="/login" element={<PublicRoute><Login /></PublicRoute>} />
            <Route path="/register" element={<PublicRoute><Register /></PublicRoute>} />

            {/* Private Routes */}
            <Route path="/*" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <Routes>
                            <Route path="/" element={<Home />} />
                            <Route path="/Dashboard" element={<Dashboard />} />
                            <Route path="/StaffDirectory" element={<StaffDirectory />} />
                            <Route path="/AddStaff" element={<AddStaff />} />
                            <Route path="/EditStaff" element={<EditStaff />} />
                            <Route path="/Events" element={<Events />} />
                            <Route path="/CreateEvent" element={<CreateEvent />} />
                            <Route path="/EditEvent" element={<EditEvent />} />
                            <Route path="/EventDetail" element={<EventDetail />} />
                            <Route path="/Business" element={<Business />} />
                            <Route path="/Invoices" element={<Invoices />} />
                            <Route path="/Payroll" element={<Payroll />} />
                            <Route path="/Certification" element={<Certification />} />
                            <Route path="/Support" element={<Support />} />
                            <Route path="/Reports" element={<Reports />} />
                            <Route path="/Settings" element={<Settings />} />
                            <Route path="/ActivityLog" element={<ActivityLog />} />
                            <Route path="/AddBusiness" element={<AddBusiness />} />
                            <Route path="/EditBusiness" element={<EditBusiness />} />
                            <Route path="/ProcurementDashboard" element={<ProcurementDashboard />} />
                            <Route path="/OperatorDashboard" element={<OperatorDashboard />} />
                            <Route path="/VendorDashboard" element={<VendorDashboard />} />
                            <Route path="/WorkforceDashboard" element={<WorkforceDashboard />} />
                            <Route path="/Messages" element={<Messages />} />
                            <Route path="/ClientDashboard" element={<ClientDashboard />} />
                            <Route path="/Onboarding" element={<Onboarding />} />
                            <Route path="/ClientOrders" element={<ClientOrders />} />
                            <Route path="/ClientInvoices" element={<ClientInvoices />} />
                            <Route path="/VendorOrders" element={<VendorOrders />} />
                            <Route path="/VendorStaff" element={<VendorStaff />} />
                            <Route path="/VendorInvoices" element={<VendorInvoices />} />
                            <Route path="/VendorPerformance" element={<VendorPerformance />} />
                            <Route path="/WorkforceShifts" element={<WorkforceShifts />} />
                            <Route path="/WorkforceEarnings" element={<WorkforceEarnings />} />
                            <Route path="/WorkforceProfile" element={<WorkforceProfile />} />
                            <Route path="/UserManagement" element={<UserManagement />} />
                            <Route path="/Home" element={<Home />} />
                            <Route path="/VendorRateCard" element={<VendorRateCard />} />
                            <Route path="/Permissions" element={<Permissions />} />
                            <Route path="/WorkforceCompliance" element={<WorkforceCompliance />} />
                            <Route path="/Teams" element={<Teams />} />
                            <Route path="/CreateTeam" element={<CreateTeam />} />
                            <Route path="/TeamDetails" element={<TeamDetails />} />
                            <Route path="/VendorManagement" element={<VendorManagement />} />
                            <Route path="/PartnerManagement" element={<PartnerManagement />} />
                            <Route path="/EnterpriseManagement" element={<EnterpriseManagement />} />
                            <Route path="/VendorOnboarding" element={<VendorOnboarding />} />
                            <Route path="/SectorManagement" element={<SectorManagement />} />
                            <Route path="/AddEnterprise" element={<AddEnterprise />} />
                            <Route path="/AddSector" element={<AddSector />} />
                            <Route path="/AddPartner" element={<AddPartner />} />
                            <Route path="/EditVendor" element={<EditVendor />} />
                            <Route path="/SmartVendorOnboarding" element={<SmartVendorOnboarding />} />
                            <Route path="/InviteVendor" element={<InviteVendor />} />
                            <Route path="/VendorCompliance" element={<VendorCompliance />} />
                            <Route path="/EditPartner" element={<EditPartner />} />
                            <Route path="/EditSector" element={<EditSector />} />
                            <Route path="/EditEnterprise" element={<EditEnterprise />} />
                            <Route path="/VendorRates" element={<VendorRates />} />
                            <Route path="/VendorDocumentReview" element={<VendorDocumentReview />} />
                            <Route path="/VendorMarketplace" element={<VendorMarketplace />} />
                            <Route path="/RapidOrder" element={<RapidOrder />} />
                            <Route path="/SmartScheduler" element={<SmartScheduler />} />
                            <Route path="/StaffOnboarding" element={<StaffOnboarding />} />
                            <Route path="/NotificationSettings" element={<NotificationSettings />} />
                            <Route path="/TaskBoard" element={<TaskBoard />} />
                            <Route path="/InvoiceDetail" element={<InvoiceDetail />} />
                            <Route path="/InvoiceEditor" element={<InvoiceEditor />} />
                            <Route path="/Tutorials" element={<Tutorials />} />
                            <Route path="/Schedule" element={<Schedule />} />
                            <Route path="/StaffAvailability" element={<StaffAvailability />} />
                            <Route path="/WorkerShiftProposals" element={<WorkerShiftProposals />} />
                        </Routes>
                    </Layout>
                </ProtectedRoute>
            } />
        </Routes>
    );
}

export default function Pages() {
    return (
        <Router>
            <AppRoutes />
        </Router>
    );
}