import 'package:auth/auth.dart';
import 'package:dashboard_shell/dashboard_builder.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:home/home.dart';

import 'package:navigation/navigation.dart';
import 'package:get_it/get_it.dart';
import 'package:notifications/notifications.dart';
import 'package:profile/profile.dart';

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

late final GoRouter appRouter;

void configureAppRouter() {
  appRouter = GoRouter(
    navigatorKey: rootNavigatorKey,
    initialLocation: '/login',
    // redirect: (context, state) {},
    routes: [
      GoRoute(
        path: '/login',
        name: 'login',
        pageBuilder: LoginBuilder().buildPage,
      ),
      GoRoute(
        path: '/onboarding',
        name: 'onboarding',
        pageBuilder: OnboardingBuilder().buildPage,
      ),
      GoRoute(
        path: '/link_phone',
        name: 'link_phone',
        pageBuilder: LinkPhoneBuilder().buildPage,
      ),
      GoRoute(
        path: '/phone_code',
        name: 'phone_code',
        pageBuilder: PhoneCodeBuilder().buildPage,
      ),
      GoRoute(
        path: '/recover_password',
        name: 'recover_password',
        pageBuilder: RecoverPasswordBuilder().buildPage,
      ),
      StatefulShellRoute.indexedStack(
        builder: (context, state, navShell) {
          return DashboardBuilder().build(context, navShell);
        },
        branches: [
          StatefulShellBranch(
            routes: [
              GoRoute(
                path: '/main/home',
                name: 'home',
                pageBuilder: const HomeBuilder().buildPage,
              ),
            ],
          ),
          StatefulShellBranch(
            routes: [
              GoRoute(
                path: '/main/activity',
                name: 'activity',
                pageBuilder: const ActivityBuilder().buildPage,
              ),
            ],
          ),
          StatefulShellBranch(
            routes: [
              GoRoute(
                path: '/main/notifications',
                name: 'notifications',
                pageBuilder: const NotificationsBuilder().buildPage,
              ),
            ],
          ),
          StatefulShellBranch(
            routes: [
              GoRoute(
                path: '/main/profile',
                name: 'profile',
                pageBuilder: const ProfileBuilder().buildPage,
              ),
            ],
          ),
        ],
      ),
    ],
  );

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