import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../pages/dashboard_page.dart';
import '../pages/upload_page.dart';
import '../pages/parse_compare_page.dart';
import '../pages/ai_process_page.dart';
import '../pages/traction_page.dart';
import '../pages/result_page.dart';
import '../pages/task_center_page.dart';
import '../utils/constants.dart';

/// 应用路由配置
class AppRouter {
  static const String _initialLocation = String.fromEnvironment(
    'DEFAULT_ROUTE',
    defaultValue: AppRoutes.home,
    // defaultValue: '${AppRoutes.result}/1',
  );

  static final GoRouter router = GoRouter(
    initialLocation: _initialLocation,
    routes: [
      GoRoute(
        path: AppRoutes.home,
        name: 'home',
        builder: (context, state) => const DashboardPage(),
      ),
      GoRoute(
        path: AppRoutes.tasks,
        name: 'tasks',
        builder: (context, state) => const TaskCenterPage(),
      ),
      GoRoute(
        path: AppRoutes.upload,
        name: 'upload',
        builder: (context, state) => const UploadPage(),
      ),
      GoRoute(
        path: '${AppRoutes.parse}/:id',
        name: 'parse',
        builder: (context, state) {
          final id = state.pathParameters['id']!;
          return ParseComparePage(documentId: id);
        },
      ),
      GoRoute(
        path: '${AppRoutes.process}/:id',
        name: 'process',
        builder: (context, state) {
          final id = state.pathParameters['id']!;
          return AIProcessPage(documentId: id);
        },
      ),
      GoRoute(
        path: '${AppRoutes.traction}/:id',
        name: 'traction',
        builder: (context, state) {
          final id = state.pathParameters['id']!;
          return TractionPage(documentId: id);
        },
      ),
      GoRoute(
        path: '${AppRoutes.result}/:id',
        name: 'result',
        builder: (context, state) {
          final id = state.pathParameters['id']!;
          return ResultPage(documentId: id);
        },
      ),
    ],
    errorBuilder: (context, state) => Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(Icons.error_outline, size: 64, color: Colors.red),
            const SizedBox(height: 16),
            Text('页面未找到: ${state.uri}'),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: () => context.go(AppRoutes.home),
              child: const Text('返回首页'),
            ),
          ],
        ),
      ),
    ),
  );
}
