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

namespace Indotalent.Features.Setting.Tax.Cqrs;

public class GetTaxByIdResponse
{
    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 string? Description { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetTaxByIdQuery(string Id) : IRequest<GetTaxByIdResponse?>;

public class GetTaxByIdHandler : IRequestHandler<GetTaxByIdQuery, GetTaxByIdResponse?>
{
    private readonly AppDbContext _context;

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

    public async Task<GetTaxByIdResponse?> Handle(GetTaxByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.Tax
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .Select(x => new GetTaxByIdResponse
            {
                Id = x.Id,
                Code = x.Code,
                Name = x.Name,
                PercentageValue = x.PercentageValue,
                Category = x.Category,
                Description = x.Description,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy,
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}