import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../../shared/presentation/pages/not_found_page.dart';
import '../../features/settings/presentation/pages/settings_page.dart';
import '../../features/settings/presentation/pages/profile_edit_page.dart';
import '../../features/settings/presentation/pages/help_page.dart';
import '../../features/auth/presentation/pages/login_page.dart';
import '../../features/auth/presentation/pages/signup_page.dart';
import '../../features/dashboard/presentation/pages/student_dashboard_page.dart';
import '../../features/dashboard/presentation/pages/teacher_dashboard_page.dart';
import '../../features/ai_tutor/presentation/pages/tutor_chat_page_simple.dart';
import '../../features/ai_tutor/presentation/pages/chat_history_page.dart';
import '../../features/quiz/presentation/pages/quiz_list_page.dart';
import '../../features/quiz/presentation/pages/quiz_page.dart';
import '../../features/quiz/presentation/pages/teacher_quiz_page.dart';
import '../../features/profile/presentation/pages/profile_page.dart';
import '../../features/splash/presentation/pages/splash_page.dart';
import '../../features/auth/presentation/pages/role_selection_page.dart';
import '../../features/analytics/presentation/pages/analytics_page.dart';
import '../../features/achievements/presentation/pages/student_achievements_page.dart';
import '../../features/quiz/presentation/pages/quiz_management_page.dart';

/// App Router Configuration
class AppRouter {
  static const String splash = '/splash';
  static const String roleSelection = '/role-selection';
  static const String login = '/login';
  static const String signup = '/signup';
  static const String studentDashboard = '/student-dashboard';
  static const String teacherDashboard = '/teacher-dashboard';
  static const String tutor = '/ai-tutor';
  static const String quizList = '/quiz';
  static const String quiz = '/quiz/:quizId';
  static const String profile = '/profile';
  static const String settings = '/settings';
  static const String teacherAnalytics = '/teacher/analytics';
  static const String studentAchievements = '/student/achievements';
  static const String quizManagement = '/quiz-management';
  static const String chatHistory = '/chat-history';

  // Nested route paths (without leading slash)
  static const String tutorNested = 'tutor';
  static const String quizListNested = 'quiz';
  static const String quizNested = 'quiz/:quizId';

  static final GoRouter router = GoRouter(
    initialLocation: splash,
    debugLogDiagnostics: true,
    errorBuilder: (context, state) => const NotFoundPage(),

    routes: [
      // Splash Screen
      GoRoute(
        path: splash,
        name: 'splash',
        builder: (context, state) => const SplashPage(),
      ),

      // Role Selection
      GoRoute(
        path: roleSelection,
        name: 'roleSelection',
        builder: (context, state) => const RoleSelectionPage(),
      ),

      // Login
      GoRoute(
        path: login,
        name: 'login',
        builder: (context, state) =>
            LoginPage(selectedRole: state.uri.queryParameters['role']),
      ),

      // Signup
      GoRoute(
        path: signup,
        name: 'signup',
        builder: (context, state) =>
            SignupPage(selectedRole: state.uri.queryParameters['role']),
      ),

      // Student Dashboard
      GoRoute(
        path: studentDashboard,
        name: 'studentDashboard',
        builder: (context, state) => const StudentDashboardPage(),
      ),

      // Teacher Dashboard
      GoRoute(
        path: teacherDashboard,
        name: 'teacherDashboard',
        builder: (context, state) => const TeacherDashboardPage(),
        routes: [
          // Nested routes for teacher features
          GoRoute(
            path: tutorNested,
            name: 'teacherTutor',
            builder: (context, state) => const TutorChatPageSimple(),
          ),
          GoRoute(
            path: quizListNested,
            name: 'teacherQuizList',
            builder: (context, state) => const QuizListPage(),
          ),
          GoRoute(
            path: quizNested,
            name: 'teacherQuiz',
            builder: (context, state) {
              final quizId = state.pathParameters['quizId']!;
              return QuizPage(quizId: quizId);
            },
          ),
        ],
      ),

      // Profile Route
      GoRoute(
        path: profile,
        name: 'profile',
        builder: (context, state) => const ProfilePage(),
      ),

      // Settings Route
      GoRoute(
        path: settings,
        name: 'settings',
        builder: (context, state) => const SettingsPage(),
        routes: [
          // Profile Edit Route
          GoRoute(
            path: 'profile-edit',
            name: 'profileEdit',
            builder: (context, state) => const ProfileEditPage(),
          ),
          // Help Route
          GoRoute(
            path: 'help',
            name: 'help',
            builder: (context, state) => const HelpPage(),
          ),
        ],
      ),

      // AI Tutor Route with conversation ID (resume conversation) - MUST come before regular /ai-tutor route
      GoRoute(
        path: '$tutor/:conversationId',
        name: 'aiTutorConversation',
        builder: (context, state) {
          final conversationId = state.pathParameters['conversationId']!;
          return TutorChatPageSimple(conversationId: conversationId);
        },
      ),

      // AI Tutor Route (independent - new conversation)
      GoRoute(
        path: tutor,
        name: 'aiTutor',
        builder: (context, state) => const TutorChatPageSimple(),
      ),

      // Chat History Route
      GoRoute(
        path: chatHistory,
        name: 'chatHistory',
        builder: (context, state) => const ChatHistoryPage(),
      ),

      // Teacher Analytics Route
      GoRoute(
        path: teacherAnalytics,
        name: 'teacherAnalytics',
        builder: (context, state) => const AnalyticsPage(),
      ),

      // Student Achievements Route
      GoRoute(
        path: studentAchievements,
        name: 'studentAchievements',
        builder: (context, state) => const StudentAchievementsPage(),
      ),

      // Quiz Management Route
      GoRoute(
        path: quizManagement,
        name: 'quizManagement',
        builder: (context, state) => const QuizManagementPage(),
      ),

      // Quiz List Route (independent — student access)
      GoRoute(
        path: quizList,
        name: 'quizList',
        builder: (context, state) => const QuizListPage(),
      ),

      // Teacher Quiz Create Route
      GoRoute(
        path: '/teacher/quiz/create',
        name: 'teacherQuizCreate',
        builder: (context, state) => const TeacherQuizPage(),
      ),
    ],

    // Let splash screen handle all navigation logic
    redirect: (context, state) {
      // Allow all routes - splash screen will handle authentication and navigation
      return null;
    },
  );

  // Navigation helpers
  static void goToLogin(BuildContext context) {
    context.go(login);
  }

  static void goToSignup(BuildContext context) {
    context.go(signup);
  }

  static void goToStudentDashboard(BuildContext context) {
    context.go(studentDashboard);
  }

  static void goToTeacherDashboard(BuildContext context) {
    context.go(teacherDashboard);
  }

  static void goToTutor(BuildContext context) {
    context.go(tutor);
  }

  static void goToQuizList(BuildContext context) {
    context.go(quizList);
  }

  static void goToQuiz(BuildContext context, String quizId) {
    context.go('$quiz/$quizId');
  }

  static void goToProfile(BuildContext context) {
    context.go(profile);
  }

  static void goToSettings(BuildContext context) {
    context.go(settings);
  }

  static void goBack(BuildContext context) {
    context.pop();
  }

  static void replaceWith(BuildContext context, String location) {
    context.go(location);
  }
}
