import React from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import Login from './features/auth/Login';
import ForgotPassword from './features/auth/ForgotPassword';
import AppLayout from './features/layouts/AppLayout';
import AdminDashboard from './features/dashboard/AdminDashboard';
import ClientDashboard from './features/dashboard/ClientDashboard';
import VendorDashboard from './features/dashboard/VendorDashboard';
import ProtectedRoute from './features/layouts/ProtectedRoute';
import { RoleDashboardRedirect } from './features/dashboard/RoleDashboardRedirect';
import PublicLayout from './features/layouts/PublicLayout';
import StaffList from './features/workforce/directory/StaffList';
import EditStaff from './features/workforce/directory/EditStaff';
import AddStaff from './features/workforce/directory/AddStaff';
import ClientList from './features/business/clients/ClientList';
import EditClient from './features/business/clients/EditClient';
import AddClient from './features/business/clients/AddClient';
import ServiceRates from './features/business/rates/ServiceRates';
import OrderList from './features/operations/orders/OrderList';
import OrderDetail from './features/operations/orders/OrderDetail';
import ClientOrderList from './features/operations/orders/ClientOrderList';
import VendorOrderList from './features/operations/orders/VendorOrderList';
import EditOrder from './features/operations/orders/EditOrder';
import Schedule from './features/operations/schedule/Schedule';
import StaffAvailability from './features/operations/availability/StaffAvailability';
import TaskBoard from './features/operations/tasks/TaskBoard';
import InvoiceList from './features/finance/invoices/InvoiceList';
import InvoiceDetail from './features/finance/invoices/InvoiceDetail';
import InvoiceEditor from './features/finance/invoices/InvoiceEditor';
import ComplianceDashboard from './features/workforce/compliance/ComplianceDashboard';
import DocumentVault from './features/workforce/documents/DocumentVault';

/**
 * AppRoutes Component
 * Defines the main routing structure of the application.
 * Groups routes by Layout (Public vs App).
 * Implements role-based redirection after login.
 */
const AppRoutes: React.FC = () => {
  return (
    <Router>
      <Routes>
        {/* Public Routes */}
        <Route
          path="/login"
          element={
              <PublicLayout>
                <Login />
              </PublicLayout>
          }
        />
        <Route
          path="/forgot-password"
          element={
              <ForgotPassword />
          }
        />
        {/* Authenticated Routes */}
        <Route element={<AppLayout />}>
          {/* Dashboard Redirect Logic - redirects to user's correct dashboard based on role */}
          <Route path="/" element={<RoleDashboardRedirect />} />

          {/* Protected Dashboard Routes */}
          <Route
            path="/dashboard/admin"
            element={
              <ProtectedRoute
                allowedRoles={['admin']}
                redirectTo="/dashboard/admin"
              >
                <AdminDashboard />
              </ProtectedRoute>
            }
          />
          <Route
            path="/dashboard/client"
            element={
              <ProtectedRoute
                allowedRoles={['client']}
                redirectTo="/dashboard/client"
              >
                <ClientDashboard />
              </ProtectedRoute>
            }
          />
          <Route
            path="/dashboard/vendor"
            element={
              <ProtectedRoute
                allowedRoles={['vendor']}
                redirectTo="/dashboard/vendor"
              >
                <VendorDashboard />
              </ProtectedRoute>
            }
          />
          {/* Workforce Routes */}
          <Route path="/staff" element={<StaffList />} />
          <Route path="/staff/add" element={<AddStaff />} />
          <Route path="/staff/:id/edit" element={<EditStaff />} />
          <Route path="/compliance" element={<ComplianceDashboard />} />
          <Route path="/documents" element={<DocumentVault />} />
          {/* Business Routes */}
          <Route path="/clients" element={<ClientList />} />
          <Route path="/clients/:id/edit" element={<EditClient />} />
          <Route path="/clients/add" element={<AddClient />} />
          <Route path="/rates" element={<ServiceRates />} />

           {/* Operations Routes */}
           <Route path="/orders" element={<OrderList />} />
           <Route path="/orders/client" element={<ClientOrderList />} />
           <Route path="/orders/:id" element={<OrderDetail />} />
           <Route path="/orders/vendor" element={<VendorOrderList />} />
           <Route path="/orders/:id/edit" element={<EditOrder />} />

           <Route path="/schedule" element={<Schedule />} />

           <Route path='/availability' element={<StaffAvailability />} />

           <Route path="/tasks" element={<TaskBoard />} />

            {/* Finance Routes */}
          <Route path="/invoices" element={<InvoiceList />} />
          <Route path="/invoices/:id" element={<InvoiceDetail />} />
          <Route path="/invoices/:id/edit" element={<InvoiceEditor />} />
          <Route path="/invoices/new" element={<InvoiceEditor />} />
          
        </Route>
        <Route path="*" element={<Navigate to="/login" replace />} />
      </Routes>
    </Router>
  );
};


export default AppRoutes;
