import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:kasy_kit/core/bottom_menu/bottom_menu.dart';
import 'package:kasy_kit/core/config/features.dart';
import 'package:kasy_kit/core/data/api/analytics_api.dart';
import 'package:kasy_kit/core/guards/user_info_guard.dart';
import 'package:kasy_kit/core/widgets/page_not_found.dart';
import 'package:kasy_kit/features/authentication/ui/phone_auth_page.dart';
import 'package:kasy_kit/features/authentication/ui/recover_password_page.dart';
import 'package:kasy_kit/features/authentication/ui/signin_page.dart';
import 'package:kasy_kit/features/authentication/ui/signup_page.dart';
import 'package:kasy_kit/features/feedbacks/ui/component/add_feature_form.dart';
import 'package:kasy_kit/features/feedbacks/ui/feedback_page.dart';
import 'package:kasy_kit/features/llm_chat/llm_chat_page.dart';
import 'package:kasy_kit/features/local_reminder/ui/reminder_page.dart';
import 'package:kasy_kit/features/onboarding/ui/onboarding_page.dart';
import 'package:kasy_kit/features/subscription/ui/premium_page.dart';

final goRouterProvider = Provider<GoRouter>(
  (ref) => generateRouter(),
);

extension GoRouterRiverpod on Ref {
  GoRouter get goRouter => read(goRouterProvider);
}

final navigatorKey = GlobalKey<NavigatorState>();

GoRouter generateRouter({
  String? initialLocation,
  List<GoRoute>? additionalRoutes,
  List<NavigatorObserver>? observers,
}) {
  return GoRouter(
    initialLocation: '/',
    navigatorKey: navigatorKey,
    errorBuilder: (context, state) => const PageNotFound(),
    observers: [
      AnalyticsObserver(
        analyticsApi: MixpanelAnalyticsApi.instance(),
      ),
      
      ...?observers,
    ],
    routes: [
      GoRoute(
        name: 'home',
        path: '/',
        builder: (context, state) => const UserInfosGuard(
          fallbackRoute: '/onboarding',
          child: BottomMenu(),
        ),
      ),
      GoRoute(
        name: 'onboarding',
        path: '/onboarding',
        builder: (context, state) => const OnboardingPage(),
      ),
      GoRoute(
        name: 'signup',
        path: '/signup',
        builder: (context, state) => const SignupPage(),
      ),
      GoRoute(
        name: 'signin',
        path: '/signin',
        builder: (context, state) => const SigninPage(),
      ),
      GoRoute(
        name: 'signinWithPhone',
        path: '/signinWithPhone',
        builder: (context, state) => const PhoneAuthPage(),
      ),
      GoRoute(
        name: 'premium',
        path: '/premium',
        builder: (context, state) => const PremiumPage(),
      ),
      if (withFeedback) ...[
        GoRoute(
          name: 'feedback',
          path: '/feedback',
          builder: (context, state) => const FeedbackPage(),
        ),
        GoRoute(
          name: 'feedback_new',
          path: '/feedback/new',
          builder: (context, state) => const AddFeatureComponent(),
        ),
      ],
      if (withLlmChat)
        GoRoute(
          name: 'assistant',
          path: '/assistant',
          builder: (context, state) => const LlmChatPage(),
        ),
      if (withLocalNotifications)
        GoRoute(
          name: 'reminder',
          path: '/reminder',
          builder: (context, state) => const ReminderPage(),
        ),
      GoRoute(
        name: 'notifications',
        path: '/notifications',
        builder: (context, state) => const UserInfosGuard(
          fallbackRoute: '/onboarding',
          child: BottomMenu(initialRoute: 'notifications'),
        ),
      ),
      GoRoute(
        name: 'settings',
        path: '/settings',
        builder: (context, state) => const UserInfosGuard(
          fallbackRoute: '/onboarding',
          child: BottomMenu(initialRoute: 'settings'),
        ),
      ),
      GoRoute(
        name: 'recover_password',
        path: '/recover_password',
        builder: (context, state) => const RecoverPasswordPage(),
      ),
      GoRoute(
        name: '404',
        path: '/404',
        builder: (context, state) => const PageNotFound(),
      ),
    ],
  );
}

