/// Configuration principale de l'application UnionFlow
///
/// Thème dynamique (light/dark) via ThemeProvider + localisation + 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 '../l10n/app_localizations.dart';
import '../shared/design_system/theme/app_theme_sophisticated.dart';
import '../features/authentication/presentation/bloc/auth_bloc.dart';
import '../features/organizations/bloc/org_switcher_bloc.dart';
import '../core/l10n/locale_provider.dart';
import '../core/theme/theme_provider.dart';
import '../core/di/injection.dart';
import 'router/app_router.dart';

/// Application principale UnionFlow
class UnionFlowApp extends StatelessWidget {
  final LocaleProvider localeProvider;
  final ThemeProvider themeProvider;

  /// Clé globale pour afficher des SnackBars sans BuildContext (ex: session expirée)
  static final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey =
      GlobalKey<ScaffoldMessengerState>();

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

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: localeProvider),
        ChangeNotifierProvider.value(value: themeProvider),
        BlocProvider(
          create: (context) => getIt<AuthBloc>()..add(const AuthStatusChecked()),
        ),
        BlocProvider(
          create: (context) => getIt<OrgSwitcherBloc>(),
        ),
      ],
      child: Consumer2<LocaleProvider, ThemeProvider>(
        builder: (context, locale, theme, child) {
          return BlocListener<AuthBloc, AuthState>(
            listenWhen: (prev, curr) =>
                curr is AuthAuthenticated && prev is! AuthAuthenticated,
            listener: (context, _) {
              context.read<OrgSwitcherBloc>().add(const OrgSwitcherLoadRequested());
            },
            child: MaterialApp(
            title: 'UnionFlow',
            debugShowCheckedModeBanner: false,
            scaffoldMessengerKey: UnionFlowApp.scaffoldMessengerKey,

            // Thème dynamique piloté par ThemeProvider
            theme: AppThemeSophisticated.lightTheme,
            darkTheme: AppThemeSophisticated.darkTheme,
            themeMode: theme.mode,

            // Localisation
            locale: locale.locale,
            supportedLocales: LocaleProvider.supportedLocales,
            localizationsDelegates: const [
              AppLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],

            // Routes
            routes: AppRouter.routes,
            initialRoute: AppRouter.initialRoute,

            // Fix textScaler global
            builder: (context, child) {
              return MediaQuery(
                data: MediaQuery.of(context).copyWith(
                  textScaler: const TextScaler.linear(1.0),
                ),
                child: child ?? const SizedBox(),
              );
            },
          ),
        );
        },
      ),
    );
  }
}
