import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:firebase_core/firebase_core.dart';
import 'dart:developer';
import 'screens/splash_screen.dart';
import 'screens/register_complete_screen.dart';
import 'utils/log_manager.dart';

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

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

  // 글로벌 에러 핸들링
  FlutterError.onError = (FlutterErrorDetails details) {
    FlutterError.presentError(details);
    LogManager().addLog('[APP ERROR] ${details.exception}');
  };

  PlatformDispatcher.instance.onError = (error, stack) {
    LogManager().addLog('[Uncaught Error] $error');
    return true;
  };

  try {
    await Firebase.initializeApp();
  } catch (e) {
    log('Firebase initialization failed: $e');
    LogManager().addLog('[Firebase Init Error] $e');
  }
  runApp(const RupApp());
}

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

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812), // 기준 디자인 해상도 set
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return MaterialApp(
          navigatorKey: navigatorKey,
          debugShowCheckedModeBanner: false,
          home: const SplashScreen(),
          routes: {
            '/register_complete': (context) => const RegisterCompleteScreen(),
          },
        );
      },
    );
  }
}
