// ===========================================================================
// FILE: lib/main.dart
// ===========================================================================

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:provider/provider.dart';
import 'core/theme_manager.dart';
import 'logic/game_controller.dart';
import 'ui/home/home_screen.dart';
import 'services/storage_service.dart';
import 'services/audio_service.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'firebase_options.dart';
import 'package:firebase_app_check/firebase_app_check.dart';

// --- IMPORT PER IL SUPPORTO MULTILINGUA ---
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:tetraq/l10n/app_localizations.dart';

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

  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  await FirebaseAppCheck.instance.activate(
    androidProvider: kDebugMode ? AndroidProvider.debug : AndroidProvider.playIntegrity,
    appleProvider: kDebugMode ? AppleProvider.debug : AppleProvider.deviceCheck,
  );

  try {
    await FirebaseAuth.instance.signInAnonymously();
  } catch (e) {
    debugPrint("Errore Auth: $e");
  }

  await StorageService.instance.init();
  await AudioService.instance.init();

  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => ThemeManager()),
        ChangeNotifierProvider(create: (_) => GameController()),
      ],
      child: const TetraQApp(),
    ),
  );
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'TetraQ',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        fontFamily: 'Roboto',
        useMaterial3: true,
      ),

      // --- BIVIO DELLE LINGUE ATTIVATO! ---
      // Flutter si occuperà di caricare automaticamente tutte le lingue
      // che hai generato tramite lo script.
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      // ------------------------------------

      home: const HomeScreen(),
    );
  }
}