// EN: Handler for GetTherapistsQuery.
// VI: Handler cho GetTherapistsQuery.

using BookingService.API.Application.DTOs;
using BookingService.Domain.AggregatesModel.TherapistAggregate;
using MediatR;

namespace BookingService.API.Application.Queries.Therapist;

/// <summary>
/// EN: Handler for getting therapists by shop with optional active filter.
/// VI: Handler để lấy chuyên viên theo shop với tùy chọn lọc hoạt động.
/// </summary>
public class GetTherapistsQueryHandler : IRequestHandler<GetTherapistsQuery, List<TherapistDto>>
{
    private readonly ITherapistRepository _therapistRepository;
    private readonly ILogger<GetTherapistsQueryHandler> _logger;

    public GetTherapistsQueryHandler(
        ITherapistRepository therapistRepository,
        ILogger<GetTherapistsQueryHandler> logger)
    {
        _therapistRepository = therapistRepository ?? throw new ArgumentNullException(nameof(therapistRepository));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public async Task<List<TherapistDto>> Handle(GetTherapistsQuery request, CancellationToken cancellationToken)
    {
        _logger.LogInformation(
            "Getting therapists for shop {ShopId} / Lấy chuyên viên cho shop {ShopId}",
            request.ShopId);

        // EN: Get therapists from repository
        // VI: Lấy chuyên viên từ repository
        var therapists = await _therapistRepository.GetByShopIdAsync(
            request.ShopId, request.IsActive, cancellationToken);

        // EN: Map to DTOs
        // VI: Map sang DTOs
        return therapists.Select(t => new TherapistDto
        {
            Id = t.Id,
            ShopId = t.ShopId,
            Name = t.Name,
            Specialties = t.Specialties,
            WorkingHours = t.WorkingHours,
            IsActive = t.IsActive,
            CreatedAt = t.CreatedAt,
            UpdatedAt = t.UpdatedAt
        }).ToList();
    }
}
