import 'dart:async';

import 'package:flutter/foundation.dart';

import 'package:em2rp/providers/users_provider.dart';
import 'package:em2rp/providers/event_provider.dart';
import 'package:em2rp/providers/equipment_provider.dart';
import 'package:em2rp/providers/container_provider.dart';
import 'package:em2rp/providers/maintenance_provider.dart';
import 'package:em2rp/providers/alert_provider.dart';
import 'package:em2rp/utils/auth_guard_widget.dart';
import 'package:em2rp/views/alerts_page.dart';
import 'package:em2rp/views/calendar_page.dart';
import 'package:em2rp/views/login_page.dart';
import 'package:em2rp/views/equipment_management_page.dart';
import 'package:em2rp/views/container_management_page.dart';
import 'package:em2rp/views/maintenance_management_page.dart';
import 'package:em2rp/views/container_form_page.dart';
import 'package:em2rp/views/container_detail_page.dart';
import 'package:em2rp/views/event_preparation_page.dart';
import 'package:em2rp/views/event_statistics_page.dart';
import 'package:em2rp/models/container_model.dart';
import 'package:em2rp/models/event_model.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:em2rp/services/app_initializer.dart';
import 'utils/colors.dart';
import 'views/my_account_page.dart';
import 'views/user_management_page.dart';
import 'package:provider/provider.dart';
import 'providers/local_user_provider.dart';
import 'views/reset_password_page.dart';
import 'config/env.dart';
import 'utils/app_start_gate.dart';
import 'views/widgets/common/startup_splash_screen.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() {
  // Ne pas effectuer d'initialisations asynchrones lourdes ici.
  WidgetsFlutterBinding.ensureInitialized();

  if (kReleaseMode) {
    debugPrint = (String? message, {int? wrapWidth}) {};
  }

  runZonedGuarded(
    () {
      runApp(
        MultiProvider(
          providers: [
            // Fournisseur d'initialisation de l'application (initialise Firebase et cache en tâche de fond)
            ChangeNotifierProvider<AppInitializer>(
              create: (_) => AppInitializer(),
            ),
            // LocalUserProvider pour la gestion de l'authentification
            ChangeNotifierProvider<LocalUserProvider>(
                create: (context) => LocalUserProvider()),

            // UsersProvider migré vers l'API
            ChangeNotifierProvider<UsersProvider>(
              create: (context) => UsersProvider(),
            ),

            // EventProvider migré vers l'API
            ChangeNotifierProvider<EventProvider>(
              create: (context) => EventProvider(),
            ),

            // EquipmentProvider migré vers l'API
            ChangeNotifierProvider<EquipmentProvider>(
              create: (context) => EquipmentProvider(),
            ),

            // ContainerProvider migré vers l'API
            ChangeNotifierProvider<ContainerProvider>(
              create: (context) => ContainerProvider(),
            ),

            // MaintenanceProvider migré vers l'API
            ChangeNotifierProvider<MaintenanceProvider>(
              create: (context) => MaintenanceProvider(),
            ),
            ChangeNotifierProvider<AlertProvider>(
              create: (context) => AlertProvider(),
            ),
          ],
          child: const MyApp(),
        ),
      );
    },
    (error, stackTrace) {
      if (kDebugMode) {
        print('Uncaught error: $error\n$stackTrace');
      }
    },
    zoneSpecification: ZoneSpecification(
      print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
        if (!kReleaseMode) {
          parent.print(zone, line);
        }
      },
    ),
  );
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late final Future<void> _startupFuture;

  @override
  void initState() {
    super.initState();
    _startupFuture = _bootstrapApp();
  }

  Future<void> _bootstrapApp() async {
    final initializer = context.read<AppInitializer>();
    final localAuthProvider = context.read<LocalUserProvider>();

    await initializer.initialize();

    // Lancer la connexion automatique en dev sans bloquer le démarrage initial
    if (Env.isDevelopment && FirebaseAuth.instance.currentUser == null) {
      unawaited(
        localAuthProvider.signInWithEmailAndPassword(
          Env.devAdminEmail,
          Env.devAdminPassword,
        ).then((_) {
          return localAuthProvider.loadUserData();
        }).catchError((e) {
          if (kDebugMode) debugPrint('Dev auto-login failed: $e');
        }),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<void>(
      future: _startupFuture,
      builder: (context, snapshot) {
        if (snapshot.connectionState != ConnectionState.done) {
          return const MaterialApp(
            debugShowCheckedModeBanner: false,
            home: StartupSplashScreen(),
          );
        }

        return MaterialApp(
          title: 'EM2 Hub',
          theme: ThemeData(
            primarySwatch: Colors.red,
            primaryColor: AppColors.noir,
            colorScheme:
                ColorScheme.fromSwatch().copyWith(secondary: AppColors.rouge),
            textTheme: const TextTheme(
              bodyMedium: TextStyle(color: AppColors.noir),
            ),
            inputDecorationTheme: const InputDecorationTheme(
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(color: AppColors.noir),
              ),
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: AppColors.gris),
              ),
              labelStyle: TextStyle(color: AppColors.noir),
              hintStyle: TextStyle(color: AppColors.gris),
            ),
            elevatedButtonTheme: ElevatedButtonThemeData(
              style: ElevatedButton.styleFrom(
                foregroundColor: AppColors.blanc,
                backgroundColor: AppColors.noir,
              ),
            ),
          ),
          locale: const Locale('fr', 'FR'),
          supportedLocales: const [
            Locale('fr', 'FR'),
          ],
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          routes: {
            '/login': (context) => const LoginPage(),
            '/alerts': (context) => const AuthGuard(child: AlertsPage()),
            '/calendar': (context) => const AuthGuard(
                allowWhileLoading: true, child: CalendarPage()),
            '/my_account': (context) => const AuthGuard(child: MyAccountPage()),
            '/user_management': (context) => const AuthGuard(
                requiredPermission: "view_all_users",
                child: UserManagementPage()),
            '/reset_password': (context) {
              final args = ModalRoute.of(context)!.settings.arguments
                  as Map<String, dynamic>;
              return ResetPasswordPage(
                email: args['email'] as String,
                actionCode: args['actionCode'] as String,
              );
            },
            '/equipment_management': (context) => const AuthGuard(
                requiredPermission: "view_equipment",
                child: EquipmentManagementPage()),
            '/container_management': (context) => const AuthGuard(
                requiredPermission: "view_equipment",
                child: ContainerManagementPage()),
            '/maintenance_management': (context) => const AuthGuard(
                requiredPermission: "manage_maintenances",
                child: MaintenanceManagementPage()),
            '/container_form': (context) {
              final args = ModalRoute.of(context)?.settings.arguments;
              return AuthGuard(
                requiredPermission: "manage_equipment",
                child: ContainerFormPage(
                  container: args as ContainerModel?,
                ),
              );
            },
            '/container_detail': (context) {
              final container = ModalRoute.of(context)!.settings.arguments
                  as ContainerModel;
              return AuthGuard(
                requiredPermission: "view_equipment",
                child: ContainerDetailPage(container: container),
              );
            },
            '/event_preparation': (context) {
              final args = ModalRoute.of(context)!.settings.arguments
                  as Map<String, dynamic>;
              final event = args['event'] as EventModel;
              return AuthGuard(
                child: EventPreparationPage(
                  initialEvent: event,
                ),
              );
            },
            '/event_statistics': (context) => const AuthGuard(
                requiredPermission: 'generate_reports',
                child: EventStatisticsPage()),
          },
          home: const AppStartGate(),
        );
      },
    );
  }
}
