using MediatR;
using WhatsAppService.Domain.AggregatesModel.SampleAggregate;

namespace WhatsAppService.API.Application.Queries;

/// <summary>
/// EN: Handler for GetSampleQuery.
/// VI: Handler cho GetSampleQuery.
/// </summary>
public class GetSampleQueryHandler : IRequestHandler<GetSampleQuery, SampleViewModel?>
{
    private readonly ISampleRepository _sampleRepository;

    public GetSampleQueryHandler(ISampleRepository sampleRepository)
    {
        _sampleRepository = sampleRepository ?? throw new ArgumentNullException(nameof(sampleRepository));
    }

    public async Task<SampleViewModel?> Handle(
        GetSampleQuery request,
        CancellationToken cancellationToken)
    {
        var sample = await _sampleRepository.GetAsync(request.SampleId);

        if (sample is null)
        {
            return null;
        }

        return new SampleViewModel(
            sample.Id,
            sample.Name,
            sample.Description,
            sample.Status.Name,
            sample.CreatedAt,
            sample.UpdatedAt
        );
    }
}
