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

namespace Indotalent.Features.Inventory.Scrapping.Cqrs;

public class GetScrappingListResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public DateTime? ScrappingDate { get; set; }
    public Data.Enums.ScrappingStatus Status { get; set; }
    public string? WarehouseName { get; set; }
    public string? Description { get; set; }
}

public record GetScrappingListQuery() : IRequest<List<GetScrappingListResponse>>;

public class GetScrappingListHandler : IRequestHandler<GetScrappingListQuery, List<GetScrappingListResponse>>
{
    private readonly AppDbContext _context;
    public GetScrappingListHandler(AppDbContext context) => _context = context;

    public async Task<List<GetScrappingListResponse>> Handle(GetScrappingListQuery request, CancellationToken cancellationToken)
    {
        return await _context.Scrapping
            .AsNoTracking()
            .Include(x => x.Warehouse)
            .OrderByDescending(x => x.CreatedAt)
            .Select(x => new GetScrappingListResponse
            {
                Id = x.Id,
                AutoNumber = x.AutoNumber,
                ScrappingDate = x.ScrappingDate,
                Status = x.Status,
                WarehouseName = x.Warehouse!.Name,
                Description = x.Description
            }).ToListAsync(cancellationToken);
    }
}