using Microsoft.EntityFrameworkCore;
using PromotionService.Domain.AggregatesModel.RedemptionAggregate;
using PromotionService.Domain.SeedWork;

namespace PromotionService.Infrastructure.Repositories;

/// <summary>
/// EN: Repository implementation for Redemption aggregate.
/// VI: Triển khai repository cho Redemption aggregate.
/// </summary>
public class RedemptionRepository : IRedemptionRepository
{
    private readonly PromotionServiceContext _context;

    public IUnitOfWork UnitOfWork => _context;

    public RedemptionRepository(PromotionServiceContext context)
    {
        _context = context ?? throw new ArgumentNullException(nameof(context));
    }

    public async Task<IEnumerable<Redemption>> GetByVoucherIdAsync(Guid voucherId)
    {
        return await _context.Redemptions
            .Where(r => r.VoucherId == voucherId)
            .OrderByDescending(r => r.RedeemedAt)
            .ToListAsync();
    }

    public async Task<IEnumerable<Redemption>> GetByUserIdAsync(Guid userId)
    {
        return await _context.Redemptions
            .Where(r => r.UserId == userId)
            .OrderByDescending(r => r.RedeemedAt)
            .ToListAsync();
    }

    public async Task<IEnumerable<Redemption>> GetByCampaignIdAsync(Guid campaignId)
    {
        return await _context.Redemptions
            .Where(r => r.CampaignId == campaignId)
            .OrderByDescending(r => r.RedeemedAt)
            .ToListAsync();
    }

    public Redemption Add(Redemption redemption)
    {
        return _context.Redemptions.Add(redemption).Entity;
    }
}
