/**
 * App.tsx
 * 
 * Haupt-App-Komponente mit Multi-Tenant Router-Setup.
 * 
 * URL-Struktur:
 * - /                                    → Dashboard/Übersicht
 * - /settings                            → Benutzer-Einstellungen
 * - /mandates/:mandateId/:featureCode/:instanceId/* → Feature-Instanz-Routen
 * - /admin/*                             → System-Administration (nur SysAdmin)
 */

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

// Import global CSS reset first
import './index.css';

// Auth Pages (Public)
import Login from './pages/Login';
import Register from './pages/Register';
import PasswordResetRequest from './pages/PasswordResetRequest';
import Reset from './pages/Reset';
import { InvitePage } from './pages/InvitePage';

// Providers
import { AuthProvider } from './providers/auth/AuthProvider';
import { ProtectedRoute } from './providers/auth/ProtectedRoute';
import { LanguageProvider } from './providers/language/LanguageContext';
import { ToastProvider } from './contexts/ToastContext';
import { WorkflowSelectionProvider } from './contexts/WorkflowSelectionContext';
import { FileProvider } from './contexts/FileContext';

// Layouts
import { MainLayout } from './layouts/MainLayout';
import { FeatureLayout } from './layouts/FeatureLayout';

// Pages
import { DashboardPage } from './pages/Dashboard';
import { SettingsPage } from './pages/Settings';
import { FeatureViewPage } from './pages/FeatureView';
import { AdminMandatesPage, AdminUsersPage, AdminUserMandatesPage, AdminFeatureAccessPage, AdminInvitationsPage, AdminMandateRolesPage, AdminFeatureRolesPage, AdminFeatureInstanceUsersPage } from './pages/admin';

// Workflow Pages (global)
import { PlaygroundPage, WorkflowsPage, AutomationsPage } from './pages/workflows';

// Basedata Pages (global)
import { PromptsPage, FilesPage, ConnectionsPage } from './pages/basedata';

// Migrate Pages (temporary - to be migrated to feature instances)
import { ChatbotPage, PekPage, SpeechPage } from './pages/migrate';

