import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
// Firebase Core
import 'package:firebase_core/firebase_core.dart';
// Firebase Database (필요하다면)
import 'package:firebase_database/firebase_database.dart';

// firebase_options.dart 파일을 import
import 'firebase_options.dart';

import 'views/login/login_page.dart';
import 'views/room/main_page.dart';

// 모바일 광고
import 'package:google_mobile_ads/google_mobile_ads.dart';

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

  // 파이어베이스 초기화
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  // 모바일 광고 초기화
  MobileAds.instance.initialize();
  
  // RequestConfiguration configuration = RequestConfiguration(
  //   testDeviceIds: ["CF2323B16A2434EF4291F29404AD42AC"], // 여기에 자신의 기기 ID 추가
  // );
  
  // MobileAds.instance.updateRequestConfiguration(configuration);

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ALLSCORE',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: FutureBuilder<bool>(
        future: _checkLoginStatus(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Scaffold(
              body: Center(child: CircularProgressIndicator()),
            );
          } else if (snapshot.hasData && snapshot.data == true) {
            return const MainPage();
          } else {
            return const LoginPage();
          }
        },
      ),
    );
  }

  Future<bool> _checkLoginStatus() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool autoLogin = prefs.getBool('auto_login') ?? false;
    String authToken = prefs.getString('auth_token') ?? '';
    return autoLogin && authToken.isNotEmpty;
  }
}
