import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:provider/provider.dart';
import 'services/app_provider.dart';
import 'services/notification_service.dart';
import 'app.dart';

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
}

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
    statusBarIconBrightness: Brightness.light,
    systemNavigationBarColor: Color(0xFF0A1628),
  ));

  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

  NotificationService.navigatorKey = navigatorKey;
  await NotificationService.initialize();

  final provider = AppProvider();
  await provider.init();
  
  // Set the shared API instance for notifications after auth is ready
  NotificationService.setApi(provider.api);

  runApp(
    ChangeNotifierProvider.value(
      value: provider,
      child: const ReasonableDoubtApp(),
    ),
  );
}
