/**
 * 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';

// Providers
import { AuthProvider } from './providers/auth/AuthProvider';
import { ProtectedRoute } from './providers/auth/ProtectedRoute';
import { LanguageProvider } from './providers/language/LanguageContext';

// 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';

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>
                <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 />} />
                        
                        {/* ================================================== */}
                        {/* 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 />} />
                            
                            {/* ============================================== */}
                            {/* 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={<div>Admin: Mandanten (TODO)</div>} />
                                <Route path="users" element={<div>Admin: Benutzer (TODO)</div>} />
                                <Route path="roles" element={<div>Admin: Globale Rollen (TODO)</div>} />
                            </Route>
                        </Route>
                        
                        {/* ================================================== */}
                        {/* CATCH-ALL - Redirect to Dashboard                  */}
                        {/* ================================================== */}
                        <Route path="*" element={
                            <ProtectedRoute>
                                <MainLayout />
                            </ProtectedRoute>
                        } />
                    </Routes>
                </Router>
            </AuthProvider>
        </LanguageProvider>
    );
}

export default App;
