import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:injectable/injectable.dart';

import 'common/utils/fcm_service.dart';
import 'injection.dart';
import 'presentation/app_widget.dart';

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

  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.dark,
    ),
  );

  if (kReleaseMode) {
    debugPrint = (message, {wrapWidth}) => '';
  }

  await configureDependencies(
    kReleaseMode ? Environment.prod : Environment.dev,
  );

  // Setup Crashlytics
  FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
  PlatformDispatcher.instance.onError = (error, stack) {
    FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
    return true;
  };

  // Initialize FCM after dependencies are ready
  try {
    await getIt<FcmService>().initialize(
      onMessageTap: (message) {
        debugPrint('[FCM] Navigate based on: ${message.data}');
      },
    );
  } catch (e) {
    debugPrint('[FCM] Initialization failed: $e');
  }

  runApp(const AppWidget());
}
