import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:on_field_work/helpers/services/auth_service.dart';
import 'package:on_field_work/helpers/services/tenant_service.dart';
import 'package:on_field_work/view/auth/forgot_password_screen.dart';
import 'package:on_field_work/view/auth/login_screen.dart';
import 'package:on_field_work/view/auth/register_account_screen.dart';
import 'package:on_field_work/view/auth/reset_password_screen.dart';
import 'package:on_field_work/view/error_pages/coming_soon_screen.dart';
import 'package:on_field_work/view/error_pages/error_404_screen.dart';
import 'package:on_field_work/view/error_pages/error_500_screen.dart';
import 'package:on_field_work/view/dashboard/dashboard_screen.dart';
import 'package:on_field_work/view/Attendence/attendance_screen.dart';
import 'package:on_field_work/view/employees/employees_screen.dart';
import 'package:on_field_work/view/auth/login_option_screen.dart';
import 'package:on_field_work/view/auth/mpin_screen.dart';
import 'package:on_field_work/view/auth/mpin_auth_screen.dart';
import 'package:on_field_work/view/directory/directory_main_screen.dart';
import 'package:on_field_work/view/expense/expense_screen.dart';
import 'package:on_field_work/view/document/user_document_screen.dart';
import 'package:on_field_work/view/tenant/tenant_selection_screen.dart';
import 'package:on_field_work/view/finance/finance_screen.dart';
import 'package:on_field_work/view/finance/advance_payment_screen.dart';
import 'package:on_field_work/view/finance/payment_request_screen.dart';
import 'package:on_field_work/view/service_project/service_project_screen.dart';
import 'package:on_field_work/view/infraProject/infra_project_screen.dart';

class AuthMiddleware extends GetMiddleware {
  @override
  RouteSettings? redirect(String? route) {
    if (!AuthService.isLoggedIn) {
      if (route != '/auth/login-option') {
        return const RouteSettings(name: '/auth/login-option');
      }
    } else if (!TenantService.isTenantSelected) {
      if (route != '/select-tenant') {
        return const RouteSettings(name: '/select-tenant');
      }
    }
    return null;
  }
}

getPageRoute() {
  var routes = [
    GetPage(
        name: '/',
        page: () => DashboardScreen(),
        middlewares: [AuthMiddleware()]),
    GetPage(
      name: '/dashboard',
      page: () => DashboardScreen(), // or your actual home screen
      middlewares: [AuthMiddleware()],
    ),
    GetPage(
        name: '/select-tenant',
        page: () => const TenantSelectionScreen(),
        middlewares: [AuthMiddleware()]),

    // Dashboard
    GetPage(
        name: '/dashboard/attendance',
        page: () => AttendanceScreen(),
        middlewares: [AuthMiddleware()]),
    GetPage(
        name: '/dashboard',
        page: () => DashboardScreen(),
        middlewares: [AuthMiddleware()]),
    GetPage(
        name: '/dashboard/employees',
        page: () => EmployeesScreen(),
        middlewares: [AuthMiddleware()]),
    GetPage(
        name: '/dashboard/directory-main-page',
        page: () => DirectoryMainScreen(),
        middlewares: [AuthMiddleware()]),
    // Expense
    GetPage(
        name: '/dashboard/expense-main-page',
        page: () => ExpenseMainScreen(),
        middlewares: [AuthMiddleware()]),
    // Documents
    GetPage(
        name: '/dashboard/document-main-page',
        page: () => UserDocumentsPage(),
        middlewares: [AuthMiddleware()]),
    // Finance
    GetPage(
        name: '/dashboard/finance',
        page: () => FinanceScreen(),
        middlewares: [AuthMiddleware()]),
    GetPage(
        name: '/dashboard/payment-request',
        page: () => PaymentRequestMainScreen(),
        middlewares: [AuthMiddleware()]),
    // Infrastructure Projects
    GetPage(
        name: '/dashboard/infra-projects',
        page: () => InfraProjectScreen(),
        middlewares: [AuthMiddleware()]),

    // Authentication
    GetPage(name: '/auth/login', page: () => LoginScreen()),
    GetPage(name: '/auth/login-option', page: () => LoginOptionScreen()),
    GetPage(name: '/auth/mpin', page: () => MPINScreen()),
    GetPage(name: '/auth/mpin-auth', page: () => MPINAuthScreen()),
    GetPage(
        name: '/auth/register_account',
        page: () => const RegisterAccountScreen()),
    GetPage(name: '/auth/forgot_password', page: () => ForgotPasswordScreen()),
    GetPage(
        name: '/auth/reset_password', page: () => const ResetPasswordScreen()),
    // Error
    GetPage(
        name: '/error/coming_soon',
        page: () => ComingSoonScreen(),
        middlewares: [AuthMiddleware()]),
    GetPage(
        name: '/error/500',
        page: () => Error500Screen(),
        middlewares: [AuthMiddleware()]),
    GetPage(
        name: '/error/404',
        page: () => Error404Screen(),
        middlewares: [AuthMiddleware()]),

    // Advance Payment
    GetPage(
      name: '/dashboard/advance-payment',
      page: () => AdvancePaymentScreen(),
      middlewares: [AuthMiddleware()],
    ),

    // Service Projects
    GetPage(
      name: '/dashboard/service-projects',
      page: () => ServiceProjectScreen(),
      middlewares: [AuthMiddleware()],
    ),
  ];
  return routes
      .map((e) => GetPage(
          name: e.name,
          page: e.page,
          middlewares: e.middlewares,
          transition: Transition.noTransition))
      .toList();
}
