namespace CMSMicroservice.Application.CommissionCQ.Queries.GetUserCommissionPayouts;

public class GetUserCommissionPayoutsQueryHandler : IRequestHandler<GetUserCommissionPayoutsQuery, GetUserCommissionPayoutsResponseDto>
{
    private readonly IApplicationDbContext _context;
    private readonly IWeekDefinitionRepository _weekDefinitionRepository;

    public GetUserCommissionPayoutsQueryHandler(
        IApplicationDbContext context,
        IWeekDefinitionRepository weekDefinitionRepository)
    {
        _context = context;
        _weekDefinitionRepository = weekDefinitionRepository;
    }

    public async Task<GetUserCommissionPayoutsResponseDto> Handle(GetUserCommissionPayoutsQuery request, CancellationToken cancellationToken)
    {
        var query = _context.UserCommissionPayouts
            .Include(x => x.WeekDefinition)
            .Include(x => x.User)
            .AsNoTracking()
            .AsQueryable();

        // فیلترها
        if (request.UserId.HasValue)
        {
            query = query.Where(x => x.UserId == request.UserId.Value);
        }

        if (request.Status.HasValue)
        {
            query = query.Where(x => x.Status == request.Status.Value);
        }

        if (request.WeekDefinitionId!=null)
        {
            query = query.Where(x => x.WeekDefinitionId == request.WeekDefinitionId);
        }

        query = query.ApplyOrder(sortBy: request.SortBy ?? "Created");

        var meta = await query.GetMetaData(request.PaginationState, cancellationToken);

        var models = await query
            .PaginatedListAsync(paginationState: request.PaginationState)
            .Select(x => new GetUserCommissionPayoutsResponseModel
            {
                Id = x.Id,
                UserId = x.UserId,
                FirstName = x.User.FirstName,
                LastName = x.User.LastName,
                WeekDefinitionId = x.WeekDefinitionId,
                WeekDisplayName = x.WeekDefinition != null ? x.WeekDefinition.DisplayName : "",
                WeeklyPoolId = x.WeeklyPoolId,
                BalancesEarned = x.BalancesEarned,
                ValuePerBalance = x.ValuePerBalance,
                TotalAmount = x.TotalAmount,
                Status = x.Status,
                PaidAt = x.PaidAt,
                WithdrawalMethod = x.WithdrawalMethod,
                IbanNumber = x.IbanNumber,
                WithdrawnAt = x.WithdrawnAt,
                Created = x.Created
            })
            .ToListAsync(cancellationToken);

        // // Populate WeekDisplayName from cache
        // foreach (var model in models)
        // {
        //     model.WeekDisplayName = _weekDefinitionRepository.GetDisplayNameByGregorianWeekNumber(model.WeekNumber);
        // }

        return new GetUserCommissionPayoutsResponseDto
        {
            MetaData = meta,
            Models = models
        };
    }
}
