import React from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import { useAuth } from './context/AuthContext';
import ProtectedRoute from './components/common/ProtectedRoute';
import AppLayout from './components/common/AppLayout';

// Pages
import LoginPage from './pages/LoginPage';
import ChangePasswordPage from './pages/ChangePasswordPage';
import DashboardPage from './pages/DashboardPage';
import UserPortalPage from './pages/UserPortalPage';
import FidoKeysPage from './pages/FidoKeysPage';
import AssetsPage from './pages/AssetsPage';
import OnOffboardingPage from './pages/OnOffboardingPage';
import UsersPage from './pages/UsersPage';
import TicketsPage from './pages/TicketsPage';
import TicketDetailPage from './pages/TicketDetailPage';
import LicensesPage from './pages/LicensesPage';
import HealthPage from './pages/HealthPage';
import ApiDocsPage from './pages/ApiDocsPage';
import SystemPage from './pages/SystemPage';
import MaintenancePage from './pages/MaintenancePage';
import TicketMetricsPage from './pages/TicketMetricsPage';
import AiPage from './pages/AiPage';
import SettingsPage from './pages/SettingsPage';
import KnowledgeBasePage from './pages/KnowledgeBasePage';
import DefectReportPage from './pages/DefectReportPage';
import DocsPage from './pages/DocsPage';
import ProcessManagementPage from './pages/ProcessManagementPage';
import ItOverviewPage from './pages/ItOverviewPage';
import OnboardingConfirmPage from './pages/OnboardingConfirmPage';
import MyAccountPage from './pages/MyAccountPage';
import AnlagevermoegenPage from './pages/AnlagevermoegenPage';
import IsoPage from './pages/IsoPage';
import RiskPage from './pages/RiskPage';
import EntraPage from './pages/EntraPage';
import MonitoringPage from './pages/MonitoringPage';
import AgentDetailPage from './pages/AgentDetailPage';
import DefenderPage from './pages/DefenderPage';
import WarehousePage from './pages/WarehousePage';
import PortalPage from './pages/PortalPage';
import PatchManagementPage from './pages/PatchManagementPage';
import OffboardingDetailPage from './pages/OffboardingDetailPage';
import ProxmoxPage from './pages/ProxmoxPage';
import DockerPage from './pages/DockerPage';
import KnowledgeAiPage from './pages/KnowledgeAiPage';
import SharesPage from './pages/SharesPage';
import PublicSharePage from './pages/PublicSharePage';
import SecurityReportsPage from './pages/SecurityReportsPage';
import ScannerPage from './pages/ScannerPage';
import TVDashboardPage from './pages/TVDashboardPage';
import UserManagementPage from './pages/UserManagementPage';
import FeedbackPage from './pages/FeedbackPage';
import RdpPage from './pages/RdpPage';

const L = ({ children, roles }) => (
    <ProtectedRoute allowedRoles={roles}>
        <AppLayout>{children}</AppLayout>
    </ProtectedRoute>
);

