import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:provider/provider.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'app.dart';
import 'core/theme/theme_provider.dart';
import 'data/repositories/session_repository.dart';
import 'services/target_detection_service.dart';
import 'services/score_calculator_service.dart';
import 'services/grouping_analyzer_service.dart';
import 'services/image_processing_service.dart';
import 'features/session/session_provider.dart';

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

  // Initialize date formatting for French locale
  await initializeDateFormatting('fr_FR', null);

  // Initialize FFI for desktop platforms
  if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
  }

  FlutterError.onError = (FlutterErrorDetails details) {
    FlutterError.presentError(details);
  };

  runApp(
    MultiProvider(
      providers: [
        Provider<ImageProcessingService>(
          create: (_) => ImageProcessingService(),
        ),
        Provider<TargetDetectionService>(
          create: (context) => TargetDetectionService(
            imageProcessingService: context.read<ImageProcessingService>(),
          ),
        ),
        Provider<ScoreCalculatorService>(
          create: (_) => ScoreCalculatorService(),
        ),
        Provider<GroupingAnalyzerService>(
          create: (_) => GroupingAnalyzerService(),
        ),
        Provider<SessionRepository>(create: (_) => SessionRepository()),
        ChangeNotifierProvider<ThemeProvider>(create: (_) => ThemeProvider()),
        ChangeNotifierProvider<SessionProvider>(create: (_) => SessionProvider()),
      ],
      child: const BullyApp(),
    ),
  );
}
