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

namespace Indotalent.Features.Inventory.PositiveAdjustment.Cqrs;

public class GetPositiveAdjustmentListResponse
{
    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 GetPositiveAdjustmentListQuery() : IRequest<List<GetPositiveAdjustmentListResponse>>;

public class GetPositiveAdjustmentListHandler : IRequestHandler<GetPositiveAdjustmentListQuery, List<GetPositiveAdjustmentListResponse>>
{
    private readonly AppDbContext _context;
    public GetPositiveAdjustmentListHandler(AppDbContext context) => _context = context;

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