namespace CMSMicroservice.Application.CommissionCQ.Queries.GetWeeklyCommissionPool;

public class GetWeeklyCommissionPoolQueryHandler : IRequestHandler<GetWeeklyCommissionPoolQuery, WeeklyCommissionPoolDto?>
{
    private readonly IApplicationDbContext _context;

    public GetWeeklyCommissionPoolQueryHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<WeeklyCommissionPoolDto?> Handle(GetWeeklyCommissionPoolQuery request, CancellationToken cancellationToken)
    {
        var pool = await _context.WeeklyCommissionPools
            .AsNoTracking()
            .Where(x => x.WeekNumber == request.WeekNumber)
            .Select(x => new WeeklyCommissionPoolDto
            {
                Id = x.Id,
                WeekNumber = x.WeekNumber,
                TotalPoolAmount = x.TotalPoolAmount,
                TotalBalances = x.TotalBalances,
                ValuePerBalance = x.ValuePerBalance,
                IsCalculated = x.IsCalculated,
                CalculatedAt = x.CalculatedAt,
                Created = x.Created
            })
            .FirstOrDefaultAsync(cancellationToken);

        return pool;
    }
}
