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

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

import Login from './pages/Login';
import Register from './pages/Register';
import PasswordResetRequest from './pages/PasswordResetRequest';
import Reset from './pages/Reset';

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

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 ROUTE - requires authentication */}
                        <Route path="/" element={
                            <ProtectedRoute>
                                <FileProvider>
                                    <WorkflowSelectionProvider>
                                        <Home />
                                    </WorkflowSelectionProvider>
                                </FileProvider>
                            </ProtectedRoute>
                        } />
                        
                        {/* Catch-all redirect to home */}
                        <Route path="*" element={
                            <ProtectedRoute>
                                <FileProvider>
                                    <WorkflowSelectionProvider>
                                        <Home />
                                    </WorkflowSelectionProvider>
                                </FileProvider>
                            </ProtectedRoute>
                        } />
                    </Routes>
                </Router>
            </AuthProvider>
        </LanguageProvider>
    );
}

export default App;