import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

import '../../features/home/pages/home_page.dart';
import '../../features/programs/pages/programs_page.dart';
import '../../features/program_detail/pages/program_detail_page.dart';
import '../../features/settings/pages/settings_page.dart';
import '../../features/home/pages/complete_page.dart';

/// 应用路由配置
final goRouterProvider = Provider<GoRouter>((ref) {
  return GoRouter(
    initialLocation: '/',
    routes: [
      GoRoute(
        path: '/',
        name: 'home',
        builder: (context, state) {
          // 支持 ?tab=N 查询参数,用于从其他页面跳回首页并切换到指定 tab
          final tabParam = state.uri.queryParameters['tab'];
          final initialTab = int.tryParse(tabParam ?? '') ?? 0;
          return HomePage(initialTab: initialTab);
        },
      ),
      GoRoute(
        path: '/programs',
        name: 'programs',
        builder: (context, state) => const ProgramsPage(),
      ),
      GoRoute(
        path: '/programs/:id',
        name: 'programDetail',
        builder: (context, state) {
          final id = state.pathParameters['id'];
          return ProgramDetailPage(programId: id ?? '');
        },
      ),
      GoRoute(
        path: '/settings',
        name: 'settings',
        builder: (context, state) => const SettingsPage(),
      ),
      GoRoute(
        path: '/complete',
        name: 'complete',
        builder: (context, state) => const CompletePage(),
      ),
    ],
  );
});