// apps/app_sudoku/lib/main.dart
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:provider/provider.dart';

// [C] 공통 서비스
import 'package:service_api/service_api.dart';
// [A] 공통 UI 셸 (인트로 화면)
import 'package:feature_common/feature_common.dart'; 
// [B] 스도쿠 게임 (로비 화면)
import 'package:feature_game_sudoku/feature_game_sudoku.dart'; 

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await MobileAds.instance.initialize();

  runApp(
    // 🔽 [수정] ChangeNotifierProvider -> MultiProvider
    MultiProvider(
      providers: [
        // 1. 기존 ThemeNotifier
        ChangeNotifierProvider(
          // 🔽 [수정] .loadTheme() 호출 (앱 시작 시 테마 로드)
          create: (_) => ThemeNotifier(),
        ),
        
        // 🔽 [신규] SessionNotifier 추가
        ChangeNotifierProvider(
          create: (_) => SessionNotifier(),
        ),
      ],
      child: const MyApp(),
    ),
  );
}

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

  @override
  Widget build(BuildContext context) {
    // 🔽 [수정] context.watch 대신 context.select를 사용하여
    // 테마 변경 시에만 MaterialApp이 재빌드되도록 최적화
    final themeData = context.select((ThemeNotifier n) => n.currentTheme);
    final darkThemeData = context.select((ThemeNotifier n) => n.currentDarkTheme);
    final themeMode = context.select((ThemeNotifier n) => n.currentThemeMode);

    return MaterialApp(
      title: '스도쿠 게임',
      theme: themeData,
      darkTheme: darkThemeData,
      themeMode: themeMode,
      debugShowCheckedModeBanner: false,

      home: IntroScreen(
        nextScreenBuilder: (context) => const SudokuLobbyScreen(),
      ),
    );
  }
}