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

using BookingService.API.Application.DTOs;
using BookingService.Infrastructure.Repositories;
using MediatR;

namespace BookingService.API.Application.Queries;

/// <summary>
/// EN: Handler for retrieving a single appointment.
/// VI: Handler để lấy một appointment.
/// </summary>
public class GetAppointmentQueryHandler : IRequestHandler<GetAppointmentQuery, AppointmentDto?>
{
    private readonly IAppointmentRepository _appointmentRepository;

    public GetAppointmentQueryHandler(IAppointmentRepository appointmentRepository)
    {
        _appointmentRepository = appointmentRepository ?? throw new ArgumentNullException(nameof(appointmentRepository));
    }

    public async Task<AppointmentDto?> Handle(GetAppointmentQuery request, CancellationToken cancellationToken)
    {
        var appointment = await _appointmentRepository.GetByIdAsync(request.AppointmentId, cancellationToken);
        
        if (appointment == null)
        {
            return null;
        }

        return new AppointmentDto
        {
            Id = appointment.Id,
            ShopId = appointment.ShopId,
            CustomerId = appointment.CustomerId,
            StaffId = appointment.StaffId,
            ResourceId = appointment.ResourceId,
            ServiceId = appointment.ServiceId,
            StartTime = appointment.StartTime,
            EndTime = appointment.EndTime,
            Status = appointment.Status,
            CreatedAt = appointment.CreatedAt
        };
    }
}
