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

namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;

public class DeliveryOrderInvenTransResponse
{
    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 GetDeliveryOrderByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public DateTime? DeliveryDate { get; set; }
    public Data.Enums.DeliveryOrderStatus Status { get; set; }
    public string? Description { get; set; }
    public string? SalesOrderId { get; set; }
    public string? SalesOrderNumber { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
    public List<DeliveryOrderInvenTransResponse> Items { get; set; } = new();
}

public record GetDeliveryOrderByIdQuery(string Id) : IRequest<GetDeliveryOrderByIdResponse?>;

public class GetDeliveryOrderByIdHandler : IRequestHandler<GetDeliveryOrderByIdQuery, GetDeliveryOrderByIdResponse?>
{
    private readonly AppDbContext _context;
    public GetDeliveryOrderByIdHandler(AppDbContext context) => _context = context;

    public async Task<GetDeliveryOrderByIdResponse?> Handle(GetDeliveryOrderByIdQuery request, CancellationToken cancellationToken)
    {
        var master = await _context.DeliveryOrder
            .AsNoTracking()
            .Include(x => x.SalesOrder)
            .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.DeliveryOrder))
            .Select(x => new DeliveryOrderInvenTransResponse
            {
                Id = x.Id,
                ProductId = x.ProductId,
                ProductName = x.Product!.Name,
                WarehouseId = x.WarehouseId,
                WarehouseName = x.Warehouse!.Name,
                Movement = x.Movement
            })
            .ToListAsync(cancellationToken);

        return new GetDeliveryOrderByIdResponse
        {
            Id = master.Id,
            AutoNumber = master.AutoNumber,
            DeliveryDate = master.DeliveryDate,
            Status = master.Status,
            Description = master.Description,
            SalesOrderId = master.SalesOrderId,
            SalesOrderNumber = master.SalesOrder?.AutoNumber,
            CreatedAt = master.CreatedAt,
            CreatedBy = master.CreatedBy,
            UpdatedAt = master.UpdatedAt,
            UpdatedBy = master.UpdatedBy,
            Items = details
        };
    }
}