import 'package:flutter/material.dart';
import 'package:tugas_akhir_supabase/screens/admin/admin_dashboard.dart';
import 'package:tugas_akhir_supabase/screens/admin/community_management.dart';
import 'package:tugas_akhir_supabase/screens/admin/crop_management.dart';
import 'package:tugas_akhir_supabase/screens/admin/user_management.dart';
import 'package:tugas_akhir_supabase/screens/auth/forgot_password_screen.dart';
import 'package:tugas_akhir_supabase/screens/auth/login_screen.dart';
import 'package:tugas_akhir_supabase/screens/auth/otp_screen.dart';
import 'package:tugas_akhir_supabase/screens/auth/register_screen.dart';
import 'package:tugas_akhir_supabase/screens/auth/reset_password_screen.dart';
import 'package:tugas_akhir_supabase/screens/calendar/calendar_screen.dart'
    as calendar;
import 'package:tugas_akhir_supabase/screens/calendar/field_management_screen.dart';
import 'package:tugas_akhir_supabase/screens/calendar/schedule_list_screen.dart';
import 'package:tugas_akhir_supabase/screens/calendar/schedule_detail_screen.dart'
    as detail;
import 'package:tugas_akhir_supabase/screens/community/community_screen.dart';
import 'package:tugas_akhir_supabase/screens/community/enhanced_community_screen.dart';
import 'package:tugas_akhir_supabase/screens/home_screen.dart';
import 'package:tugas_akhir_supabase/screens/image_processing/plant_scanner_screen.dart'
    as scanner;
import 'package:tugas_akhir_supabase/screens/intro/animation_splash_screen.dart'
    as splash;
import 'package:tugas_akhir_supabase/screens/intro/intro_page_screen.dart'
    as intro;
import 'package:tugas_akhir_supabase/screens/panen/analisis_chart_screen.dart'
    as chart;
import 'package:tugas_akhir_supabase/screens/panen/analisis_hasil_screen.dart'
    as hasil;
import 'package:tugas_akhir_supabase/screens/panen/analisis_input_screen.dart'
    as input;
import 'package:tugas_akhir_supabase/screens/panen/analisis_panen_screen.dart'
    as panen;
import 'package:tugas_akhir_supabase/screens/profile_screen.dart';
import 'package:tugas_akhir_supabase/widgets/session_guard_wrapper.dart';

/// Defines all routes used in the application
class AppRoutes {
  /// Non-authenticated routes
  static final Map<String, Widget Function(BuildContext)> _publicRoutes = {
    '/': (context) => const splash.SplashScreen(),
    '/intro': (context) => const intro.AnimatedIntroScreen(),
    '/login': (context) => const LoginScreen(),
    '/register': (context) => const RegisterScreen(),
    '/forgot-password': (context) => const ForgotPasswordScreen(),
    '/otp': (context) {
      final args =
          ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?;
      return OtpScreen(
        email: args?['email'] ?? '',
        userId: args?['userId'] ?? '',
      );
    },
    '/reset-password': (context) => const ResetPasswordScreen(),
  };

  /// Authenticated routes
  static final Map<String, Widget Function(BuildContext)>
  _authenticatedRoutes = {
    '/home': (context) => const HomeScreen(),
    '/profile': (context) => const ProfileScreen(),
    '/calendar': (context) => const calendar.KalenderTanamScreen(),
    '/field-management': (context) => const FieldManagementScreen(),
    '/schedule-list': (context) => const ScheduleListScreen(),
    '/plant-scanner': (context) => const scanner.PlantScannerScreen(),
    '/community': (context) => const CommunityScreen(),
    '/enhanced-community': (context) => const EnhancedCommunityScreen(),
    '/analisis': (context) {
      final args =
          ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?;
      return panen.HarvestAnalysisScreen(userId: args?['userId'] ?? '');
    },
    '/analisis-input': (context) {
      final args =
          ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?;
      return input.AnalisisInputScreen(userId: args?['userId'] ?? '');
    },
    '/analisis-hasil': (context) {
      final args =
          ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?;
      return hasil.HarvestResultScreen(
        userId: args?['userId'] ?? '',
        harvestData: args?['harvestData'],
        scheduleData: args?['scheduleData'],
      );
    },
    '/analisis-chart': (context) {
      final args =
          ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?;
      return chart.HarvestAnalysisChart(
        userId: args?['userId'] ?? '',
        harvestData: args?['harvestData'],
        scheduleData: args?['scheduleData'],
        isManualInput: args?['isManualInput'] ?? false,
      );
    },
    '/kalender-detail': (context) {
      final args =
          ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?;
      return detail.ScheduleDetailScreen(scheduleId: args?['scheduleId'] ?? '');
    },

    /// Admin routes
    '/admin': (context) => const AdminDashboard(),
    '/admin/users': (context) => const UserManagement(),
    '/admin/crops': (context) => const CropManagement(),
    '/admin/community': (context) => const CommunityManagement(),
  };

  /// Combined routes with session guard for authenticated routes
  static Map<String, Widget Function(BuildContext)> get routes {
    final Map<String, Widget Function(BuildContext)> allRoutes = {};

    /// Add public routes as-is
    allRoutes.addAll(_publicRoutes);

    /// Add profile route without SessionGuardWrapper for easier access
    allRoutes['/profile'] = (context) => const ProfileScreen();

    /// Add admin routes without SessionGuardWrapper to prevent blocking
    allRoutes['/admin'] = (context) => const AdminDashboard();
    allRoutes['/admin/users'] = (context) => const UserManagement();
    allRoutes['/admin/crops'] = (context) => const CropManagement();
    allRoutes['/admin/community'] = (context) => const CommunityManagement();

    /// Add field management route without SessionGuardWrapper
    allRoutes['/field-management'] = (context) => const FieldManagementScreen();

    /// Add other authenticated routes wrapped with SessionGuardWrapper
    _authenticatedRoutes.forEach((route, builder) {
      // Skip routes that are already added above
      if (route != '/profile' &&
          route != '/admin' &&
          route != '/admin/users' &&
          route != '/admin/crops' &&
          route != '/admin/community' &&
          route != '/field-management') {
        allRoutes[route] =
            (context) => SessionGuardWrapper(child: builder(context));
      }
    });

    return allRoutes;
  }
}
