import 'package:get/get.dart';
import 'package:flutter/material.dart';
import '../../features/auth/views/phone_input_view.dart';
import '../../features/auth/views/otp_verify_view.dart';
import '../../features/auth/views/biometric_setup_view.dart';
import '../../features/auth/views/biometric_auth_view.dart';
import '../../features/main_shell/controllers/main_shell_controller.dart';
import '../../features/main_shell/views/main_shell_view.dart';
import '../../features/dashboard/controllers/dashboard_controller.dart';
import '../../features/scanner/views/scanner_view.dart';
import '../../features/scanner/controllers/scanner_controller.dart';
import '../../features/invoices/controllers/invoices_controller.dart';
import '../../features/settings/controllers/settings_controller.dart';
import '../../features/subscription/views/subscription_view.dart';
import '../../features/subscription/views/payment_receipt_view.dart';
import '../../features/invoices/views/invoice_detail_view.dart';
import '../../features/onboarding/views/onboarding_view.dart';
 import '../../features/onboarding/controllers/onboarding_controller.dart';
 import '../../core/storage/secure_storage.dart';
 import '../../features/companies/views/companies_management_view.dart';
 import '../../features/companies/views/company_stats_view.dart';
 import '../../features/users/views/users_management_view.dart';
 import '../../features/tenants/views/tenants_management_view.dart';
 
 part 'app_routes.dart';

class AppPages {
  // ignore: constant_identifier_names
  static const INITIAL = AppRoutes.SPLASH;

  static final routes = [
    GetPage(
      name: AppRoutes.SPLASH,
      page: () {
        // Check login state after a short delay
        Future.delayed(const Duration(seconds: 2), () async {
          final token = await SecureStorage().getToken();
          if (token != null && token.isNotEmpty) {
            // User is already logged in, request Biometric unlock before dashboard
            Get.offAllNamed(AppRoutes.BIOMETRIC_AUTH);
          } else {
            // Check if user has seen onboarding
            final hasSeenOnboarding =
                await SecureStorage().read('has_seen_onboarding');
            if (hasSeenOnboarding == 'true') {
              Get.offAllNamed(AppRoutes.PHONE_INPUT);
            } else {
              Get.offAllNamed(AppRoutes.ONBOARDING);
            }
          }
        });
        return Scaffold(
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(
                  'assets/images/logo.jpg',
                  height: 120,
                  errorBuilder: (context, error, stackTrace) => const Icon(
                    Icons.security,
                    size: 100,
                    color: Color(0xFF0F4C81),
                  ),
                ),
                const SizedBox(height: 24),
                const CircularProgressIndicator(),
              ],
            ),
          ),
        );
      },
    ),
    GetPage(
      name: AppRoutes.PHONE_INPUT,
      page: () => PhoneInputView(),
    ),
    GetPage(
      name: AppRoutes.OTP_VERIFY,
      page: () => OtpVerifyView(),
    ),
    GetPage(
      name: AppRoutes.BIOMETRIC_SETUP,
      page: () => BiometricSetupView(),
    ),
    GetPage(
      name: AppRoutes.BIOMETRIC_AUTH,
      page: () => const BiometricAuthView(),
    ),
    // NEW: Main Shell (replaces standalone dashboard route as the home)
    GetPage(
      name: AppRoutes.MAIN,
      page: () => const MainShellView(),
      binding: BindingsBuilder(() {
        Get.put(MainShellController());
        Get.put(DashboardController());
        Get.put(InvoicesController());
        Get.put(SettingsController());
      }),
    ),
    // Keep dashboard as standalone for backward compatibility
    GetPage(
      name: AppRoutes.DASHBOARD,
      page: () => const MainShellView(), // Now redirects to MainShell
      binding: BindingsBuilder(() {
        Get.put(MainShellController());
        Get.put(DashboardController());
        Get.put(InvoicesController());
        Get.put(SettingsController());
      }),
    ),
    GetPage(
      name: AppRoutes.SCANNER,
      page: () => const ScannerView(),
      binding: BindingsBuilder(() {
        Get.put(ScannerController());
      }),
    ),
    GetPage(
      name: AppRoutes.SUBSCRIPTION,
      page: () => const SubscriptionView(),
    ),
    GetPage(
      name: AppRoutes.PAYMENT_RECEIPT,
      page: () => const PaymentReceiptView(),
    ),
    GetPage(
      name: AppRoutes.INVOICE_DETAIL,
      page: () => const InvoiceDetailView(),
    ),
    GetPage(
      name: AppRoutes.ONBOARDING,
      page: () => const OnboardingView(),
      binding: BindingsBuilder(() {
        Get.put(OnboardingController());
      }),
    ),
     GetPage(
       name: AppRoutes.COMPANIES_MANAGEMENT,
       page: () => const CompaniesManagementView(),
     ),
     GetPage(
       name: AppRoutes.COMPANY_STATS,
       page: () => const CompanyStatsView(),
     ),
     GetPage(
       name: AppRoutes.TENANTS_MANAGEMENT,
       page: () => const TenantsManagementView(),
     ),
     GetPage(
       name: AppRoutes.USERS_MANAGEMENT,
       page: () => const UsersManagementView(),
     ),
   ];
 }
