import 'package:bookmywages/model/detail_page_model.dart';
import 'package:bookmywages/model/vendor_model/vendor_service_model.dart';
import 'package:bookmywages/routers/consts_router.dart';
import 'package:bookmywages/view/auth/login_page.dart';
import 'package:bookmywages/view/auth/otp_screen.dart';
import 'package:bookmywages/view/auth/sign_up_screen.dart';
import 'package:bookmywages/view/intro_screens/flash_screen.dart';
import 'package:bookmywages/view/intro_screens/vendor_welcome_page.dart';
import 'package:bookmywages/view/intro_screens/welcome_page.dart';
import 'package:bookmywages/view/user_main_screens/Category_page.dart';
import 'package:bookmywages/view/user_main_screens/booking_screen.dart';
import 'package:bookmywages/view/user_main_screens/detail_service_page.dart';
import 'package:bookmywages/view/user_main_screens/history_screen/history_main_contoller.dart';
import 'package:bookmywages/view/user_main_screens/home_screen.dart';
import 'package:bookmywages/view/user_main_screens/list_service_screen.dart';
import 'package:bookmywages/view/user_main_screens/main_contoller.dart';
import 'package:bookmywages/view/user_main_screens/most_popluar_viewall.dart';
import 'package:bookmywages/view/user_main_screens/notification_page.dart';
import 'package:bookmywages/view/user_main_screens/package_screen.dart';
import 'package:bookmywages/view/user_main_screens/profile_screens/edit_profile.dart';
import 'package:bookmywages/view/user_main_screens/profile_screens/profile_changepass.dart';
import 'package:bookmywages/view/user_main_screens/profile_screens/profile_main_page.dart';
import 'package:bookmywages/view/vendor_main_screens/Vendor_profile/vendor_edit_profile.dart';
import 'package:bookmywages/view/vendor_main_screens/Vendor_profile/vendor_profile_main.dart';
import 'package:bookmywages/view/vendor_main_screens/vendor_catgories.dart';
import 'package:bookmywages/view/vendor_main_screens/vendor_history/vendor_history_maincontroller.dart';
import 'package:bookmywages/view/vendor_main_screens/vendor_history/vendor_service.dart';
import 'package:bookmywages/view/vendor_main_screens/vendor_history/vendor_serviceupload.dart';
import 'package:bookmywages/view/vendor_main_screens/vendor_homepage.dart';
import 'package:bookmywages/view/vendor_main_screens/vendor_maincontoller.dart';
import 'package:bookmywages/view/vendor_main_screens/vendor_package_page.dart';
import 'package:bookmywages/view/vendor_main_screens/vendor_register_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AppRoutes {
  
  static final routes = [
    GetPage(name: RouterConts.flashScreen, page: () => const FlashScreen()),
    GetPage(name: RouterConts.welcomepage, page: () => const WelcomePage()),
    GetPage(name: RouterConts.loginpage, page: () => const LoginScreen()),
    GetPage(name: RouterConts.siguppage, page: () => const SignUpScreen()),
    GetPage(
      name: RouterConts.otp,
      page: () => OtpScreen(userId: Get.arguments),
    ),

    // 🎯 Wrap main screens inside MainController
    GetPage(
      name: RouterConts.main,
      page: () => MainController(child: HomeScreen()),
    ),
    GetPage(
      name: RouterConts.homescreen,
      page: () => MainController(child: HomeScreen()),
    ),

   
    GetPage(
      name: RouterConts.listservice,
      page: () {
        final arguments = Get.arguments ?? {};
        final id = arguments['id']?.toString() ?? '0';
        final subcategoryId = arguments['subcategoryId'];
        final service = arguments['service'];
        final sourceTab = arguments['sourceTab'] ?? 0;

        return MainController(
          initialBottomIndex: sourceTab,
          child: ListServiceScreen(
            id: id,
            subcategoryId: subcategoryId,
            service: service,
          ),
        );
      },
    ),
    GetPage(
      name: RouterConts.detailserivce,
      page: () {
        final arguments = Get.arguments ?? {};

        // Extract and convert ID to int
        int id = 0; // Default value
        int sourceTab = 0;

        if (arguments is String) {
          id = int.tryParse(arguments) ?? 0;
        } else if (arguments is Map) {
          id = arguments['id'] is int
              ? arguments['id']
              : int.tryParse(arguments['id']?.toString() ?? '') ?? 0;
          sourceTab = arguments['sourceTab'] ?? 0;
        } else if (arguments is int) {
          id = arguments;
        }

        return MainController(
          initialBottomIndex: sourceTab,
          child: DetailServicePage(id: id), // Now passing int
        );
      },
    ),
    GetPage(
      name: RouterConts.bookingserivce,
      page: () {
        // Get the service model from arguments
        final arguments = Get.arguments ?? {};
        final DetailPageModel service = arguments is DetailPageModel
            ? arguments
            : arguments['service'] as DetailPageModel;

        // 🎯 NEW: Get source tab for booking screen
        final sourceTab = arguments is Map ? arguments['sourceTab'] ?? 0 : 0;

        return MainController(
          initialBottomIndex: sourceTab, // 🎯 Maintain tab state
          child: BookingScreen(service: service),
        );
      },
    ),
    GetPage(
      name: RouterConts.mostpopluarserviceviewall,
      page: () => MainController(
        initialBottomIndex: 0, // 🎯 Always from home
        child: MostPopluarViewall(),
      ),
    ),
    GetPage(
      name: RouterConts.profilemainscreen,
      page: () => MainController(child: ProfileMainPage()),
    ),
    GetPage(
      name: RouterConts.editprofile,
      page: () => MainController(child: EditProfile()),
    ),
    GetPage(
      name: RouterConts.changepassword,
      page: () => MainController(child: ProfileChangepass()),
    ),
    GetPage(
      name: RouterConts.packageList,
      page: () => MainController(child: PackageScreen()),
    ),
    GetPage(
      name: RouterConts.categorypage,
      page: () => MainController(child: CategoryPage()),
    ),
    GetPage(
      name: RouterConts.history,
      page: () {
        final arguments = Get.arguments;
        int? historyTabIndex;

        // Handle different argument types
        if (arguments is Map) {
          // Get history tab index from map
          historyTabIndex = arguments['historyTab'];
        } else if (arguments is int) {
          // For backward compatibility - treat as history tab index
          historyTabIndex = arguments;
        }

        return MainController(
          child: HistoryScreen(initialTabIndex: historyTabIndex),
          initialBottomIndex: 3, // Always set bottom nav to history (index 3)
        );
      },
    ),
    GetPage(name: RouterConts.vendorwelcome, page: () => VendorWelcomePage()),
    GetPage(
      name: RouterConts.vendorresgister,
      page: () => VendorRegisterPage(),
    ),
    GetPage(
      name: RouterConts.venodor,
      page: () => VendorController(child: VendorHomepage()),
    ),

    GetPage(
      name: RouterConts.vendorhomepage,
      page: () => VendorController(child: VendorHomepage()),
    ),
    GetPage(
      name: RouterConts.profilemainvendor,
      page: () => VendorController(child: VendorProfileMain()),
    ),
    GetPage(
      name: RouterConts.vendoreditprofile,
      page: () => VendorController(child: VendorEditProfile()),
    ),
    GetPage(
      name: RouterConts.vendorpackage,
      page: () => VendorController(child: VendorPackagePage()),
    ),
    GetPage(
      name: RouterConts.vendorcategory,
      page: () => VendorController(child: VendorCatgories()),
    ),
    GetPage(
      name: RouterConts.vendorhistory,
      page: () {
        final arguments = Get.arguments;
        int? historyTabIndex;

        // Handle different argument types
        if (arguments is Map) {
          // Get history tab index from map
          historyTabIndex = arguments['historyTab'];
        } else if (arguments is int) {
          // For backward compatibility - treat as history tab index
          historyTabIndex = arguments;
        }

        return VendorController(
          child: VendorHistoryMainController(
            initialTabIndex: historyTabIndex, // Pass the tab index
          ),
        );
      },
    ),
    GetPage(
      name: RouterConts.vendorservice,
      page: () {
        final args = Get.arguments as Map<String, dynamic>;
        return VendorController(
          child: VendorService(id: args['id'], tittle: args['tittle']),
        );
      },
    ),
    GetPage(
      name: RouterConts.vendorserviceupload,
      page: () {
        final VendorServiceModel service = Get.arguments as VendorServiceModel;

        return VendorController(child: VendorServiceupload(service: service));
      },
    ),
    
  ];


  

}

