import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'core/theme/app_colors.dart';
import 'services/app_provider.dart';
import 'features/splash/splash_screen.dart';
import 'features/onboarding/onboarding_screen.dart';
import 'features/auth/login_screen.dart';
import 'features/auth/register_screen.dart';
import 'features/home/home_shell.dart';
import 'features/chat/chat_screen.dart';
import 'features/voice_call/voice_call_screen.dart';
import 'features/documents/document_screen.dart';
import 'features/rights/rights_cards_screen.dart';
import 'features/rights/rights_card_detail_screen.dart';
import 'features/tools/glossary_screen.dart';
import 'features/tools/cost_estimator_screen.dart';
import 'features/tools/statute_check_screen.dart';
import 'features/tools/contract_draft_screen.dart';
import 'features/tools/law_search_screen.dart';
import 'features/profile/profile_screen.dart';
import 'features/profile/edit_profile_screen.dart';
import 'features/profile/change_password_screen.dart';
import 'features/settings/settings_screen.dart';
import 'features/bookmarks/bookmarks_screen.dart';
import 'features/reminders/reminders_screen.dart';
import 'features/emergency/emergency_screen.dart';
import 'features/intake/intake_screen.dart';
import 'features/stats/stats_screen.dart';
import 'features/legal/legal_screen.dart';
import 'main.dart' show navigatorKey;

class ReasonableDoubtApp extends StatelessWidget {
  const ReasonableDoubtApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
      title: 'Reasonable Doubt',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        brightness: Brightness.dark,
        scaffoldBackgroundColor: AppColors.navy,
        fontFamily: 'Inter',
        colorScheme: ColorScheme.dark(
          primary: AppColors.royalBlue,
          secondary: AppColors.gold,
          surface: AppColors.navy,
        ),
        appBarTheme: const AppBarTheme(
          backgroundColor: Colors.transparent,
          elevation: 0,
        ),
      ),
      initialRoute: '/',
      onGenerateRoute: (settings) {
        Widget page;
        switch (settings.name) {
          case '/':
            page = const SplashScreen();
          case '/onboarding':
            page = const OnboardingScreen();
          case '/login':
            page = const LoginScreen();
          case '/register':
            page = const RegisterScreen();
          case '/home':
            page = const HomeShell();
          case '/chat':
            final args = settings.arguments as Map<String, dynamic>?;
            page = ChatScreen(
              conversationId: args?['conversationId'],
              initialMessage: args?['initialMessage'],
            );
          case '/voice-call':
            page = const VoiceCallScreen();
          case '/documents':
            page = Scaffold(
              backgroundColor: const Color(0xFF0A1628),
              appBar: AppBar(
                title: const Text('Documents', style: TextStyle(color: Colors.white)),
                backgroundColor: Colors.transparent,
                elevation: 0,
                iconTheme: const IconThemeData(color: Colors.white),
              ),
              body: const DocumentScreen(),
            );
          case '/rights':
            page = const RightsCardsScreen();
          case '/glossary':
            page = const GlossaryScreen();
          case '/cost-estimator':
            page = const CostEstimatorScreen();
          case '/profile':
            page = const ProfileScreen();
          case '/settings':
            page = const SettingsScreen();
          case '/bookmarks':
            page = const BookmarksScreen();
          case '/reminders':
            page = const RemindersScreen();
          case '/statute-check':
            page = const StatuteCheckScreen();
          case '/contract-draft':
            page = const ContractDraftScreen();
          case '/law-search':
            page = const LawSearchScreen();
          case '/emergency':
            page = const EmergencyScreen();
          case '/intake':
            page = const IntakeScreen();
          case '/edit-profile':
            page = const EditProfileScreen();
          case '/change-password':
            page = const ChangePasswordScreen();
          case '/stats':
            page = const StatsScreen();
          case '/terms':
            page = const LegalScreen(showTerms: true);
          case '/privacy':
            page = const LegalScreen(showTerms: false);
          case '/rights-detail':
            final args = settings.arguments as Map<String, dynamic>?;
            page = RightsCardDetailScreen(cardId: args?['cardId']?.toString() ?? '');
          default:
            page = const SplashScreen();
        }
        return PageRouteBuilder(
          settings: settings,
          pageBuilder: (_, __, ___) => page,
          transitionsBuilder: (context, animation, secondaryAnimation, child) {
            return FadeTransition(
              opacity: CurvedAnimation(parent: animation, curve: Curves.easeOut),
              child: SlideTransition(
                position: Tween<Offset>(
                  begin: const Offset(0, 0.04),
                  end: Offset.zero,
                ).animate(CurvedAnimation(parent: animation, curve: Curves.easeOutCubic)),
                child: child,
              ),
            );
          },
          transitionDuration: const Duration(milliseconds: 350),
        );
      },
    );
  }
}
