import 'package:english_learning/core/services/dio_client.dart';
import 'package:english_learning/core/services/repositories/completed_topics_repository.dart';
import 'package:english_learning/core/services/repositories/exercise_repository.dart';
import 'package:english_learning/core/services/repositories/history_repository.dart';
import 'package:english_learning/core/utils/styles/theme.dart';
import 'package:english_learning/features/auth/provider/user_provider.dart';
import 'package:english_learning/features/auth/provider/validator_provider.dart';
import 'package:english_learning/features/history/provider/history_provider.dart';
import 'package:english_learning/features/home/provider/completed_topics_provider.dart';
import 'package:english_learning/features/learning/modules/exercises/providers/exercise_provider.dart';
import 'package:english_learning/features/learning/modules/level/providers/level_provider.dart';
import 'package:english_learning/features/learning/modules/topics/providers/topic_provider.dart';
import 'package:english_learning/features/learning/provider/section_provider.dart';
import 'package:english_learning/features/splash/screens/splash_screen.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        Provider<DioClient>(
          create: (_) => DioClient(),
        ),
        ChangeNotifierProvider(create: (_) => ValidatorProvider()),
        ChangeNotifierProvider(create: (_) => TopicProvider()),
        ChangeNotifierProvider(create: (_) => LevelProvider()),
        ChangeNotifierProvider(create: (_) => SectionProvider()),
        ChangeNotifierProvider(create: (_) => UserProvider()),
        ProxyProvider<DioClient, ExerciseRepository>(
          update: (_, dioClient, __) => ExerciseRepository(dioClient),
        ),
        ProxyProvider<DioClient, HistoryRepository>(
          update: (_, dioClient, __) => HistoryRepository(dioClient),
        ),
        ProxyProvider<DioClient, CompletedTopicsRepository>(
          update: (_, dioClient, __) => CompletedTopicsRepository(dioClient),
        ),
        ChangeNotifierProxyProvider2<HistoryRepository, SectionProvider,
            HistoryProvider>(
          create: (context) => HistoryProvider(
            context.read<HistoryRepository>(),
            context.read<SectionProvider>(),
          ),
          update: (context, historyRepository, sectionProvider, previous) =>
              HistoryProvider(
            historyRepository,
            sectionProvider,
          ),
        ),
        ChangeNotifierProxyProvider2<UserProvider, ExerciseRepository,
            ExerciseProvider>(
          create: (context) => ExerciseProvider(
            context.read<ExerciseRepository>(),
            context.read<UserProvider>(),
          ),
          update: (context, userProvider, exerciseRepository, previous) =>
              ExerciseProvider(
            exerciseRepository,
            userProvider,
          )..updateFrom(previous),
        ),
        ChangeNotifierProxyProvider<CompletedTopicsRepository,
            CompletedTopicsProvider>(
          create: (context) => CompletedTopicsProvider(
            context.read<CompletedTopicsRepository>(),
          ),
          update: (context, completedTopicsRepository, previous) =>
              CompletedTopicsProvider(completedTopicsRepository),
        ),
      ],
      child: Consumer<UserProvider>(
        builder: (context, userProvider, _) {
          return MaterialApp(
            debugShowCheckedModeBanner: false,
            theme: appTheme(),
            home: const SplashScreen(),
          );
        },
      ),
    );
  }
}
