import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../auth/auth_store.dart';
import '../theme/app_animations.dart';
import '../../features/splash/splash_screen.dart';
import '../../features/auth/login_screen.dart';
import '../../features/auth/otp_screen.dart';
import '../../features/home/home_shell.dart';
import '../../features/home/dashboard_screen.dart';
import '../../features/schedule/schedule_screen.dart';
import '../../features/notifications/notifications_screen.dart';
import '../../features/profile/profile_screen.dart';
import '../../features/participants/participant_detail_screen.dart';
import '../../features/attendance/attendance_screen.dart';
import '../../features/invoices/invoices_screen.dart';
import '../../features/evaluations/evaluations_screen.dart';
import '../../features/events/events_screen.dart';
import '../../features/shop/shop_screen.dart';
import '../../features/messages/messages_screen.dart';
import '../../features/service_requests/service_requests_screen.dart';
import '../../features/registration/registration_screen.dart';
import '../../features/gate_pass/gate_pass_screen.dart';
import '../../features/about/about_academy_screen.dart';

final routerProvider = Provider<GoRouter>((ref) {
  final authState = ref.watch(authStoreProvider);

  return GoRouter(
    initialLocation: '/splash',
    redirect: (context, state) {
      if (authState.isLoading) return '/splash';

      final isAuth = authState.isAuthenticated;
      final isAuthRoute = state.matchedLocation.startsWith('/auth');
      final isSplash = state.matchedLocation == '/splash';
      final isPublic = state.matchedLocation.startsWith('/register');

      if (isSplash && !authState.isLoading) {
        return isAuth ? '/home' : '/auth/login';
      }

      if (!isAuth && !isAuthRoute && !isSplash && !isPublic) {
        return '/auth/login';
      }

      if (isAuth && isAuthRoute) {
        return '/home';
      }

      return null;
    },
    routes: [
      GoRoute(
        path: '/splash',
        pageBuilder: (context, state) =>
            PageTransitions.fade(const SplashScreen(), key: 'splash'),
      ),
      GoRoute(
        path: '/auth/login',
        pageBuilder: (context, state) =>
            PageTransitions.fade(const LoginScreen(), key: 'login'),
      ),
      GoRoute(
        path: '/auth/otp',
        pageBuilder: (context, state) {
          final phone = state.extra as String? ?? '';
          return PageTransitions.slide(OtpScreen(phone: phone), key: 'otp');
        },
      ),
      GoRoute(
        path: '/register',
        pageBuilder: (context, state) =>
            PageTransitions.slide(const RegistrationScreen(), key: 'register'),
      ),
      ShellRoute(
        builder: (context, state, child) => HomeShell(child: child),
        routes: [
          GoRoute(
            path: '/home',
            pageBuilder: (context, state) =>
                PageTransitions.fade(const DashboardScreen(), key: 'home'),
          ),
          GoRoute(
            path: '/schedule',
            pageBuilder: (context, state) =>
                PageTransitions.fade(const ScheduleScreen(), key: 'schedule'),
          ),
          GoRoute(
            path: '/notifications',
            pageBuilder: (context, state) =>
                PageTransitions.fade(const NotificationsScreen(), key: 'notifications'),
          ),
          GoRoute(
            path: '/profile',
            pageBuilder: (context, state) =>
                PageTransitions.fade(const ProfileScreen(), key: 'profile'),
          ),
        ],
      ),
      GoRoute(
        path: '/participants/:uuid',
        pageBuilder: (context, state) {
          final uuid = state.pathParameters['uuid']!;
          return PageTransitions.slide(
            ParticipantDetailScreen(uuid: uuid),
            key: 'participant-$uuid',
          );
        },
      ),
      GoRoute(
        path: '/participants/:uuid/attendance',
        pageBuilder: (context, state) {
          final uuid = state.pathParameters['uuid']!;
          return PageTransitions.slide(
            AttendanceScreen(participantUuid: uuid),
            key: 'attendance-$uuid',
          );
        },
      ),
      GoRoute(
        path: '/participants/:uuid/invoices',
        pageBuilder: (context, state) {
          final uuid = state.pathParameters['uuid']!;
          return PageTransitions.slide(
            InvoicesScreen(participantUuid: uuid),
            key: 'invoices-$uuid',
          );
        },
      ),
      GoRoute(
        path: '/participants/:uuid/evaluations',
        pageBuilder: (context, state) {
          final uuid = state.pathParameters['uuid']!;
          return PageTransitions.slide(
            EvaluationsScreen(participantUuid: uuid),
            key: 'evaluations-$uuid',
          );
        },
      ),
      GoRoute(
        path: '/events',
        pageBuilder: (context, state) =>
            PageTransitions.slide(const EventsScreen(), key: 'events'),
      ),
      GoRoute(
        path: '/shop',
        pageBuilder: (context, state) =>
            PageTransitions.slide(const ShopScreen(), key: 'shop'),
      ),
      GoRoute(
        path: '/messages',
        pageBuilder: (context, state) =>
            PageTransitions.slide(const MessagesScreen(), key: 'messages'),
      ),
      GoRoute(
        path: '/service-requests',
        pageBuilder: (context, state) =>
            PageTransitions.slide(const ServiceRequestsScreen(), key: 'requests'),
      ),
      GoRoute(
        path: '/gate-pass',
        pageBuilder: (context, state) =>
            PageTransitions.slide(const GatePassScreen(), key: 'gate-pass'),
      ),
      GoRoute(
        path: '/about-academy',
        pageBuilder: (context, state) =>
            PageTransitions.slide(const AboutAcademyScreen(), key: 'about-academy'),
      ),
    ],
  );
});
