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

namespace Indotalent.Features.Inventory.NegativeAdjustment.Cqrs;

public class GetNegativeAdjustmentListResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public DateTime? AdjustmentDate { get; set; }
    public Data.Enums.AdjustmentStatus Status { get; set; }
    public string? Description { get; set; }
}

public record GetNegativeAdjustmentListQuery() : IRequest<List<GetNegativeAdjustmentListResponse>>;

public class GetNegativeAdjustmentListHandler : IRequestHandler<GetNegativeAdjustmentListQuery, List<GetNegativeAdjustmentListResponse>>
{
    private readonly AppDbContext _context;
    public GetNegativeAdjustmentListHandler(AppDbContext context) => _context = context;

    public async Task<List<GetNegativeAdjustmentListResponse>> Handle(GetNegativeAdjustmentListQuery request, CancellationToken cancellationToken)
    {
        return await _context.NegativeAdjustment
            .AsNoTracking()
            .OrderByDescending(x => x.CreatedAt)
            .Select(x => new GetNegativeAdjustmentListResponse
            {
                Id = x.Id,
                AutoNumber = x.AutoNumber,
                AdjustmentDate = x.AdjustmentDate,
                Status = x.Status,
                Description = x.Description
            }).ToListAsync(cancellationToken);
    }
}