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

namespace MembershipService.API.Application.Queries;

/// <summary>
/// EN: Handler for getting stamp card status for a member at a shop.
/// VI: Handler de lay trang thai stamp card cho member tai shop.
/// </summary>
public class GetStampCardQueryHandler : IRequestHandler<GetStampCardQuery, StampCardDto?>
{
    private readonly IStampCardRepository _stampCardRepository;

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

    public async Task<StampCardDto?> Handle(GetStampCardQuery request, CancellationToken cancellationToken)
    {
        var card = await _stampCardRepository.GetByMemberAndShopAsync(
            request.ShopId, request.MemberId, cancellationToken);

        if (card == null)
            return null;

        return new StampCardDto(
            card.Id,
            card.ShopId,
            card.MemberId,
            card.CardName,
            card.TotalStampsRequired,
            card.CurrentStamps,
            card.IsCompleted,
            card.RewardClaimed,
            card.CreatedAt,
            card.CompletedAt);
    }
}
