import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:go_router/go_router.dart';
import 'package:salespro_admin/Route/shell_route_warpper.dart';
import 'package:salespro_admin/Screen/Authentication/add_profile.dart';
import 'package:salespro_admin/Screen/Authentication/forgot_password.dart';
import 'package:salespro_admin/Screen/Authentication/sign_up.dart';
import 'package:salespro_admin/Screen/Category%20List/category_list.dart';
import 'package:salespro_admin/Screen/Customer%20List/add_customer.dart';
import 'package:salespro_admin/Screen/Customer%20List/customer_list.dart';
import 'package:salespro_admin/Screen/Due%20List/due_list_screen.dart';
import 'package:salespro_admin/Screen/Expenses/expense_category.dart';
import 'package:salespro_admin/Screen/Expenses/new_expense.dart';
import 'package:salespro_admin/Screen/HRM/Designation/designation_list.dart';
import 'package:salespro_admin/Screen/HRM/salaries%20list/salaries_list_screen.dart';
import 'package:salespro_admin/Screen/Income/income_category.dart';
import 'package:salespro_admin/Screen/Income/new_income.dart';
import 'package:salespro_admin/Screen/Inventory%20Sales/inventory_sales.dart';
import 'package:salespro_admin/Screen/POS%20Sale/pos_sale.dart';
import 'package:salespro_admin/Screen/Payment%20Types/payment_types_list.dart';
import 'package:salespro_admin/Screen/Product/product_brand.dart';
import 'package:salespro_admin/Screen/Product/product_expired_list.dart';
import 'package:salespro_admin/Screen/Product/product_unit.dart';
import 'package:salespro_admin/Screen/Purchase%20List/purchase_list.dart';
import 'package:salespro_admin/Screen/Purchase%20Return/purchase_returns_list.dart';
import 'package:salespro_admin/Screen/Purchase/purchase.dart';
import 'package:salespro_admin/Screen/Sale%20List/pos_sale_edit.dart';
import 'package:salespro_admin/Screen/Sale%20List/sale_list.dart';
import 'package:salespro_admin/Screen/Sales%20Return/sales_returns_list.dart';
import 'package:salespro_admin/Screen/User%20Role%20System/user_role_screen.dart';
import 'package:salespro_admin/Screen/Whatsapp%20Marketing/whatsapp_marketing_screen.dart';
import 'package:salespro_admin/Screen/invoice_setting/invoice_setting.dart';
import 'package:salespro_admin/Screen/low_stock/low_stock.dart';
import 'package:salespro_admin/Screen/quatation_screen/quatation_screen.dart';
import 'package:salespro_admin/const.dart';
import 'package:salespro_admin/model/income_modle.dart';
import 'package:salespro_admin/model/user_role_model.dart';

import '../Screen/Authentication/log_in.dart';
import '../Screen/Authentication/profile_setup.dart';
import '../Screen/Customer List/edit_customer.dart';
import '../Screen/Expenses/expense_edit.dart';
import '../Screen/Expenses/expenses_list.dart';
import '../Screen/HRM/employees/employee_list.dart';
import '../Screen/Home/home_screen.dart';
import '../Screen/Income/income_Edit.dart';
import '../Screen/Income/income_list.dart';
import '../Screen/Ledger Screen/ledger_screen.dart';
import '../Screen/LossProfit/lossProfit_screen.dart';
import '../Screen/POS Sale/show_sale_payment_popup.dart';
import '../Screen/Product/WarebasedProduct.dart';
import '../Screen/Product/add_product.dart';
import '../Screen/Product/edit_product.dart';
import '../Screen/Product/product barcode/barcode_generate.dart';
import '../Screen/Product/product.dart';
import '../Screen/Purchase List/purchase_new_edit.dart';
import '../Screen/Purchase Return/purchase_return_screen.dart';
import '../Screen/Quotation List/quotation_list.dart';
import '../Screen/Reports/current_stock_widget.dart';
import '../Screen/Reports/daily_transaction.dart';
import '../Screen/Reports/report_screen.dart';
import '../Screen/Sales Return/sales_return_screen.dart';
import '../Screen/Subscription/purchase_plan.dart';
import '../Screen/Subscription/subscription_plan_page.dart';
import '../Screen/Supplier List/supplier_list.dart';
import '../Screen/User Role System/user_role_permisson.dart';
import '../Screen/WareHouse/ware_house_list.dart';
import '../Screen/WareHouse/warehouse_details.dart';
import '../Screen/Widgets/Pop UP/Purchase/purchase_payment_popup.dart';
import '../Screen/tax rates/tax_model.dart';
import '../Screen/tax rates/tax_rate_screen.dart';
import '../model/customer_model.dart';
import '../model/expense_model.dart';
import '../model/personal_information_model.dart';
import '../model/sale_transaction_model.dart';
import 'not_found.dart';

