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

namespace Indotalent.Features.Inventory.StockCount.Cqrs;

public class GetStockCountListResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public DateTime? CountDate { get; set; }
    public Data.Enums.StockCountStatus Status { get; set; }
    public string? WarehouseName { get; set; }
    public string? Description { get; set; }
}

public record GetStockCountListQuery() : IRequest<List<GetStockCountListResponse>>;

public class GetStockCountListHandler : IRequestHandler<GetStockCountListQuery, List<GetStockCountListResponse>>
{
    private readonly AppDbContext _context;
    public GetStockCountListHandler(AppDbContext context) => _context = context;

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