import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';
import 'package:sigap/app.dart';
import 'package:sigap/navigation_menu.dart';
import 'package:sigap/src/features/personalization/presentasion/bindings/personalization_bindings.dart';
import 'package:sigap/src/utils/theme/theme.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

Future<void> main() async {
  // Make sure to initialize bindings first
  WidgetsFlutterBinding.ensureInitialized();

  // Register navigation controller early since it's needed for NavigationMenu
  Get.put(NavigationController(), permanent: true);

  // Make sure status bar is properly set
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(statusBarColor: Colors.transparent),
  );

  // FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);

  // Load environment variables from the .env file
  await dotenv.load(fileName: ".env");

  // -- GetX Local Storage
  await GetStorage.init();

  // Initialize the authentication repository with Supabase
  await Supabase.initialize(
    url: dotenv.env['SUPABASE_URL'] ?? '',
    anonKey: dotenv.env['SUPABASE_ANON_KEY'] ?? '',
    authOptions: const FlutterAuthClientOptions(
      authFlowType: AuthFlowType.pkce,
      // detectSessionInUri: true,
    ),
    realtimeClientOptions: RealtimeClientOptions(
      logLevel: RealtimeLogLevel.info,
    ),
    storageOptions: const StorageClientOptions(retryAttempts: 10),
  );

  // Initialize the Mapbox
  String mapboxAccesToken = dotenv.env['MAPBOX_ACCESS_TOKEN'] ?? '';

  MapboxOptions.setAccessToken(mapboxAccesToken);

  runApp(const App());
}

