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

namespace Indotalent.Features.Purchase.GoodsReceive.Cqrs;

public class GoodsReceiveInvenTransResponse
{
    public string? Id { get; set; }
    public string? ProductId { get; set; }
    public string? ProductName { get; set; }
    public string? WarehouseId { get; set; }
    public string? WarehouseName { get; set; }
    public double? Movement { get; set; }
}

public class GetGoodsReceiveByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public DateTime? ReceiveDate { get; set; }
    public Data.Enums.GoodsReceiveStatus Status { get; set; }
    public string? Description { get; set; }
    public string? PurchaseOrderId { get; set; }
    public string? PurchaseOrderNumber { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
    public List<GoodsReceiveInvenTransResponse> Items { get; set; } = new();
}

public record GetGoodsReceiveByIdQuery(string Id) : IRequest<GetGoodsReceiveByIdResponse?>;

public class GetGoodsReceiveByIdHandler : IRequestHandler<GetGoodsReceiveByIdQuery, GetGoodsReceiveByIdResponse?>
{
    private readonly AppDbContext _context;
    public GetGoodsReceiveByIdHandler(AppDbContext context) => _context = context;

    public async Task<GetGoodsReceiveByIdResponse?> Handle(GetGoodsReceiveByIdQuery request, CancellationToken cancellationToken)
    {
        var master = await _context.GoodsReceive
            .AsNoTracking()
            .Include(x => x.PurchaseOrder)
            .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

        if (master == null) return null;

        var details = await _context.InventoryTransaction
            .AsNoTracking()
            .Include(x => x.Product)
            .Include(x => x.Warehouse)
            .Where(x => x.ModuleId == request.Id && x.ModuleName == nameof(Data.Entities.GoodsReceive))
            .Select(x => new GoodsReceiveInvenTransResponse
            {
                Id = x.Id,
                ProductId = x.ProductId,
                ProductName = x.Product!.Name,
                WarehouseId = x.WarehouseId,
                WarehouseName = x.Warehouse!.Name,
                Movement = x.Movement
            })
            .ToListAsync(cancellationToken);

        return new GetGoodsReceiveByIdResponse
        {
            Id = master.Id,
            AutoNumber = master.AutoNumber,
            ReceiveDate = master.ReceiveDate,
            Status = master.Status,
            Description = master.Description,
            PurchaseOrderId = master.PurchaseOrderId,
            PurchaseOrderNumber = master.PurchaseOrder?.AutoNumber,
            CreatedAt = master.CreatedAt,
            CreatedBy = master.CreatedBy,
            UpdatedAt = master.UpdatedAt,
            UpdatedBy = master.UpdatedBy,
            Items = details
        };
    }
}