import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:habo/constants.dart';
import 'package:habo/generated/l10n.dart';
import 'package:habo/habits/habits_manager.dart';
import 'package:habo/model/habo_model.dart';
import 'package:habo/navigation/app_router.dart';
import 'package:habo/navigation/app_state_manager.dart';
import 'package:habo/navigation/route_information_parser.dart';
import 'package:habo/services/service_locator.dart';
import 'package:habo/settings/settings_manager.dart';
import 'package:habo/themes.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Initialize settings
  final settingsManager = SettingsManager();
  await settingsManager.loadData();

  // Initialize database
  final haboModel = HaboModel();
  await haboModel.initDatabase();

  // Initialize service locator
  final scaffoldKey = GlobalKey<ScaffoldMessengerState>();
  ServiceLocator.instance.initialize(scaffoldKey, haboModel, settingsManager);

  // Initialize habits manager
  final habitsManager = HabitsManager(
    habitRepository: ServiceLocator.instance.repositoryFactory.habitRepository,
    eventRepository: ServiceLocator.instance.repositoryFactory.eventRepository,
    categoryRepository: ServiceLocator.instance.repositoryFactory.categoryRepository,
    backupService: ServiceLocator.instance.backupService,
    notificationService: ServiceLocator.instance.notificationService,
    uiFeedbackService: ServiceLocator.instance.uiFeedbackService,
  );
  await habitsManager.loadHabits();

  // Initialize notification service
  await ServiceLocator.instance.notificationService.init();

  // Create app state manager and router
  final appStateManager = AppStateManager();
  final appRouter = AppRouter(appStateManager);

  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: settingsManager),
        ChangeNotifierProvider.value(value: habitsManager),
        ChangeNotifierProvider.value(value: appStateManager),
      ],
      child: HaboApp(
        appRouter: appRouter,
        settingsManager: settingsManager,
        scaffoldKey: scaffoldKey,
      ),
    ),
  );
}

class HaboApp extends StatelessWidget {
  final AppRouter appRouter;
  final SettingsManager settingsManager;
  final GlobalKey<ScaffoldMessengerState> scaffoldKey;

  const HaboApp({
    super.key,
    required this.appRouter,
    required this.settingsManager,
    required this.scaffoldKey,
  });

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Habo',
      scaffoldMessengerKey: scaffoldKey,
      debugShowCheckedModeBanner: false,
      theme: HaboTheme.lightTheme(),
      darkTheme: HaboTheme.darkTheme(),
      themeMode: ThemeMode.system,
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      routerDelegate: appRouter,
      routeInformationParser: HaboRouteInformationParser(),
    );
  }
}
