import 'package:account/account.dart';
import 'package:auth/auth.dart';
import 'package:functions/functions.dart';
import 'package:customer_service/customer_service.dart';
import 'package:legacy_dashboard_shell/legacy_dashboard_builder.dart';
import 'package:dashboard_shell/dashboard_builder.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:go_router/go_router.dart';
import 'package:hub/hub.dart';
import 'package:home/home.dart';
import 'package:navigation/navigation.dart';
import 'package:notifications/notifications.dart';
import 'package:profile/profile.dart';
import 'package:splash/splash.dart';

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

late final GoRouter appRouter;

void configureAppRouter() {
  appRouter = GoRouter(
    navigatorKey: rootNavigatorKey,
    initialLocation: AppRoutes.dashboardHub,
    debugLogDiagnostics: true,
    routes: [
      GoRoute(
        path: AppRoutes.splash,
        name: 'splash',
        pageBuilder: SplashBuilder().buildPage,
      ),
      StatefulShellRoute.indexedStack(
        builder: (context, state, navShell) {
          return LegacyDashboardBuilder().build(context, navShell);
        },
        branches: [
          StatefulShellBranch(
            routes: [
              GoRoute(
                path: AppRoutes.dashboardHub,
                name: 'hub',
                pageBuilder: const HubBuilder().buildPage,
              ),
            ],
          ),
          StatefulShellBranch(
            routes: [
              GoRoute(
                path: AppRoutes.dashboardFunctions,
                name: 'functions',
                pageBuilder: FunctionsBuilder().buildPage,
              ),
            ],
          ),
        ],
      ),
      GoRoute(
        path: AppRoutes.contacts,
        name: 'contacts',
        pageBuilder: ContactsBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.remoteConnection,
        name: 'remote_connection',
        pageBuilder: RemoteConnectionBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.locateDevice,
        name: 'locate_device',
        pageBuilder: LocateDeviceBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.activityMeter,
        name: 'activity_meter',
        pageBuilder: ActivityMeterBuilder().buildPage,
      ),

      GoRoute(
        path: AppRoutes.accountSettings,
        name: 'account_settings',
        pageBuilder: AccountSettingsBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.personalData,
        name: 'personal_data',
        pageBuilder: PersonalDataBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.changePassword,
        name: 'change_password',
        pageBuilder: ChangePasswordBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.linkedDevices,
        name: 'linked_devices',
        pageBuilder: LinkedDevicesBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.appUsers,
        name: 'app_users',
        pageBuilder: AppUsersBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.deleteAccount,
        name: 'delete_account',
        pageBuilder: DeleteAccountBuilder().buildPage,
      ),

      GoRoute(
        path: AppRoutes.customerService,
        name: 'customer_service',
        pageBuilder: CustomerServiceBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.login,
        name: 'login',
        pageBuilder: LoginBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.signup,
        name: 'signup',
        pageBuilder: SignupBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.onboarding,
        name: 'onboarding',
        pageBuilder: OnboardingBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.linkPhone,
        name: 'request_link_phone',
        pageBuilder: RequestLinkPhoneBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.phoneCode,
        name: 'Verify_link_phone_code',
        pageBuilder: VerifyLinkPhoneCodeBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.recoverPassword,
        name: 'recover_password',
        pageBuilder: RequestRecoveryBuilder().buildPage,
      ),
      GoRoute(
        path: AppRoutes.deviceSignup,
        name: 'device_signup',
        pageBuilder: DeviceSignupBuilder().buildPage,
      ),
      StatefulShellRoute.indexedStack(
        builder: (context, state, navShell) {
          return DashboardBuilder().build(context, navShell);
        },
        branches: [
          StatefulShellBranch(
            routes: [
              GoRoute(
                path: AppRoutes.dashboardHome,
                name: 'home',
                pageBuilder: const HomeBuilder().buildPage,
              ),
            ],
          ),
          StatefulShellBranch(
            routes: [
              GoRoute(
                path: AppRoutes.dashboardActivity,
                name: 'activity',
                pageBuilder: const ActivityBuilder().buildPage,
              ),
            ],
          ),
          StatefulShellBranch(
            routes: [
              GoRoute(
                path: AppRoutes.dashboardNotifications,
                name: 'notifications',
                pageBuilder: const NotificationsBuilder().buildPage,
              ),
            ],
          ),
          StatefulShellBranch(
            routes: [
              GoRoute(
                path: AppRoutes.dashboardProfile,
                name: 'profile',
                pageBuilder: const ProfileBuilder().buildPage,
              ),
            ],
          ),
        ],
      ),
    ],
  );

  GetIt.I<NavigationContract>().setRouter(appRouter);
}