function App() {
    // Load saved theme preference and set app name on app mount
    useEffect(() => {
        // Set app name globally using configuration
        import('../config/config').then(({ getAppName }) => {
            const appName = getAppName();
            document.title = appName;
        });
        
        // Load saved theme preference
        const savedTheme = localStorage.getItem('theme');
        const prefersDark = savedTheme === 'dark' || (!savedTheme && window.matchMedia('(prefers-color-scheme: dark)').matches);
        
        if (prefersDark) {
            document.documentElement.classList.add('dark-theme');
            document.documentElement.classList.remove('light-theme');
        } else {
            document.documentElement.classList.add('light-theme');
            document.documentElement.classList.remove('dark-theme');
        }
        document.documentElement.setAttribute('data-theme', prefersDark ? 'dark' : 'light');
    }, []);
    
    return (
        <LanguageProvider>
            <AuthProvider>
                <ToastProvider>
                    <WorkflowSelectionProvider>
                    <FileProvider>
                    <Router>
                        <Routes>
                        {/* ================================================== */}
                        {/* PUBLIC AUTH ROUTES - NO AUTHENTICATION REQUIRED    */}
                        {/* ================================================== */}
                        <Route path="/login" element={<Login />} />
                        <Route path="/register" element={<Register />} />
                        <Route path="/password-reset-request" element={<PasswordResetRequest />} />
                        <Route path="/reset" element={<Reset />} />
                        <Route path="/invite/:token" element={<InvitePage />} />
                        
                        {/* ================================================== */}
                        {/* PROTECTED ROUTES - REQUIRE AUTHENTICATION          */}
                        {/* ================================================== */}
                        <Route path="/" element={
                            <ProtectedRoute>
                                <MainLayout />
                            </ProtectedRoute>
                        }>
                            {/* Dashboard (Root) */}
                            <Route index element={<DashboardPage />} />
                            
                            {/* System-Seiten (ohne Instanz-Kontext) */}
                            <Route path="settings" element={<SettingsPage />} />
                            
                            {/* ============================================== */}
                            {/* WORKFLOWS ROUTES (global)                      */}
                            {/* ============================================== */}
                            <Route path="workflows">
                                <Route path="playground" element={<PlaygroundPage />} />
                                <Route path="list" element={<WorkflowsPage />} />
                                <Route path="automations" element={<AutomationsPage />} />
                            </Route>
                            
                            {/* ============================================== */}
                            {/* BASISDATEN ROUTES (global)                     */}
                            {/* ============================================== */}
                            <Route path="basedata">
                                <Route path="prompts" element={<PromptsPage />} />
                                <Route path="files" element={<FilesPage />} />
                                <Route path="connections" element={<ConnectionsPage />} />
                            </Route>
                            
                            {/* ============================================== */}
                            {/* MIGRATE TO FEATURES (temporary)                */}
                            {/* ============================================== */}
                            <Route path="chatbot" element={<ChatbotPage />} />
                            <Route path="pek" element={<PekPage />} />
                            <Route path="speech" element={<SpeechPage />} />
                            
                            {/* ============================================== */}
                            {/* FEATURE-INSTANZ ROUTES                         */}
                            {/* /mandates/:mandateId/:featureCode/:instanceId  */}
                            {/* ============================================== */}
                            <Route 
                                path="mandates/:mandateId/:featureCode/:instanceId" 
                                element={<FeatureLayout />}
                            >
                                {/* Feature Views - dynamisch basierend auf featureCode */}
                                <Route index element={<FeatureViewPage view="dashboard" />} />
                                <Route path="dashboard" element={<FeatureViewPage view="dashboard" />} />
                                <Route path="organisations" element={<FeatureViewPage view="organisations" />} />
                                <Route path="contracts" element={<FeatureViewPage view="contracts" />} />
                                <Route path="documents" element={<FeatureViewPage view="documents" />} />
                                <Route path="positions" element={<FeatureViewPage view="positions" />} />
                                <Route path="roles" element={<FeatureViewPage view="roles" />} />
                                <Route path="access" element={<FeatureViewPage view="access" />} />
                                <Route path="runs" element={<FeatureViewPage view="runs" />} />
                                <Route path="files" element={<FeatureViewPage view="files" />} />
                                <Route path="conversations" element={<FeatureViewPage view="conversations" />} />
                                
                                {/* Catch-all für unbekannte Sub-Pfade */}
                                <Route path="*" element={<FeatureViewPage view="not-found" />} />
                            </Route>
                            
                            {/* ============================================== */}
                            {/* ADMIN ROUTES (nur SysAdmin)                    */}
                            {/* ============================================== */}
                            <Route path="admin">
                                <Route path="mandates" element={<AdminMandatesPage />} />
                                <Route path="users" element={<AdminUsersPage />} />
                                <Route path="user-mandates" element={<AdminUserMandatesPage />} />
                                <Route path="feature-instances" element={<AdminFeatureAccessPage />} />
                                <Route path="feature-roles" element={<AdminFeatureRolesPage />} />
                                <Route path="feature-users" element={<AdminFeatureInstanceUsersPage />} />
                                <Route path="invitations" element={<AdminInvitationsPage />} />
                                <Route path="mandate-roles" element={<AdminMandateRolesPage />} />
                            </Route>
                        </Route>
                        
                        {/* ================================================== */}
                        {/* CATCH-ALL - Redirect to Dashboard                  */}
                        {/* ================================================== */}
                        <Route path="*" element={
                            <ProtectedRoute>
                                <MainLayout />
                            </ProtectedRoute>
                        } />
                        </Routes>
                    </Router>
                    </FileProvider>
                    </WorkflowSelectionProvider>
                </ToastProvider>
            </AuthProvider>
        </LanguageProvider>
    );
}

export default App;
