import 'dart:async';
import 'dart:io';
import 'dart:math';

import 'package:Intaleq/controller/functions/crud.dart';
import 'package:Intaleq/controller/payment/paymob/paymob_response.dart';
import 'package:Intaleq/views/home/HomePage/contact_us.dart';
import 'package:Intaleq/views/home/HomePage/share_app_page.dart';
import 'package:Intaleq/views/home/my_wallet/passenger_wallet.dart';
import 'package:Intaleq/views/home/profile/passenger_profile_page.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:flutter/services.dart';
import 'package:quick_actions/quick_actions.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
import 'constant/api_key.dart';
import 'constant/info.dart';
import 'constant/notification.dart';
import 'controller/firebase/firbase_messge.dart';
import 'controller/firebase/local_notification.dart';
import 'controller/functions/encrypt_decrypt.dart';
import 'controller/functions/secure_storage.dart';
import 'controller/home/deep_link_controller.dart';
import 'controller/local/local_controller.dart';
import 'controller/local/translations.dart';
import 'controller/payment/paymob/paymob_wallet.dart';
import 'firebase_options.dart';
import 'models/db_sql.dart';
import 'splash_screen_page.dart';

final box = GetStorage();
final storage = FlutterSecureStorage();
// final PaymobPayment paymobPayment = PaymobPayment();
// final PaymobPayment paymobPayment = PaymobPayment();
// final PaymobPaymentWallet paymobPaymentWallet = PaymobPaymentWallet();

DbSql sql = DbSql.instance;
// EncryptionHelper encryptionHelper = EncryptionHelper.instance;
@pragma('vm:entry-point')
Future<void> backgroundMessageHandler(RemoteMessage message) async {
  await Firebase.initializeApp();

  FirebaseMessagesController().fireBaseTitles(message);
}

void main() {
  // ⚠️ لا تستدعِ ensureInitialized هنا خارج الزون

  runZonedGuarded(() async {
    // 1) أنشئ الـ Binding في نفس الـ Zone
    WidgetsFlutterBinding.ensureInitialized();

    // 2) تهيئات خفيفة فقط قبل runApp
    await GetStorage.init();
    if (Platform.isAndroid || Platform.isIOS) {
      await Firebase.initializeApp(
          options: DefaultFirebaseOptions.currentPlatform);
      FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
      await SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);
    }
    Stripe.publishableKey = AK.publishableKey;
    Get.put(DeepLinkController(), permanent: true);

    // 3) شغّل التطبيق فوراً
    runApp(const MyApp());

    // 4) بعد أول إطار: التهيئات الثقيلة
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      // لا Wakelock عالمي — فعّله فقط أثناء الرحلة
      await WakelockPlus.enable();
      final AppInitializer initializer = AppInitializer();

      await initializer.initializeApp();
      await EncryptionHelper.initialize();

      final notificationController = Get.put(NotificationController());
      await notificationController.initNotifications();

      // أجّل طلب صلاحيات/توكن FCM
      final fcm = FirebaseMessagesController();
      await fcm.requestFirebaseMessagingPermission();
      await fcm.getNotificationSettings();
      await fcm.getToken();

      // Generate a random index to pick a message
      // Generate and schedule passenger daily notifications safely
      try {
        // 1) تأكد أن القائمة موجودة وغير فارغة
        final List<String> msgs = passengerMessages ?? const [];
        if (msgs.isEmpty) {
          // fallback افتراضي إذا ما في رسائل
          const fallbackTitle = 'Intaleq';
          const fallbackBody = 'مرحباً بك! تابع رحلاتك بأمان مع انطلق.';
          notificationController.scheduleNotificationsForSevenDays(
            fallbackTitle,
            fallbackBody,
            "tone1",
          );
        } else {
          // 2) اختَر عنصرًا عشوائيًا بأمان
          final rnd = Random();
          final idx = rnd.nextInt(msgs.length); // msgs.length > 0 أكيد
          final raw = msgs[idx];

          // 3) افصل العنوان/النص بأمان حتى لو ما في ':'
          final parts = raw.split(':');
          final title = parts.isNotEmpty ? parts.first.trim() : 'Intaleq';
          final body = parts.length > 1
              ? parts.sublist(1).join(':').trim() // يحافظ على أي ':' إضافية
              : 'مرحباً بك! تابع رحلاتك بأمان مع انطلق.';

          // 4) جدولة الإشعارات
          notificationController.scheduleNotificationsForSevenDays(
            title.isEmpty ? 'Intaleq' : title,
            body.isEmpty ? 'مرحباً بك! تابع رحلاتك بأمان مع انطلق.' : body,
            "tone1",
          );
        }
      } catch (e, st) {
        // لا تعطل التشغيل بسبب إشعار اختياري
        CRUD.addError('notif_init: $e', st.toString(), 'main');
      }

      final QuickActions quickActions = QuickActions();

      quickActions.initialize((String shortcutType) {
        // print('Activated shortcut: $shortcutType');
        if (shortcutType == 'share_app') {
          Get.toNamed('/shareApp');
        } else if (shortcutType == 'wallet') {
          Get.toNamed('/wallet');
        } else if (shortcutType == 'profile') {
          Get.toNamed('/profile');
        } else if (shortcutType == 'contact_support') {
          Get.toNamed('/contactSupport');
        }
      });
      quickActions.setShortcutItems(<ShortcutItem>[
        ShortcutItem(
          type: 'share_app',
          localizedTitle: 'Share App'.tr,
          icon: 'icon_share',
        ),
        ShortcutItem(
          type: 'wallet',
          localizedTitle: 'Wallet'.tr,
          icon: 'icon_wallet',
        ),
        ShortcutItem(
          type: 'profile',
          localizedTitle: 'Profile'.tr,
          icon: 'icon_user',
        ),
        ShortcutItem(
          type: 'contact_support',
          localizedTitle: 'Contact Support'.tr,
          icon: 'icon_support',
        ),
      ]);
    });
  }, (error, stack) {
    // بديل آمن للتجميع
    final s = error.toString();
    final ignored = s.contains('PERMISSION_DENIED') ||
        s.contains('FormatException') ||
        s.contains('Null check operator used on a null value');
    if (!ignored) CRUD.addError(s, stack.toString(), 'main');
  });
} // void main() async {
//   WidgetsFlutterBinding.ensureInitialized();
//   WakelockPlus.enable();

