import 'package:go_router/go_router.dart';

import '../core/constants/app_constants.dart';
import '../features/screens.dart';
import '../shared/widgets/app_shells.dart';

final GoRouter appRouter = GoRouter(
  initialLocation: '/splash',
  redirect: (context, state) async {
    final path = state.matchedLocation;
    final serverUrl = await AppConstants.getServerUrl();

    if ((serverUrl == null || serverUrl.isEmpty) && path != '/server-connect') {
      return '/server-connect';
    }
    if (path == '/server-connect' &&
        serverUrl != null &&
        serverUrl.isNotEmpty) {
      return '/splash';
    }
    return null;
  },
  routes: [
    GoRoute(
        path: '/server-connect',
        builder: (_, __) => const ServerConnectScreen()),
    GoRoute(path: '/splash', builder: (_, __) => const SplashScreen()),
    GoRoute(path: '/login', builder: (_, __) => const LoginScreen()),
    GoRoute(path: '/register', builder: (_, __) => const RegisterScreen()),
    GoRoute(
        path: '/incoming-call', builder: (_, __) => const IncomingCallScreen()),
    ShellRoute(
      builder: (_, __, child) => UserShell(child: child),
      routes: [
        GoRoute(
            path: '/user/walkguide',
            builder: (_, __) => const WalkGuideScreen()),
        GoRoute(path: '/user/sos', builder: (_, __) => const SosScreen()),
        GoRoute(
            path: '/user/activity',
            builder: (_, __) => const ActivityLogScreen()),
        GoRoute(
            path: '/user/notifications',
            builder: (_, __) => const NotificationScreen()),
        GoRoute(
            path: '/user/navigation',
            builder: (_, __) => const NavigationModeScreen()),
        GoRoute(
            path: '/user/settings',
            builder: (_, __) => const UserSettingsScreen()),
        GoRoute(
            path: '/user/pairing',
            builder: (_, __) => const UserPairingScreen()),
        GoRoute(path: '/user/call', builder: (_, __) => const CallScreen()),
        GoRoute(
            path: '/user/benchmark',
            builder: (_, __) => const AiBenchmarkScreen()),
      ],
    ),
    ShellRoute(
      builder: (_, __, child) => GuardianShell(child: child),
      routes: [
        GoRoute(
            path: '/guardian/dashboard',
            builder: (_, __) => const GuardianDashboardScreen()),
        GoRoute(
            path: '/guardian/map',
            builder: (_, __) => const GuardianMapScreen()),
        GoRoute(
            path: '/guardian/logs',
            builder: (_, __) => const GuardianActivityLogScreen()),
        GoRoute(
            path: '/guardian/send-notif',
            builder: (_, __) => const GuardianSendNotifScreen()),
        GoRoute(
            path: '/guardian/ai-config',
            builder: (_, __) => const GuardianAiConfigScreen()),
        GoRoute(
            path: '/guardian/voice-cmd',
            builder: (_, __) => const GuardianVoiceCmdScreen()),
        GoRoute(
            path: '/guardian/shortcuts',
            builder: (_, __) => const GuardianShortcutScreen()),
        GoRoute(
            path: '/guardian/geofence',
            builder: (_, __) => const GuardianGeofenceScreen()),
        GoRoute(
            path: '/guardian/pairing',
            builder: (_, __) => const GuardianPairingScreen()),
        GoRoute(
            path: '/guardian/settings',
            builder: (_, __) => const GuardianSettingsScreen()),
        GoRoute(
            path: '/guardian/benchmark',
            builder: (_, __) => const AiBenchmarkScreen()),
      ],
    ),
  ],
);
