// import 'dart:developer';

// import 'package:flutter/material.dart';
// import 'package:flutter/services.dart';
// import 'package:flutter_riverpod/flutter_riverpod.dart';
// import 'package:flutter_secure_storage/flutter_secure_storage.dart';
// import 'package:slrpg_app/src/shared/data/remote/secure_auth_store.dart';
// import 'src/app.dart';
// import 'src/shared/data/local/app_database.dart';
// import 'src/shared/data/remote/api_client.dart';
// import 'src/shared/data/remote/pb_auth_store.dart';
// import 'package:flutter_dotenv/flutter_dotenv.dart';

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

//   try {
//     await dotenv.load(fileName: '.env');
//     log('Environment loaded: ${dotenv.env['ENVIRONMENT']}');
//     log('API URL: ${dotenv.env['API_BASE_URL']}');
//   } catch (e) {
//     log('Could not load .env file: $e');
//     log('Using default production values');
//   }

//   await SystemChrome.setPreferredOrientations([
//     DeviceOrientation.portraitUp,
//     DeviceOrientation.portraitDown,
//   ]);

//   final database = AppDatabase();

//   const secureStorage = FlutterSecureStorage(
//       aOptions: AndroidOptions(encryptedSharedPreferences: true));
//   final authStore = PbAuthStore();
//   // final authStore = SecureAuthStore(storage: secureStorage);
//   await authStore.loadFromStorage();

//   runApp(
//     ProviderScope(
//       overrides: [
//         // Datenbank Override (wie gehabt)
//         appDatabaseProvider.overrideWithValue(database),

//         // ApiClient Override: Wir geben den BEREITS GELADENEN Store rein
//         apiClientProvider.overrideWith((ref) => ApiClient(
//             authStore: authStore, // Hier injizieren!
//             storage: secureStorage)),
//       ],
//       child: const SLRPGApp(), // Dein Root Widget (Name prüfen, falls anders)
//     ),
//   );
// // }
//   // runApp(
//   //   ProviderScope(
//   //     overrides: [
//   //       appDatabaseProvider.overrideWithValue(database),
//   //       apiClientProvider
//   //           .overrideWith((ref) => ApiClient(authStore: authStore)),
//   //     ],
//   //     child: const SLRPGApp(),
//   //   ),
//   // );
// }

// final appDatabaseProvider =
//     Provider<AppDatabase>((ref) => throw UnimplementedError());
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:slrpg_app/src/app.dart';
import 'package:slrpg_app/src/shared/data/local/app_database.dart';
import 'package:slrpg_app/src/shared/data/remote/api_client.dart';
import 'package:slrpg_app/src/shared/data/remote/pb_auth_store.dart';

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

  // 1. Env laden
  try {
    await dotenv.load(fileName: '.env');
  } catch (e) {
    log('Could not load .env file: $e');
  }

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

  final database = AppDatabase();

  // 2. Auth Store erstellen UND laden (Warten!)
  final authStore = PbAuthStore();
  await authStore.loadFromStorage(); // Das ist der entscheidende 'await'

  log("Auth loaded. Valid? ${authStore.isValid}"); // Debug Log

  // 3. App starten mit injiziertem Store
  runApp(
    ProviderScope(
      overrides: [
        appDatabaseProvider.overrideWithValue(database),
        apiClientProvider
            .overrideWith((ref) => ApiClient(authStore: authStore)),
      ],
      child: const SLRPGApp(),
    ),
  );
}

// Provider Definition für DB (falls noch nicht vorhanden)
final appDatabaseProvider =
    Provider<AppDatabase>((ref) => throw UnimplementedError());
