import 'package:em2rp/services/api_service.dart';
import 'package:em2rp/repositories/event_repository.dart';
import 'package:em2rp/repositories/equipment_repository.dart';
import 'package:em2rp/repositories/container_repository.dart';
import 'package:em2rp/repositories/alert_repository.dart';
import 'package:em2rp/repositories/user_repository.dart';
import 'package:em2rp/repositories/option_repository.dart';

/// Service façade pour rétrocompatibilité.
/// Délègue les opérations aux Repositories de domaine respectifs.
class DataService {
  final EventRepository eventRepository;
  final EquipmentRepository equipmentRepository;
  final ContainerRepository containerRepository;
  final AlertRepository alertRepository;
  final UserRepository userRepository;
  final OptionRepository optionRepository;

  DataService(ApiService apiService)
      : eventRepository = EventRepository(apiService),
        equipmentRepository = EquipmentRepository(apiService),
        containerRepository = ContainerRepository(apiService),
        alertRepository = AlertRepository(apiService),
        userRepository = UserRepository(apiService),
        optionRepository = OptionRepository(apiService);

  // ============================================================================
  // OPTIONS & METADATA (delegated to OptionRepository)
  // ============================================================================
  Future<List<Map<String, dynamic>>> getOptions() => optionRepository.getOptions();

  Future<List<Map<String, dynamic>>> getEventTypes() => optionRepository.getEventTypes();

  Future<String> createEventType({required String name, required double defaultPrice}) =>
      optionRepository.createEventType(name: name, defaultPrice: defaultPrice);

  Future<void> updateEventType({required String eventTypeId, String? name, double? defaultPrice}) =>
      optionRepository.updateEventType(eventTypeId: eventTypeId, name: name, defaultPrice: defaultPrice);

  Future<void> deleteEventType(String eventTypeId) => optionRepository.deleteEventType(eventTypeId);

  Future<String> createOption(String code, Map<String, dynamic> data) =>
      optionRepository.createOption(code, data);

  Future<void> updateOption(String optionId, Map<String, dynamic> data) =>
      optionRepository.updateOption(optionId, data);

  Future<void> deleteOption(String optionId) => optionRepository.deleteOption(optionId);

  // ============================================================================
  // EVENTS (delegated to EventRepository)
  // ============================================================================
  Future<void> updateEventEquipment({
    required String eventId,
    List<Map<String, dynamic>>? assignedEquipment,
    String? preparationStatus,
    String? loadingStatus,
    String? unloadingStatus,
    String? returnStatus,
  }) =>
      eventRepository.updateEventEquipment(
        eventId: eventId,
        assignedEquipment: assignedEquipment,
        preparationStatus: preparationStatus,
        loadingStatus: loadingStatus,
        unloadingStatus: unloadingStatus,
        returnStatus: returnStatus,
      );

  Future<void> updateEvent(String eventId, Map<String, dynamic> data) =>
      eventRepository.updateEvent(eventId, data);

  Future<void> deleteEvent(String eventId) => eventRepository.deleteEvent(eventId);

  Future<List<Map<String, dynamic>>> getEventsByEventType(String eventTypeId) =>
      eventRepository.getEventsByEventType(eventTypeId);

  Future<Map<String, dynamic>> getEvents({String? userId}) =>
      eventRepository.getEvents(userId: userId);

  Future<Map<String, dynamic>> getEventsByMonth({required String userId, required int year, required int month}) =>
      eventRepository.getEventsByMonth(userId: userId, year: year, month: month);

  Future<List<Map<String, dynamic>>> searchEvents({required String userId, required String query, int limit = 20}) =>
      eventRepository.searchEvents(userId: userId, query: query, limit: limit);

  Future<Map<String, dynamic>> getEventWithDetails(String eventId) =>
      eventRepository.getEventWithDetails(eventId);

  // ============================================================================
  // EQUIPMENTS & AVAILABILITY (delegated to EquipmentRepository)
  // ============================================================================
  Future<List<Map<String, dynamic>>> getEquipments() =>
      equipmentRepository.getEquipments();

  Future<List<Map<String, dynamic>>> getEquipmentsByIds(List<String> equipmentIds) =>
      equipmentRepository.getEquipmentsByIds(equipmentIds);

  Future<Map<String, dynamic>> getEquipmentsPaginated({
    int limit = 20,
    String? startAfter,
    String? category,
    String? status,
    String? searchQuery,
    String sortBy = 'id',
    String sortOrder = 'asc',
  }) =>
      equipmentRepository.getEquipmentsPaginated(
        limit: limit,
        startAfter: startAfter,
        category: category,
        status: status,
        searchQuery: searchQuery,
        sortBy: sortBy,
        sortOrder: sortOrder,
      );

  Future<void> createEquipment(String equipmentId, Map<String, dynamic> data) =>
      equipmentRepository.createEquipment(equipmentId, data);

  Future<void> updateEquipment(String equipmentId, Map<String, dynamic> data) =>
      equipmentRepository.updateEquipment(equipmentId, data);

