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

import '../../features/auth/screens/login_screen.dart';
import '../../features/auth/screens/register_screen.dart';
import '../../features/learning/screens/home_screen.dart';
import '../../features/dialogue/screens/dialogue_main_screen.dart';
import '../../shared/providers/auth_provider.dart';

final routerProvider = Provider<GoRouter>((ref) {
  final authState = ref.watch(authProvider);
  
  return GoRouter(
    initialLocation: authState.isAuthenticated ? '/home' : '/login',
    redirect: (context, state) {
      final isAuthenticated = authState.isAuthenticated;
      final isAuthRoute = state.uri.path.startsWith('/auth');
      
      if (!isAuthenticated && !isAuthRoute) {
        return '/login';
      }
      
      if (isAuthenticated && isAuthRoute) {
        return '/home';
      }
      
      return null;
    },
    routes: [
      // Authentication Routes
      GoRoute(
        path: '/login',
        builder: (context, state) => const LoginScreen(),
      ),
      GoRoute(
        path: '/register',
        builder: (context, state) => const RegisterScreen(),
      ),
      
      // Main App Routes
      GoRoute(
        path: '/home',
        builder: (context, state) => const HomeScreen(),
      ),
      
      // Learning Routes
      GoRoute(
        path: '/vocabulary',
        builder: (context, state) => const Scaffold(
          body: Center(child: Text('詞彙練習頁面')),
        ),
      ),
      GoRoute(
        path: '/dialogue',
        builder: (context, state) {
          final scenarioId = state.uri.queryParameters['scenarioId'] ?? 'restaurant_001';
          final levelId = state.uri.queryParameters['levelId'] ?? 'level_001';
          final isTimeChallenge = state.uri.queryParameters['timeChallenge'] == 'true';
          
          return DialogueMainScreen(
            scenarioId: scenarioId,
            levelId: levelId,
            isTimeChallenge: isTimeChallenge,
          );
        },
      ),
      GoRoute(
        path: '/challenge',
        builder: (context, state) => const Scaffold(
          body: Center(child: Text('限時挑戰頁面')),
        ),
      ),
      
      // Profile Routes
      GoRoute(
        path: '/profile',
        builder: (context, state) => const Scaffold(
          body: Center(child: Text('個人檔案頁面')),
        ),
      ),
    ],
    
    // Error handling
    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(
              '頁面未找到',
              style: Theme.of(context).textTheme.headlineSmall,
            ),
            const SizedBox(height: 8),
            Text(
              state.error?.toString() ?? '未知錯誤',
              style: Theme.of(context).textTheme.bodyMedium,
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 24),
            ElevatedButton(
              onPressed: () => context.go('/home'),
              child: const Text('返回首頁'),
            ),
          ],
        ),
      ),
    ),
  );
});