import 'package:get/get.dart';
import 'package:sigap/src/features/personalization/data/models/models/roles_model.dart';
import 'package:sigap/src/features/personalization/data/repositories/roles_repository.dart';
import 'package:sigap/src/utils/popups/loaders.dart';

enum RoleType { viewer, officer }

class RoleSelectionController extends GetxController {
  static RoleSelectionController get instance => Get.find();

  // Repositories
  final _rolesRepository = Get.find<RolesRepository>();

  // List of available roles
  final RxList<RoleModel> roles = <RoleModel>[].obs;

  // Selected role
  final Rx<RoleModel?> selectedRole = Rx<RoleModel?>(null);

  // Role type (Viewer or Officer)
  final Rx<RoleType> roleType = RoleType.viewer.obs;

  // Loading state
  final RxBool isLoading = false.obs;
  final RxBool isOfficer = false.obs;

  // Flag to track role loading state
  final isRolesLoading = true.obs;

  @override
  void onInit() {
    super.onInit();
    fetchRoles();
  }

  // Fetch available roles from repository
  Future<void> fetchRoles() async {
    try {
      isLoading.value = true;
      isRolesLoading.value = true;

      // Get all roles from repository
      final allRoles = await _rolesRepository.getAllRoles();

      // Filter roles to only include viewer and officer
      roles.value =
          allRoles
              .where(
                (role) =>
                    role.name.toLowerCase() == 'viewer' ||
                    role.name.toLowerCase() == 'officer',
              )
              .toList();
      
      // Set default role to Viewer
      if (roles.isNotEmpty) {
        // Find the viewer role
        final viewerRole = roles.firstWhere(
          (role) => role.name.toLowerCase() == 'viewer',
          orElse: () => roles[0], // Fallback to first role if viewer not found
        );

        // Select the viewer role by default
        selectRole(viewerRole);
      }
    } catch (e) {
      TLoaders.errorSnackBar(
        title: 'Error',
        message: 'Failed to load available roles. Please try again.',
      );
    } finally {
      isLoading.value = false;
      isRolesLoading.value = false;
    }
  }

  // Select a role
  void selectRole(RoleModel role) {
    selectedRole.value = role;

    // Set role type based on selected role
    if (role.name.toLowerCase() == 'officer') {
      roleType.value = RoleType.officer;
      isOfficer.value = true;
    } else {
      roleType.value = RoleType.viewer;
      isOfficer.value = false;
    }
  }

  // Continue with selected role
  // Future<void> continueWithRole() async {
  //   if (selectedRole.value == null) {
  //     TLoaders.errorSnackBar(
  //       title: 'Error',
  //       message: 'Please select a role to continue.',
  //     );
  //     return;
  //   }

  //   try {
  //     isLoading.value = true;

  //     // Logger().i('Selected role: ${selectedRole.value?.name}');

  //     // Check if the selected role is officer
  //     if (selectedRole.value?.name.toLowerCase() == 'officer') {
  //       isOfficer.value = true;
  //     } else {
  //       isOfficer.value = false;
  //     }

  //     // Navigate directly to step form with selected role
  //     Get.toNamed(
  //       AppRoutes.registrationForm,
  //       arguments: {'role': selectedRole.value},
  //     );
  //   } catch (e) {
  //     TLoaders.errorSnackBar(
  //       title: 'Error',
  //       message:
  //           'An error occurred while selecting the role. Please try again.',
  //     );
  //     // Logger().e('Error in continueWithRole: $e');
  //   } finally {
  //     isLoading.value = false;
  //   }
  // }
}
