import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../screens/main/main_screen.dart';
import '../screens/discovery/discovery_screen.dart';
import '../screens/library/library_screen.dart';
import '../screens/control/control_screen.dart';
import '../screens/control/free_control_screen.dart';
import '../screens/control/pattern_control_screen.dart';
import '../screens/profile/profile_screen.dart';
import '../screens/profile/settings_screen.dart';
import '../screens/profile/topup_screen.dart';
import '../screens/profile/device_manager_screen.dart';
import '../screens/profile/subscription_screen.dart';
import '../screens/profile/privacy_screen.dart';
import '../screens/profile/help_screen.dart';
import '../screens/interaction/interaction_screen.dart';
import '../screens/player/script_player_screen.dart';

// Private navigators
final _rootNavigatorKey = GlobalKey<NavigatorState>();
final _shellNavigatorDiscoveryKey = GlobalKey<NavigatorState>(debugLabel: 'shellDiscovery');
final _shellNavigatorLibraryKey = GlobalKey<NavigatorState>(debugLabel: 'shellLibrary');
final _shellNavigatorControlKey = GlobalKey<NavigatorState>(debugLabel: 'shellControl');
final _shellNavigatorProfileKey = GlobalKey<NavigatorState>(debugLabel: 'shellProfile');

final appRouter = GoRouter(
  navigatorKey: _rootNavigatorKey,
  initialLocation: '/discovery',
  routes: [
    // Top-level route for Interaction to cover BottomNav
    GoRoute(
      path: '/interaction/:characterId',
      parentNavigatorKey: _rootNavigatorKey, 
      builder: (context, state) {
        final characterId = state.pathParameters['characterId']!;
        return InteractionScreen(characterId: characterId);
      },
    ),
    // Top-level route for Script Player to cover BottomNav
    GoRoute(
      path: '/player/:scenarioId',
      parentNavigatorKey: _rootNavigatorKey,
      builder: (context, state) {
        final scenarioId = state.pathParameters['scenarioId']!;
        return ScriptPlayerScreen(scenarioId: scenarioId);
      },
    ),
    // Top-level route for Free Control to cover BottomNav
    GoRoute(
      path: '/control/free',
      parentNavigatorKey: _rootNavigatorKey,
      builder: (context, state) => const FreeControlScreen(),
    ),
    // Top-level route for Pattern Control to cover BottomNav
    GoRoute(
      path: '/control/pattern',
      parentNavigatorKey: _rootNavigatorKey,
      builder: (context, state) => const PatternControlScreen(),
    ),
    // Profile sub-pages
    GoRoute(
      path: '/profile/settings',
      parentNavigatorKey: _rootNavigatorKey,
      builder: (context, state) => const SettingsScreen(),
    ),
    GoRoute(
      path: '/profile/topup',
      parentNavigatorKey: _rootNavigatorKey,
      builder: (context, state) => const TopupScreen(),
    ),
    GoRoute(
      path: '/profile/device',
      parentNavigatorKey: _rootNavigatorKey,
      builder: (context, state) => const DeviceManagerScreen(),
    ),
    GoRoute(
      path: '/profile/subscription',
      parentNavigatorKey: _rootNavigatorKey,
      builder: (context, state) => const SubscriptionScreen(),
    ),
    GoRoute(
      path: '/profile/privacy',
      parentNavigatorKey: _rootNavigatorKey,
      builder: (context, state) => const PrivacyScreen(),
    ),
    GoRoute(
      path: '/profile/help',
      parentNavigatorKey: _rootNavigatorKey,
      builder: (context, state) => const HelpScreen(),
    ),
    StatefulShellRoute.indexedStack(
      builder: (context, state, navigationShell) {
        return MainScreen(navigationShell: navigationShell);
      },
      branches: [
        StatefulShellBranch(
          navigatorKey: _shellNavigatorDiscoveryKey,
          routes: [
            GoRoute(
              path: '/discovery',
              builder: (context, state) => const DiscoveryScreen(),
            ),
          ],
        ),
        StatefulShellBranch(
          navigatorKey: _shellNavigatorLibraryKey,
          routes: [
            GoRoute(
              path: '/library',
              builder: (context, state) => const LibraryScreen(),
            ),
          ],
        ),
        StatefulShellBranch(
          navigatorKey: _shellNavigatorControlKey,
          routes: [
            GoRoute(
              path: '/control',
              builder: (context, state) => const ControlScreen(),
            ),
          ],
        ),
        StatefulShellBranch(
          navigatorKey: _shellNavigatorProfileKey,
          routes: [
            GoRoute(
              path: '/profile',
              builder: (context, state) => const ProfileScreen(),
            ),
          ],
        ),
      ],
    ),
  ],
);