//   await GetStorage.init();
// // --- إضافة جديدة: تهيئة وحدة التحكم في الروابط العميقة ---
//   Get.put(DeepLinkController(), permanent: true);
//   // ----------------------------------------------------
//   final AppInitializer initializer = AppInitializer();

//   await initializer.initializeApp();
//   await EncryptionHelper.initialize();

//   NotificationController notificationController =
//       Get.put(NotificationController());

//   // Stripe.publishableKey = AK.publishableKey;
//   // if (box.read(BoxName.driverID) != null) {}

//   if (Platform.isAndroid || Platform.isIOS) {
//     await Firebase.initializeApp(
//       options: DefaultFirebaseOptions.currentPlatform,
//     );
//     await FirebaseMessagesController().requestFirebaseMessagingPermission();

//     FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);

//     List<Future> initializationTasks = [
//       FirebaseMessagesController().getNotificationSettings(),
//       FirebaseMessagesController().getToken(),
//     ];
//     // cameras = await availableCameras();
//     await Future.wait(initializationTasks);
//     SystemChrome.setPreferredOrientations([
//       DeviceOrientation.portraitUp,
//       DeviceOrientation.portraitDown,
//     ]);
//   }

//   await notificationController.initNotifications();

//   // Generate a random index to pick a message
//   final random = Random();
//   final randomMessage = messages[random.nextInt(messages.length)];

//   // Schedule the notification with the random message
//   notificationController.scheduleNotificationsForSevenDays(
//     randomMessage.split(':')[0],
//     randomMessage.split(':')[1],
//     "tone1",
//   );

//   final QuickActions quickActions = QuickActions();

//   quickActions.initialize((String shortcutType) {
//     // print('Activated shortcut: $shortcutType');
//     if (shortcutType == 'share_app') {
//       Get.toNamed('/shareApp');
//     } else if (shortcutType == 'wallet') {
//       Get.toNamed('/wallet');
//     } else if (shortcutType == 'profile') {
//       Get.toNamed('/profile');
//     } else if (shortcutType == 'contact_support') {
//       Get.toNamed('/contactSupport');
//     }
//   });
//   quickActions.setShortcutItems(<ShortcutItem>[
//     ShortcutItem(
//       type: 'share_app',
//       localizedTitle: 'Share App'.tr,
//       icon: 'icon_share',
//     ),
//     ShortcutItem(
//       type: 'wallet',
//       localizedTitle: 'Wallet'.tr,
//       icon: 'icon_wallet',
//     ),
//     ShortcutItem(
//       type: 'profile',
//       localizedTitle: 'Profile'.tr,
//       icon: 'icon_user',
//     ),
//     ShortcutItem(
//       type: 'contact_support',
//       localizedTitle: 'Contact Support'.tr,
//       icon: 'icon_support',
//     ),
//   ]);

//   runZonedGuarded<Future<void>>(() async {
//     runApp(const MyApp());
//   }, (error, stack) {
//     // ==== START: ERROR FILTER ====
//     String errorString = error.toString();

//     // Print all errors to the local debug console for development
//     print("Caught Dart error: $error");
//     print(stack);

//     // We will check if the error contains keywords for errors we want to ignore.
//     // If it's one of them, we will NOT send it to the server.
//     bool isIgnoredError = errorString.contains('PERMISSION_DENIED') ||
//         errorString.contains('FormatException') ||
//         errorString.contains('Null check operator used on a null value');

//     if (!isIgnoredError) {
//       // Only send the error to the server if it's not in our ignore list.
//       CRUD.addError(error.toString(), stack.toString(), 'main');
//     } else {
//       print("Ignoring error and not sending to server: $errorString");
//     }
//     // ==== END: ERROR FILTER ====
//   });
// }

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

  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) {
    LocaleController localController = Get.put(LocaleController());

    return GetMaterialApp(
      title: AppInformation.appName,
      translations: MyTranslation(),
      debugShowCheckedModeBanner: false,
      locale: localController.language,
      theme: localController.appTheme,
      key: UniqueKey(),

      // routes: {'/':const HomePage()},
      // home: LoginCaptin());
      initialRoute: '/',
      getPages: [
        GetPage(name: '/', page: () => SplashScreen()),
        GetPage(name: '/shareApp', page: () => ShareAppPage()),
        GetPage(name: '/wallet', page: () => PassengerWallet()),
        GetPage(name: '/profile', page: () => PassengerProfilePage()),
        GetPage(name: '/contactSupport', page: () => ContactUsPage()),
      ],
      // home: SplashScreen()
    );
  }
}