  Future<void> deleteEquipment(String equipmentId, {bool forceDelete = false}) =>
      equipmentRepository.deleteEquipment(equipmentId, forceDelete: forceDelete);

  Future<void> updateEquipmentStatusOnly({required String equipmentId, String? status, int? availableQuantity}) =>
      equipmentRepository.updateEquipmentStatusOnly(
        equipmentId: equipmentId,
        status: status,
        availableQuantity: availableQuantity,
      );

  Future<List<Map<String, dynamic>>> searchEquipmentsForAssistant({required String query, int limit = 12}) =>
      equipmentRepository.searchEquipmentsForAssistant(query: query, limit: limit);

  Future<Map<String, dynamic>> checkEquipmentAvailabilityForAssistant({
    required String equipmentId,
    required DateTime startDate,
    required DateTime endDate,
    String? excludeEventId,
  }) =>
      equipmentRepository.checkEquipmentAvailabilityForAssistant(
        equipmentId: equipmentId,
        startDate: startDate,
        endDate: endDate,
        excludeEventId: excludeEventId,
      );

  Future<Map<String, dynamic>> checkEquipmentAvailability({
    required String equipmentId,
    required DateTime startDate,
    required DateTime endDate,
    String? excludeEventId,
  }) =>
      equipmentRepository.checkEquipmentAvailability(
        equipmentId: equipmentId,
        startDate: startDate,
        endDate: endDate,
        excludeEventId: excludeEventId,
      );

  Future<Map<String, dynamic>> getConflictingEquipmentIds({
    required DateTime startDate,
    required DateTime endDate,
    String? excludeEventId,
    int installationTime = 0,
    int disassemblyTime = 0,
  }) =>
      equipmentRepository.getConflictingEquipmentIds(
        startDate: startDate,
        endDate: endDate,
        excludeEventId: excludeEventId,
        installationTime: installationTime,
        disassemblyTime: disassemblyTime,
      );

  Future<List<Map<String, dynamic>>> getMaintenances({String? equipmentId}) =>
      equipmentRepository.getMaintenances(equipmentId: equipmentId);

  Future<void> deleteMaintenance(String maintenanceId) =>
      equipmentRepository.deleteMaintenance(maintenanceId);

  // ============================================================================
  // CONTAINERS (delegated to ContainerRepository)
  // ============================================================================
  Future<List<Map<String, dynamic>>> getContainers() =>
      containerRepository.getContainers();

  Future<List<Map<String, dynamic>>> getContainersByIds(List<String> containerIds) =>
      containerRepository.getContainersByIds(containerIds);

  Future<Map<String, dynamic>> getContainersPaginated({
    int limit = 20,
    String? startAfter,
    String? type,
    String? status,
    String? searchQuery,
    String? category,
    String sortBy = 'id',
    String sortOrder = 'asc',
  }) =>
      containerRepository.getContainersPaginated(
        limit: limit,
        startAfter: startAfter,
        type: type,
        status: status,
        searchQuery: searchQuery,
        category: category,
        sortBy: sortBy,
        sortOrder: sortOrder,
      );

  Future<List<Map<String, dynamic>>> getContainersByEquipment(String equipmentId) =>
      containerRepository.getContainersByEquipment(equipmentId);

  Future<Map<String, dynamic>> checkContainerAvailability({
    required String containerId,
    required DateTime startDate,
    required DateTime endDate,
    String? excludeEventId,
  }) =>
      containerRepository.checkContainerAvailability(
        containerId: containerId,
        startDate: startDate,
        endDate: endDate,
        excludeEventId: excludeEventId,
      );

  // ============================================================================
  // USERS (delegated to UserRepository)
  // ============================================================================
  Future<Map<String, dynamic>> getCurrentUser() => userRepository.getCurrentUser();

  Future<List<Map<String, dynamic>>> getUsers() => userRepository.getUsers();

  Future<Map<String, dynamic>> getUser(String userId) => userRepository.getUser(userId);

  Future<void> deleteUser(String userId) => userRepository.deleteUser(userId);

  Future<void> updateUser(String userId, Map<String, dynamic> data) =>
      userRepository.updateUser(userId, data);

  Future<Map<String, dynamic>> createUserWithInvite({
    required String email,
    required String firstName,
    required String lastName,
    String? phoneNumber,
    required String roleId,
  }) =>
      userRepository.createUserWithInvite(
        email: email,
        firstName: firstName,
        lastName: lastName,
        phoneNumber: phoneNumber,
        roleId: roleId,
      );

  Future<List<Map<String, dynamic>>> getRoles() => userRepository.getRoles();

  // ============================================================================
  // ALERTS (delegated to AlertRepository)
  // ============================================================================
  Future<List<Map<String, dynamic>>> getAlerts() => alertRepository.getAlerts();

  Future<void> markAlertAsRead(String alertId) => alertRepository.markAlertAsRead(alertId);

  Future<void> deleteAlert(String alertId) => alertRepository.deleteAlert(alertId);
}