abstract class AcnooAppRoutes {
  static final routerConfig = GoRouter(
    initialLocation: '/',
    routes: [
      ShellRoute(
        builder: (context, state, child) {
          return ShellRouteWrapper(
            child: child,
          );
        },
        routes: [
          ///-----------------------DashBoard Route---------------------------
          GoRoute(
            path: '/dashboard',
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: MtHomeScreen(),
            ),
          ),

          ///-----------------------------Sales Route------------------------
          GoRoute(
            path: '/sales',
            redirect: (context, state) async {
              if (state.fullPath == '/sales') {
                return '/sales/pos-sales';
              }
              return null;
            },
            routes: [
              ///----------------Pos Sale Route------------------------------

              GoRoute(
                path: 'pos-sales',
                redirect: (context, state) {
                  if (finalUserRoleModel.posSaleCreate == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                builder: (context, state) {
                  // Check if state.extra is null
                  if (state.extra == null || state.extra is! SaleTransactionModel) {
                    // Handle the error case
                    return PosSale();
                  }
                  // Safely cast state.extra to SaleTransactionModel
                  final quotation = state.extra as SaleTransactionModel;
                  return PosSale(quotation: quotation);
                },
              ),

              ///----------------------payment popup------------------------
              GoRoute(
                path: 'show-payment-popup',
                builder: (context, state) {
                  final extra = state.extra as Map<String, dynamic>? ?? {};
                  final transitionModel = extra['transitionModel'];
                  final isFromQuotation = extra['isFromQuotation'] ?? false;

                  return ShowPaymentPopUp(
                    transitionModel: transitionModel,
                    isFromQuotation: isFromQuotation,
                  );
                },
              ),

              ///-----------------Inventory Sales Route---------------------
              GoRoute(
                path: 'inventory-sales',
                redirect: (context, state) {
                  if (finalUserRoleModel.inventorySaleCreate == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                pageBuilder: (context, state) => const NoTransitionPage<void>(
                  child: InventorySales(),
                ),
              ),

              ///---------------------Sales List Route------------------
              GoRoute(
                path: 'sale-list',
                redirect: (context, state) {
                  if (finalUserRoleModel.saleListRead == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                pageBuilder: (context, state) => const NoTransitionPage<void>(
                  child: SaleList(),
                ),
              ),

              ///---------------------Sales Return Route--------------------------
              GoRoute(
                path: 'sales-return-list',
                redirect: (context, state) {
                  if (finalUserRoleModel.saleReturnRead == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                pageBuilder: (context, state) => const NoTransitionPage<void>(
                  child: SalesReturn(),
                ),
              ),

              GoRoute(
                path: 'sales-return',
                redirect: (context, state) {
                  if (finalUserRoleModel.saleReturnCreate == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                builder: (context, state) {
                  final extra = state.extra as Map<String, dynamic>?; // Extract passed data
                  return SalesReturnScreen(
                    personalInformationModel: extra?['personalInformationModel'],
                    saleTransactionModel: extra?['saleTransactionModel'],
                  );
                },
              ),

              GoRoute(
                path: '/sales-edit',
                redirect: (context, state) {
                  if (finalUserRoleModel.saleListUpdate == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                builder: (BuildContext context, GoRouterState state) {
                  final args = state.extra as EditPosSale;
                  return EditPosSale(
                    transitionModel: args.transitionModel,
                    personalInformationModel: args.personalInformationModel,
                    isPosScreen: args.isPosScreen,
                    popUpContext: context,
                  );
                },
              ),

              ///-------------------------Quotation Route---------------------
              GoRoute(
                  path: 'quotation-list',
                  redirect: (context, state) {
                    if (finalUserRoleModel.quotationListRead == false && isSubUser) {
                      EasyLoading.showError('You don\'t have permission to access this page');
                      return '/dashboard';
                    }
                    return null;
                  },
                  pageBuilder: (context, state) => const NoTransitionPage<void>(
                        child: QuotationList(),
                      ),
                  routes: [
                    ///-----------------Quotation screen------------------
                    GoRoute(
                      path: 'quotation-screen',
                      redirect: (context, state) {
                        if (finalUserRoleModel.quotationListCreate == false && isSubUser) {
                          EasyLoading.showError('You don\'t have permission to access this page');
                          return '/dashboard';
                        }
                        return null;
                      },
                      pageBuilder: (context, state) => const NoTransitionPage<void>(
                        child: QuotationScreen(),
                      ),
                    ),
                  ]),
            ],
          ),

          ///----------------------purchase route---------------------------
          GoRoute(
            path: '/purchase',
            redirect: (context, state) async {
              if (state.fullPath == '/purchase') {
                return '/purchase/pos-purchase';
              }
              return null;
            },
            routes: [
              ///----------------Pos Purchase Route------------------------------
              GoRoute(
                path: 'pos-purchase',
                redirect: (context, state) {
                  if (finalUserRoleModel.purchaseCreate == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                pageBuilder: (context, state) => const NoTransitionPage<void>(
                  child: Purchase(),
                ),
              ),

              ///----------------------purchase payments-------------------
              GoRoute(
                path: 'purchase-payment-popup',
                builder: (context, state) {
                  final extra = state.extra as Map<String, dynamic>? ?? {};

                  return PurchaseShowPaymentPopUp(
                    transitionModel: extra['transitionModel'],
                  );
                },
              ),

              ///---------------------Purchase List Route------------------
              GoRoute(
                path: 'purchase-list',
                redirect: (context, state) {
                  if (finalUserRoleModel.purchaseRead == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                pageBuilder: (context, state) => const NoTransitionPage<void>(
                  child: PurchaseList(),
                ),
              ),

              ///---------------------Purchase Return Route--------------------------
              GoRoute(
                path: 'purchase-return',
                redirect: (context, state) {
                  if (finalUserRoleModel.purchaseReturnRead == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                pageBuilder: (context, state) => const NoTransitionPage<void>(
                  child: PurchaseReturn(),
                ),
              ),

              ///-----------------------purchase return-------------------
              GoRoute(
                path: '/purchase-returns',
                redirect: (context, state) {
                  if (finalUserRoleModel.purchaseReturnCreate == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                builder: (context, state) {
                  final extra = state.extra as Map<String, dynamic>? ?? {};
                  return PurchaseReturnScreen(
                    purchaseTransactionModel: extra['purchaseTransactionModel'],
                    personalInformationModel: extra['personalInformationModel'],
                  );
                },
              ),
            ],
          ),

          ///--------------------------Category------------------------
          GoRoute(
            path: '/category-list',
            redirect: (context, state) {
              if (finalUserRoleModel.categoriesRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: CategoryList(),
            ),
          ),

          ///----------------------Product------------------------------------
          GoRoute(
              path: '/product',
              redirect: (context, state) {
                if (finalUserRoleModel.productRead == false && isSubUser) {
                  EasyLoading.showError('You don\'t have permission to access this page');
                  return '/dashboard';
                }
                return null;
              },
              pageBuilder: (context, state) => const NoTransitionPage<void>(
                    child: Product(),
                  ),
              routes: [
                ///----------------------add product-------------------
                GoRoute(
                  path: 'add-product',
                  redirect: (context, state) {
                    if (finalUserRoleModel.productCreate == false && isSubUser) {
                      EasyLoading.showError('You don\'t have permission to access this page');
                      return '/dashboard';
                    }
                    return null;
                  },
                  builder: (context, state) {
                    final extra = state.extra as Map<String, dynamic>?;

                    return AddProduct(
                      allProductsCodeList: extra?['allProductsCodeList'] ?? [],
                      warehouseBasedProductModel: (extra?['warehouseBasedProductModel'] as List<dynamic>?)
                              ?.map((e) => e as WarehouseBasedProductModel)
                              .toList() ??
                          [],
                    );
                  },
                ),

                ///--------------edit product----------------------------------
                GoRoute(
                  path: 'edit-product',
                  redirect: (context, state) {
                    if (finalUserRoleModel.productUpdate == false && isSubUser) {
                      EasyLoading.showError('You don\'t have permission to access this page');
                      return '/dashboard';
                    }
                    return null;
                  },
                  builder: (context, state) {
                    final extra = state.extra as Map<String, dynamic>?;

                    return EditProduct(
                      productModel: extra?['productModel'], // Pass productModel as is
                      allProductsNameList: extra?['allProductsNameList'] ?? [],
                      groupTaxModel: (extra?['groupTaxModel'] as List<dynamic>?)
                              ?.map((e) => e as GroupTaxModel) // Ensure correct type conversion
                              .toList() ??
                          [],
                    );
                  },
                ),

                ///----------------barcode-generator----------------------------
                GoRoute(
                    path: 'barcode-generator',
                    pageBuilder: (context, state) => const NoTransitionPage<void>(
                          child: BarcodeGenerate(),
                        ))
              ]),

          ///--------------------Product expired list----------------------------
          GoRoute(
            path: '/product-expired',
            redirect: (context, state) {
              if (finalUserRoleModel.expiredRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: ProductExpiredList(),
            ),
          ),

          ///------------------product brand----------------------------------------
          GoRoute(
            path: '/product-brand',
            redirect: (context, state) {
              if (finalUserRoleModel.brandRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: ProductBrand(),
            ),
          ),

          ///------------------product unit----------------------------------------
          GoRoute(
            path: '/product-unit',
            redirect: (context, state) {
              if (finalUserRoleModel.unitRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: ProductUnit(),
            ),
          ),

          ///--------------------Ware house Route----------------------------
          GoRoute(
            path: '/warehouse-list',
            redirect: (context, state) {
              if (finalUserRoleModel.wareHouseRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: WareHouseList(),
            ),
          ),

          ///-----------------------Supplier List----------------------------
          GoRoute(
            path: '/supplier-list',
            redirect: (context, state) {
              if (finalUserRoleModel.supplierListRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: SupplierList(),
            ),
          ),

          ///--------------------------Customer List------------------------
          GoRoute(
            path: '/customer-list',
            redirect: (context, state) {
              if (finalUserRoleModel.customerListRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: CustomerList(),
            ),
          ),

          ///--------------------------Add Customer------------------------
          GoRoute(
            path: '/add-customer',
            redirect: (context, state) {
              if (finalUserRoleModel.customerListCreate == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            builder: (BuildContext context, GoRouterState state) {
              final extra = state.extra as Map<String, dynamic>;
              return AddCustomer(
                typeOfCustomerAdd: extra['typeOfCustomerAdd'] as String,
                listOfPhoneNumber: extra['listOfPhoneNumber'] as List<String>,
              );
            },
          ),

          ///----------------------Edit customer---------------------------
          GoRoute(
            path: '/edit-customer',
            redirect: (context, state) {
              if (finalUserRoleModel.customerListUpdate == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            builder: (BuildContext context, GoRouterState state) {
              final extra = state.extra as Map<String, dynamic>;
              return EditCustomer(
                customerModel: extra['customerModel'] as CustomerModel,
                allPreviousCustomer: extra['allPreviousCustomer'] as List<CustomerModel>,
                typeOfCustomerAdd: extra['typeOfCustomerAdd'] as String,
              );
            },
          ),

          ///---------------edit Sale---------------------------
          GoRoute(
            path: '/edit-pos-sale',
            redirect: (context, state) {
              if (finalUserRoleModel.posSaleUpdate == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            builder: (context, state) {
              // Extract the data from the `extra` parameter
              final extra = state.extra as Map<String, dynamic>;
              final transitionModel = extra['transitionModel'] as SaleTransactionModel;
              final personalInformationModel = extra['personalInformationModel'] as PersonalInformationModel;
              final isPosScreen = extra['isPosScreen'] as bool;
              final popUpContext = extra['popUpContext'] as BuildContext;

              return EditPosSale(
                transitionModel: transitionModel,
                personalInformationModel: personalInformationModel,
                isPosScreen: isPosScreen,
                popUpContext: popUpContext,
              );
            },
          ),

          ///---------------------purchase edit------------------
          GoRoute(
            path: '/edit-purchase',
            redirect: (context, state) {
              if (finalUserRoleModel.purchaseUpdate == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            builder: (context, state) {
              final extra = state.extra as Map<String, dynamic>? ?? {};

              return EditPurchase(
                personalInformationModel: extra['personalInformationModel'],
                isPosScreen: extra['isPosScreen'] ?? false,
                purchaseTransitionModel: extra['purchaseTransitionModel'],
                popupContext: extra['popupContext'],
              );
            },
          ),

          ///------------------warehouse details----------------------------
          GoRoute(
            path: '/warehouse-details/:id',
            redirect: (context, state) {
              if (finalUserRoleModel.wareHouseRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            builder: (context, state) {
              final String warehouseID = state.pathParameters['id']!;
              final String warehouseName = state.extra as String;
              return WareHouseDetails(warehouseID: warehouseID, warehouseName: warehouseName);
            },
          ),

          ///-----------------------Due List Route--------------------------
          GoRoute(
            path: '/due-list',
            redirect: (context, state) {
              if (finalUserRoleModel.dueListRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: DueList(),
            ),
          ),

          ///------------------Ledger----------------------------------------
          GoRoute(
            path: '/ledger',
            redirect: (context, state) {
              if (finalUserRoleModel.ledgerRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: LedgerScreen(),
            ),
          ),

          ///---------------------Loss Profit Route-----------------------------
          GoRoute(
            path: '/loss-profit',
            redirect: (context, state) {
              if (finalUserRoleModel.lossProfitRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: LossProfitScreen(),
            ),
          ),

          ///---------------------Payment Types-----------------------------
          GoRoute(
            path: '/payment-types',
            redirect: (context, state) {
              if (finalUserRoleModel.paymentTypeRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: PaymentTypesList(),
            ),
          ),

          ///--------------------------Expense Route----------------------------
          GoRoute(
              path: '/expense',
              redirect: (context, state) {
                if (finalUserRoleModel.expenseRead == false && isSubUser) {
                  EasyLoading.showError('You don\'t have permission to access this page');
                  return '/dashboard';
                }
                return null;
              },
              pageBuilder: (context, state) => const NoTransitionPage<void>(
                    child: ExpensesList(),
                  ),
              routes: [
                ///---------------------------------New Expense Route-------
                GoRoute(
                  path: 'new-expense',
                  redirect: (context, state) {
                    if (finalUserRoleModel.expenseCreate == false && isSubUser) {
                      EasyLoading.showError('You don\'t have permission to access this page');
                      return '/dashboard';
                    }
                    return null;
                  },
                  pageBuilder: (context, state) => const NoTransitionPage<void>(
                    child: NewExpense(),
                  ),
                ),

                ///-----------------expense category--------------------------
                GoRoute(
                  path: 'expense-category',
                  redirect: (context, state) {
                    if (finalUserRoleModel.expenseCategoryRead == false && isSubUser) {
                      EasyLoading.showError('You don\'t have permission to access this page');
                      return '/dashboard';
                    }
                    return null;
                  },
                  pageBuilder: (context, state) => const NoTransitionPage<void>(
                    child: ExpenseCategory(),
                  ),
                ),
                GoRoute(
                  path: 'edit-expense',
                  redirect: (context, state) {
                    if (finalUserRoleModel.expenseUpdate == false && isSubUser) {
                      EasyLoading.showError('You don\'t have permission to access this page');
                      return '/dashboard';
                    }
                    return null;
                  },
                  pageBuilder: (context, state) {
                    final expenseModel = state.extra as ExpenseModel; // Assuming you pass the data as extra
                    return NoTransitionPage<void>(
                      child: ExpenseEdit(
                        expenseModel: expenseModel,
                        // menuContext: bc,
                      ),
                    );
                  },
                )
              ]),

          ///---------------------------Income Route----------------------------
          GoRoute(
              path: '/income',
              redirect: (context, state) {
                if (finalUserRoleModel.incomeRead == false && isSubUser) {
                  EasyLoading.showError('You don\'t have permission to access this page');
                  return '/dashboard';
                }
                return null;
              },
              pageBuilder: (context, state) => const NoTransitionPage<void>(
                    child: IncomeList(),
                  ),
              routes: [
                ///---------------------------------New Expense Route-------
                GoRoute(
                  path: 'new-income',
                  redirect: (context, state) {
                    if (finalUserRoleModel.incomeCreate == false && isSubUser) {
                      EasyLoading.showError('You don\'t have permission to access this page');
                      return '/dashboard';
                    }
                    return null;
                  },
                  pageBuilder: (context, state) => const NoTransitionPage<void>(
                    child: NewIncome(),
                  ),
                ),
                GoRoute(
                  path: 'income-category',
                  redirect: (context, state) {
                    if (finalUserRoleModel.incomeCategoryRead == false && isSubUser) {
                      EasyLoading.showError('You don\'t have permission to access this page');
                      return '/dashboard';
                    }
                    return null;
                  },
                  pageBuilder: (context, state) => const NoTransitionPage<void>(
                    child: IncomeCategory(),
                  ),
                ),
                GoRoute(
                  path: 'edit-income',
                  redirect: (context, state) {
                    if (finalUserRoleModel.incomeUpdate == false && isSubUser) {
                      EasyLoading.showError('You don\'t have permission to access this page');
                      return '/dashboard';
                    }
                    return null;
                  },
                  pageBuilder: (context, state) {
                    final incomeModel = state.extra as IncomeModel; // Assuming you pass the data as extra
                    return NoTransitionPage<void>(
                      child: IncomeEdit(
                        incomeModel: incomeModel,
                        // menuContext: bc,
                      ),
                    );
                  },
                )
              ]),

          ///----------------------------DailyTransactionScreen route-----------
          GoRoute(
            path: '/transaction',
            redirect: (context, state) {
              if (finalUserRoleModel.transactionRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: DailyTransaction(),
            ),
          ),

          ///----------------Report Route----------------------------------
          GoRoute(
            path: '/reports',
            redirect: (context, state) {
              if (finalUserRoleModel.allReportsRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: SaleReports(),
            ),
          ),

          ///---------------------Stock List------------------------------------
          GoRoute(
            path: '/stock-list',
            redirect: (context, state) {
              if (finalUserRoleModel.stockListRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: CurrentStockWidget(),
            ),
          ),

          ///---------------------Add user role------------------------------------
          GoRoute(
            path: '/add-user-role',
            redirect: (context, state) {
              if ((finalUserRoleModel.userRoleCreate == false || finalUserRoleModel.userRoleUpdate == false) &&
                  isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) {
              // Check if state.extra is null
              if (state.extra == null || state.extra is! UserRoleModel) {
                List<UserRoleModel> users = state.extra as List<UserRoleModel>? ?? [];
                // Handle the error case
                return NoTransitionPage<void>(
                  child: AddUserRolePermission(
                    users: users,
                  ),
                );
              } else {
                // Safely cast state.extra to UserRoleModel
                final UserRoleModel userRoleModel = state.extra as UserRoleModel;
                return NoTransitionPage<void>(
                  child: AddUserRolePermission(userRoleModel: userRoleModel),
                );
              }
            },
          ),

          ///---------------------Low Stock List------------------------------------
          GoRoute(
            path: '/low-stock',
            redirect: (context, state) {
              if (finalUserRoleModel.lowStockRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: LowStockScreen(),
            ),
          ),

          ///----------------------whatsapp marketing-------------------------
          GoRoute(
            path: '/whatsapp-marketing',
            redirect: (context, state) {
              if (finalUserRoleModel.whatsAppMarketingRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: WhatsappMarketingScreen(),
            ),
          ),

          ///------------------------SubscriptionPage Route---------------------
          GoRoute(
              path: '/subscription',
              redirect: (context, state) {
                if (finalUserRoleModel.subscriptionRead == false && isSubUser) {
                  EasyLoading.showError('You don\'t have permission to access this page');
                  return '/dashboard';
                }
                return null;
              },
              pageBuilder: (context, state) => const NoTransitionPage<void>(
                    child: SubscriptionPage(),
                  ),
              routes: [
                GoRoute(
                  path: 'purchase-plan',
                  redirect: (context, state) {
                    if (finalUserRoleModel.saleListCreate == false && isSubUser) {
                      EasyLoading.showError('You don\'t have permission to access this page');
                      return '/dashboard';
                    }
                    return null;
                  },
                  builder: (context, state) {
                    final args = state.extra as Map<String, dynamic>? ??
                        {
                          'initialSelectedPackage': 'defaultPackage', // Provide a default value
                          'initPackageValue': 0, // Provide a default value
                        };
                    return PurchasePlan(
                      initialSelectedPackage: args['initialSelectedPackage'],
                      initPackageValue: args['initPackageValue'],
                    );
                  },
                ),
              ]),

          ///-----------------User Role Screen----------------------------------
          GoRoute(
            path: '/user-role',
            redirect: (context, state) {
              if (finalUserRoleModel.userRoleRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: UserRoleScreen(),
            ),
          ),

          ///------------------------TaxRates---------------------
          GoRoute(
            path: '/tax-rates',
            redirect: (context, state) {
              if (finalUserRoleModel.taxRateRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              return null;
            },
            pageBuilder: (context, state) => const NoTransitionPage<void>(
              child: TaxRates(),
            ),
          ),
          // ///-----------------User Role Screen----------------------------------
          // GoRoute(
          //   path: UserRoleScreen.route,
          //   pageBuilder: (context, state) => const NoTransitionPage<void>(
          //     child: UserRoleScreen(),
          //   ),
          // ),
          ///------------------------Hrm Route----------------------------------
          GoRoute(
            path: '/hrm',
            redirect: (context, state) async {
              if (finalUserRoleModel.hrmRead == false && isSubUser) {
                EasyLoading.showError('You don\'t have permission to access this page');
                return '/dashboard';
              }
              if (state.fullPath == '/hrm') {
                return '/hrm/designation-list';
              }
              return null;
            },
            routes: [
              ///----------------Designation Route------------------------------
              GoRoute(
                path: 'designation-list',
                redirect: (context, state) {
                  if (finalUserRoleModel.hrmRead == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                pageBuilder: (context, state) => const NoTransitionPage<void>(
                  child: DesignationListScreen(),
                ),
              ),

              ///-----------------Employee Route---------------------
              GoRoute(
                path: 'employee',
                redirect: (context, state) {
                  if (finalUserRoleModel.hrmRead == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                pageBuilder: (context, state) => const NoTransitionPage<void>(
                  child: EmployeeListScreen(),
                ),
              ),

              ///---------------------Sales List Route------------------
              GoRoute(
                path: 'salaries-list',
                redirect: (context, state) {
                  if (finalUserRoleModel.hrmRead == false && isSubUser) {
                    EasyLoading.showError('You don\'t have permission to access this page');
                    return '/dashboard';
                  }
                  return null;
                },
                pageBuilder: (context, state) => const NoTransitionPage<void>(
                  child: SalariesListScreen(),
                ),
              ),
            ],
          ),
          GoRoute(
              path: '/setting',
              redirect: (context, state) async {
                if (state.fullPath == '/setting') {
                  return '/setting/invoice-setting';
                }
                return null;
              },
              routes: [
                GoRoute(
                  path: 'invoice-setting',
                  pageBuilder: (context, state) => NoTransitionPage<void>(
                    child: InvoiceSetting(),
                  ),
                ),
              ]),
        ],
      ),
      GoRoute(
        path: EmailLogIn.route,
        pageBuilder: (context, state) => const NoTransitionPage<void>(
          child: EmailLogIn(),
        ),
      ),
      GoRoute(path: SignUp.route, pageBuilder: (context, state) => const NoTransitionPage<void>(child: SignUp())),
      GoRoute(
          path: ForgotPassword.route,
          pageBuilder: (context, state) => const NoTransitionPage<void>(child: ForgotPassword())),
      GoRoute(
          path: ProfileAdd.route, pageBuilder: (context, state) => const NoTransitionPage<void>(child: ProfileAdd())),
      GoRoute(
        path: '/profile-update',
        name: 'profile-update',
        builder: (context, state) {
          final details = state.extra;
          if (details is! PersonalInformationModel) {
            // Optionally, navigate back or show an error widget
            return const Placeholder();
          }
          return ProfileUpdate(personalInformationModel: details);
        },
      ),
    ],
    errorPageBuilder: (context, state) => const NoTransitionPage(
      child: NotFoundView(),
    ),
  );
}
