import 'package:get/get.dart';
import 'package:on_field_work/helpers/services/api_service.dart';
import 'package:on_field_work/helpers/services/app_logger.dart';
import 'package:on_field_work/model/tenant/tenant_services_model.dart';

class ServiceController extends GetxController {
  List<Service> services = [];
  Service? selectedService;
  final isLoadingServices = false.obs;

  /// Fetch services assigned to a project
  Future<void> fetchServices(String projectId) async {
    try {
      isLoadingServices.value = true;
      final response = await ApiService.getAssignedServices(projectId);
      if (response != null) {
        services = response.data;
        logSafe("Services fetched: ${services.length}");
      } else {
        logSafe("Failed to fetch services for project $projectId",
            level: LogLevel.error);
      }
    } finally {
      isLoadingServices.value = false;
      update();
    }
  }

  /// Select a service
  void selectService(Service? service) {
    selectedService = service;
    update();
  }

  /// Clear selection
  void clearSelection() {
    selectedService = null;
    update();
  }

  /// Current selected name
  String get currentSelection => selectedService?.name ?? "All Services";
}
