using MediatR;
using MembershipService.Domain.AggregatesModel.StampCardAggregate;

namespace MembershipService.API.Application.Queries;

/// <summary>
/// EN: Handler for getting all stamp cards for a shop (admin view).
/// VI: Handler de lay tat ca stamp cards cho shop (admin view).
/// </summary>
public class GetStampCardsQueryHandler : IRequestHandler<GetStampCardsQuery, GetStampCardsResult>
{
    private readonly IStampCardRepository _stampCardRepository;

    public GetStampCardsQueryHandler(IStampCardRepository stampCardRepository)
    {
        _stampCardRepository = stampCardRepository ?? throw new ArgumentNullException(nameof(stampCardRepository));
    }

    public async Task<GetStampCardsResult> Handle(GetStampCardsQuery request, CancellationToken cancellationToken)
    {
        var (cards, totalCount) = await _stampCardRepository.GetByShopAsync(
            request.ShopId, request.PageIndex, request.PageSize, cancellationToken);

        var items = cards.Select(card => new StampCardDto(
            card.Id,
            card.ShopId,
            card.MemberId,
            card.CardName,
            card.TotalStampsRequired,
            card.CurrentStamps,
            card.IsCompleted,
            card.RewardClaimed,
            card.CreatedAt,
            card.CompletedAt)).ToList();

        return new GetStampCardsResult(items, totalCount, request.PageIndex, request.PageSize);
    }
}
