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

namespace WhatsAppService.API.Application.Queries;

/// <summary>
/// EN: Handler for GetSamplesQuery.
/// VI: Handler cho GetSamplesQuery.
/// </summary>
public class GetSamplesQueryHandler : IRequestHandler<GetSamplesQuery, IEnumerable<SampleViewModel>>
{
    private readonly ISampleRepository _sampleRepository;

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

    public async Task<IEnumerable<SampleViewModel>> Handle(
        GetSamplesQuery request,
        CancellationToken cancellationToken)
    {
        var samples = await _sampleRepository.GetAllAsync();

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