const AppRoutes = () => {
    const { isAuthenticated, isSupport } = useAuth();
    const isStaff = isSupport();

    return (
        <Routes>
            {/* TV Dashboard – kein Layout, fullscreen */}
            <Route path="/tv" element={<TVDashboardPage />} />
            {/* Remote Desktop – kein Layout, fullscreen, eigener Tab */}
            <Route path="/rdp/:agentId" element={<RdpPage />} />

            {/* Public routes */}
            <Route path="/health" element={<HealthPage />} />
            <Route path="/defect" element={<DefectReportPage />} />
            <Route path="/s/:token" element={<PublicSharePage />} />
            <Route path="/onboarding-confirm/:token" element={<OnboardingConfirmPage />} />

            <Route
                path="/login"
                element={isAuthenticated ? <Navigate to="/dashboard" replace /> : <LoginPage />}
            />

            {/* Password change - kein Layout (Erst-Login) */}
            <Route
                path="/change-password"
                element={<ProtectedRoute><ChangePasswordPage /></ProtectedRoute>}
            />

            {/* Root redirect */}
            <Route
                path="/"
                element={
                    <ProtectedRoute>
                        <Navigate to={isStaff ? '/dashboard' : '/portal'} replace />
                    </ProtectedRoute>
                }
            />

            {/* Protected routes mit Sidebar-Layout */}
            <Route path="/dashboard"    element={<L>{isStaff ? <DashboardPage /> : <Navigate to="/portal" replace />}</L>} />
            <Route path="/portal"       element={<L><UserPortalPage /></L>} />
            <Route path="/fido-keys"    element={<L><FidoKeysPage /></L>} />

            <Route path="/assets"       element={<L roles={['super_admin', 'admin', 'support', 'bearbeiter', 'produktion']}><AssetsPage /></L>} />

            <Route path="/onboarding"   element={<L roles={['super_admin', 'admin', 'hr_personal', 'buchhaltung', 'support']}><OnOffboardingPage /></L>} />
            <Route path="/offboarding"  element={<L roles={['super_admin', 'admin', 'hr_personal', 'buchhaltung', 'support']}><OnOffboardingPage /></L>} />
            <Route path="/lifecycle"    element={<L roles={['super_admin', 'admin', 'hr_personal', 'buchhaltung', 'support']}><OnOffboardingPage /></L>} />
            <Route path="/lifecycle/offboarding/:id" element={<L roles={['super_admin', 'admin', 'hr_personal', 'buchhaltung', 'support']}><OffboardingDetailPage /></L>} />

            <Route path="/users"        element={<L roles={['super_admin', 'admin']}><UsersPage /></L>} />
            <Route path="/benutzerverwaltung" element={<L roles={['super_admin', 'admin']}><UserManagementPage /></L>} />

            <Route path="/tickets"      element={<L>{isStaff ? <TicketsPage /> : <Navigate to="/portal" replace />}</L>} />
            <Route path="/tickets/:id"  element={<L><TicketDetailPage /></L>} />

            <Route path="/ticket-metrics" element={<L roles={['super_admin', 'admin', 'support', 'bearbeiter']}><TicketMetricsPage /></L>} />
            <Route path="/licenses"     element={<L roles={['super_admin', 'admin', 'bearbeiter']}><LicensesPage /></L>} />
            <Route path="/api-docs"     element={<L roles={['super_admin', 'admin']}><ApiDocsPage /></L>} />
            <Route path="/system"       element={<L roles={['super_admin', 'admin']}><SystemPage /></L>} />
            <Route path="/maintenance"  element={<L roles={['super_admin', 'admin', 'bearbeiter']}><MaintenancePage /></L>} />
            <Route path="/ai"           element={<L roles={['super_admin', 'admin', 'support', 'bearbeiter']}><AiPage /></L>} />
            <Route path="/knowledge-base" element={<L roles={['super_admin', 'admin', 'support', 'bearbeiter']}><KnowledgeBasePage /></L>} />
            <Route path="/settings"     element={<L roles={['super_admin', 'admin']}><SettingsPage /></L>} />
            <Route path="/docs"         element={<L roles={['super_admin', 'admin']}><DocsPage /></L>} />
            <Route path="/process-management" element={<L roles={['super_admin', 'admin', 'hr_personal', 'buchhaltung', 'support']}><ProcessManagementPage /></L>} />
            <Route path="/it-overview"  element={<L roles={['super_admin', 'admin', 'support', 'bearbeiter']}><ItOverviewPage /></L>} />
            <Route path="/mein-konto"   element={<L><MyAccountPage /></L>} />
            <Route path="/anlagevermoegen" element={<L roles={['super_admin', 'admin', 'buchhaltung']}><AnlagevermoegenPage /></L>} />
            <Route path="/iso"            element={<L roles={['super_admin', 'admin']}><IsoPage /></L>} />
            <Route path="/risk"           element={<L roles={['super_admin', 'admin']}><RiskPage /></L>} />
            <Route path="/entra"          element={<L roles={['super_admin', 'admin']}><EntraPage /></L>} />
            <Route path="/monitoring"             element={<L roles={['super_admin', 'admin']}><MonitoringPage /></L>} />
            <Route path="/monitoring/device/:id"            element={<L roles={['super_admin', 'admin']}><AgentDetailPage /></L>} />
            <Route path="/monitoring/device/host/:hostname" element={<L roles={['super_admin', 'admin']}><AgentDetailPage /></L>} />
            <Route path="/defender"           element={<L roles={['super_admin', 'admin']}><DefenderPage /></L>} />
            <Route path="/warehouse"          element={<L roles={['super_admin', 'admin', 'support', 'bearbeiter']}><WarehousePage /></L>} />
            <Route path="/anleitungen"        element={<L roles={['super_admin', 'admin', 'support', 'bearbeiter', 'benutzer', 'hr_personal', 'buchhaltung', 'produktion']}><PortalPage /></L>} />
            <Route path="/patch-management"   element={<L roles={['super_admin', 'admin']}><PatchManagementPage /></L>} />
            <Route path="/proxmox"            element={<L roles={['super_admin', 'admin']}><ProxmoxPage /></L>} />
            <Route path="/docker"             element={<L roles={['super_admin', 'admin']}><DockerPage /></L>} />
            <Route path="/ki-wissen"          element={<L roles={['super_admin', 'admin']}><KnowledgeAiPage /></L>} />
            <Route path="/security-reports"   element={<L roles={['super_admin', 'admin']}><SecurityReportsPage /></L>} />
            <Route path="/network-monitor"    element={<Navigate to="/monitoring" replace />} />
            <Route path="/nexus-scanner"      element={<L roles={['super_admin', 'admin']}><ScannerPage /></L>} />
            <Route path="/feedback"           element={<L roles={['super_admin', 'admin']}><FeedbackPage /></L>} />

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

export default AppRoutes;
