import 'package:comwell_key_app/authentication/authentication_repository.dart';
import 'package:comwell_key_app/presentation/screens/check_in/check_in_route.dart';
import 'package:comwell_key_app/check_out/checkout_routes.dart';
import 'package:comwell_key_app/choose_share_room/choose_share_room_route.dart';
import 'package:comwell_key_app/hotel_information/hotel_information_route.dart';
import 'package:comwell_key_app/housekeeping/house_keeping_route.dart';
import 'package:comwell_key_app/my_booking/my_booking_route.dart';
import 'package:comwell_key_app/presentation/screens/pregistration/preregistration_route.dart';
import 'package:comwell_key_app/presentation/screens/booking_details/booking_details_route.dart';
import 'package:comwell_key_app/presentation/screens/change_password/change_password_route.dart';
import 'package:comwell_key_app/presentation/screens/notifications/notifications_route.dart';
import 'package:comwell_key_app/presentation/screens/payment_processing/payment_processing_route.dart';
import 'package:comwell_key_app/presentation/screens/permission_overview/permission_overview_route.dart';
import 'package:comwell_key_app/presentation/screens/profile/profile_route.dart';
import 'package:comwell_key_app/presentation/screens/profile_settings/profile_settings_route.dart';
import 'package:comwell_key_app/presentation/screens/room_info/room_info_route.dart';
import 'package:comwell_key_app/contact/contact_route.dart';
import 'package:comwell_key_app/find_booking/find_booking_route.dart';
import 'package:comwell_key_app/force_update/force_update_route.dart';
import 'package:comwell_key_app/key/key_route.dart';
import 'package:comwell_key_app/overview/overview_route.dart';
import 'package:comwell_key_app/presentation/screens/past_cancelled_booking_details/past_cancelled_booking_detail_route.dart';
import 'package:comwell_key_app/presentation/screens/webview/webview_route.dart';
import 'package:comwell_key_app/routing/app_routes.dart';
import 'package:comwell_key_app/routing/go_router_observer.dart';
import 'package:comwell_key_app/share/share_booking_route.dart';
import 'package:comwell_key_app/up_sales/up_sales_route.dart';
import 'package:comwell_key_app/utils/context_utils.dart';
import 'package:comwell_key_app/utils/firebase.dart';
import 'package:concierge/concierge_route.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter/material.dart';
import '../presentation/screens/login/login_route.dart';
import '../presentation/screens/onboarding/onboarding_routes.dart';
import '../presentation/screens/received_shared_booking/received_shared_booking_route.dart';
import '../utils/locator.dart';

final _rootNavigatorKey = GlobalKey<NavigatorState>();
final rootNavigatorKey = _rootNavigatorKey;

// These routes should not check if user is logged in
final authExceptions = [AppRoutes.webview, AppRoutes.login];

final router = GoRouter(
  initialLocation: AppRoutes.overview,
  navigatorKey: _rootNavigatorKey,
  debugLogDiagnostics: true,
  observers: [GoRouterObserver(), firebaseAnalyticsObserver],
  redirect: (context, state) {
    final authRepo = locator<AuthenticationRepository>();
    final isLoggedIn = authRepo.isLoggedIn;
    final isException = authExceptions.contains(state.matchedLocation);

    if (isException) {
      return null;
    }

    if (!isLoggedIn) {
      if (state.matchedLocation == AppRoutes.login) {
        return null;
      } else {
        return LoginRoute(redirectAfterLogin: state.uri.toString()).location;
      }
    }

    if (!context.prefs.onboardingHasSeenUsageTracking) {
      return const UsageTrackingPermissionRoute().location;
    }

    if (!context.prefs.onboardingHasSeenNotification) {
      return const NotificationPermissionRoute().location;
    }

    if (!context.prefs.onboardingHasSeenBluetooth) {
      return const BluetoothPermissionRoute(isOnboarding: true).location;
    }

    return null;
  },
  routes: <RouteBase>[
    $loginRoute,
    $bluetoothPermissionRoute,
    $notificationPermissionRoute,
    $usageTrackingPermissionRoute,
    $internetDisabledRoute,
    $permissionOverviewRoute,
    $profileSettingsRoute,
    $profileRoute,
    $paymentCardsRoute,
    $paymentProcessingRoute,
    $receivedSharedBookingRoute,
    overviewRoute,
    changePasswordRoute,
    webviewRoute,
    pastCancelledBookingDetailsRoute,
    bookingDetailsRoute,
    keyRoute,
    checkInRoute,
    checkInPaymentRoute,
    chooseShareRoomRoute,
    contactRoute,
    findBookingRoute,
    shareBookingRoute,
    hotelInformationRoute,
    forceUpdateRoute,
    bookingDetailsWithIdRoute,
    findBookingLoadingRoute,
    myBookingRoute,
    preregistrationRoute,
    houseKeepingRoute,
    $notificationsRoute,
    ...checkOutRoutes,
    upSalesRoute,
    roomInfoRoute,
    conciergeShellRoute
  ],
);