import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:travel_mate/blocs/balance/balance_bloc.dart';
import 'package:travel_mate/blocs/expense/expense_bloc.dart';
import 'package:travel_mate/blocs/message/message_bloc.dart';
import 'package:travel_mate/services/balance_service.dart';
import 'package:travel_mate/services/error_service.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:travel_mate/services/expense_service.dart'; 
import 'blocs/auth/auth_bloc.dart';
import 'blocs/auth/auth_event.dart';
import 'blocs/theme/theme_bloc.dart';
import 'blocs/theme/theme_event.dart';
import 'blocs/theme/theme_state.dart';
import 'blocs/group/group_bloc.dart';
import 'blocs/user/user_bloc.dart';
import 'blocs/trip/trip_bloc.dart';
import 'blocs/account/account_bloc.dart';
import 'repositories/auth_repository.dart';
import 'repositories/trip_repository.dart';
import 'repositories/user_repository.dart';
import 'repositories/group_repository.dart';
import 'repositories/message_repository.dart';
import 'repositories/account_repository.dart';
import 'repositories/expense_repository.dart';
import 'repositories/balance_repository.dart';
import 'pages/login.dart';
import 'pages/home.dart';
import 'pages/signup.dart';
import 'pages/resetpswd.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await dotenv.load(fileName: ".env");
  await Firebase.initializeApp();

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MultiRepositoryProvider(
      providers: [
        RepositoryProvider<AuthRepository>(
          create: (context) => AuthRepository(),
        ),
        RepositoryProvider<UserRepository>(
          create: (context) => UserRepository(),
        ),
        RepositoryProvider<TripRepository>(
          create: (context) => TripRepository(),
        ),
        RepositoryProvider<GroupRepository>(
          create: (context) => GroupRepository(),
        ),
        RepositoryProvider<MessageRepository>(
          create: (context) => MessageRepository(),
        ),
        RepositoryProvider<AccountRepository>(
          create: (context) => AccountRepository(),
        ),
        RepositoryProvider<ExpenseRepository>(
          create: (context) => ExpenseRepository(),
        ),
        RepositoryProvider<BalanceRepository>(
          create: (context) => BalanceRepository(
            expenseRepository: context.read<ExpenseRepository>(),
          ),
        ),
        
      ],
      child: MultiBlocProvider(
        providers: [
          BlocProvider<ThemeBloc>(
            create: (context) => ThemeBloc()..add(ThemeLoadRequested()),
          ),
          BlocProvider<AuthBloc>(
            create: (context) =>
                AuthBloc(authRepository: context.read<AuthRepository>())
                  ..add(AuthCheckRequested()),
          ),
          BlocProvider(
            create: (context) => GroupBloc(context.read<GroupRepository>()),
          ),
          BlocProvider(
            create: (context) =>
                TripBloc(context.read<TripRepository>()),
          ),
          BlocProvider(create: (context) => UserBloc()),
          BlocProvider(
            create: (context) => MessageBloc(),
          ),
          BlocProvider(
            create: (context) => AccountBloc(
              context.read<AccountRepository>(),
            ),
          ),
          
          // Nouveaux blocs
          BlocProvider<ExpenseBloc>(
            create: (context) => ExpenseBloc(
              expenseRepository: context.read<ExpenseRepository>(),
              expenseService: context.read<ExpenseService>(),
            ),
          ),
          BlocProvider<BalanceBloc>(
            create: (context) => BalanceBloc(
              balanceRepository: context.read<BalanceRepository>(),
              balanceService: context.read<BalanceService>(),
              expenseRepository: context.read<ExpenseRepository>(),
            ),
          ),
          
        ],
        child: BlocBuilder<ThemeBloc, ThemeState>(
          builder: (context, themeState) {
            return MaterialApp(
              title: 'Travel Mate',
              navigatorKey: ErrorService.navigatorKey,
              themeMode: themeState.themeMode,
              theme: ThemeData(
                colorScheme: ColorScheme.fromSeed(
                  seedColor: const Color.fromARGB(255, 180, 180, 180),
                  brightness: Brightness.light,
                ),
                useMaterial3: true,
              ),
              darkTheme: ThemeData(
                colorScheme: ColorScheme.fromSeed(
                  seedColor: const Color.fromARGB(255, 43, 43, 43),
                  brightness: Brightness.dark,
                ),
                useMaterial3: true,
              ),
              home: const LoginPage(),
              routes: {
                '/login': (context) => const LoginPage(),
                '/signup': (context) => const SignUpPage(),
                '/home': (context) => const HomePage(),
                '/forgot': (context) => const ForgotPasswordPage(),
              },
              debugShowCheckedModeBanner: false,
            );
          },
        ),
      ),
    );
  }
}
