using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Indotalent.Features.Setting.Tax.Cqrs;

public class GetTaxListResponse
{
    public string? Id { get; set; }
    public string? Code { get; set; }
    public string? Name { get; set; }
    public decimal PercentageValue { get; set; }
    public string? Category { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetTaxListQuery() : IRequest<List<GetTaxListResponse>>;

public class GetTaxListHandler : IRequestHandler<GetTaxListQuery, List<GetTaxListResponse>>
{
    private readonly AppDbContext _context;

    public GetTaxListHandler(AppDbContext context) => _context = context;

    public async Task<List<GetTaxListResponse>> Handle(GetTaxListQuery request, CancellationToken cancellationToken)
    {
        return await _context.Tax
            .AsNoTracking()
            .OrderBy(x => x.Code)
            .Select(x => new GetTaxListResponse
            {
                Id = x.Id,
                Code = x.Code,
                Name = x.Name,
                PercentageValue = x.PercentageValue,
                Category = x.Category,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy,
            })
            .ToListAsync(cancellationToken);
    }
}