/// Configuration principale de l'application UnionFlow
/// 
/// Contient la configuration globale de l'app avec thème, localisation et navigation
library app;

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import '../shared/design_system/theme/app_theme_sophisticated.dart';
import '../features/authentication/presentation/bloc/auth_bloc.dart';
import '../core/l10n/locale_provider.dart';
import '../core/di/injection.dart';
import 'router/app_router.dart';

/// Application principale avec système d'authentification Keycloak
class UnionFlowApp extends StatelessWidget {
  final LocaleProvider localeProvider;

  const UnionFlowApp({super.key, required this.localeProvider});

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: localeProvider),
        BlocProvider(
          create: (context) => getIt<AuthBloc>()..add(const AuthStatusChecked()),
        ),
      ],
      child: Consumer<LocaleProvider>(
        builder: (context, localeProvider, child) {
          return MaterialApp(
            title: 'UnionFlow',
            debugShowCheckedModeBanner: false,

            // Configuration du thème
            theme: AppThemeSophisticated.lightTheme,
            darkTheme: AppThemeSophisticated.darkTheme,
            themeMode: ThemeMode.system,

            // Configuration de la localisation
            locale: localeProvider.locale,
            supportedLocales: LocaleProvider.supportedLocales,
            localizationsDelegates: const [
              AppLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],

        // Configuration des routes
        routes: AppRouter.routes,

        // Page d'accueil par défaut
        initialRoute: AppRouter.initialRoute,

            // Builder global pour gérer les erreurs
            builder: (context, child) {
              return MediaQuery(
                data: MediaQuery.of(context).copyWith(
                  textScaler: const TextScaler.linear(1.0),
                ),
                child: child ?? const SizedBox(),
              );
            },
          );
        },
      ),
    );
  }
}
