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

namespace Indotalent.Features.Inventory.TransferOut.Cqrs;

public class TransferOutItemResponse
{
    public string? Id { get; set; }
    public string? ProductId { get; set; }
    public string? ProductName { get; set; }
    public double? Movement { get; set; }
}

public class GetTransferOutByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public DateTime? TransferReleaseDate { get; set; }
    public Data.Enums.TransferStatus Status { get; set; }
    public string? Description { get; set; }
    public string? WarehouseFromId { get; set; }
    public string? WarehouseFromName { get; set; }
    public string? WarehouseToId { get; set; }
    public string? WarehouseToName { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
    public List<TransferOutItemResponse> Items { get; set; } = new();
}

public record GetTransferOutByIdQuery(string Id) : IRequest<GetTransferOutByIdResponse?>;

public class GetTransferOutByIdHandler : IRequestHandler<GetTransferOutByIdQuery, GetTransferOutByIdResponse?>
{
    private readonly AppDbContext _context;
    public GetTransferOutByIdHandler(AppDbContext context) => _context = context;

    public async Task<GetTransferOutByIdResponse?> Handle(GetTransferOutByIdQuery request, CancellationToken cancellationToken)
    {
        var master = await _context.TransferOut
            .AsNoTracking()
            .Include(x => x.WarehouseFrom)
            .Include(x => x.WarehouseTo)
            .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

        if (master == null) return null;

        var items = await _context.InventoryTransaction
            .AsNoTracking()
            .Include(x => x.Product)
            .Where(x => x.ModuleId == request.Id && x.ModuleName == nameof(Data.Entities.TransferOut))
            .Select(x => new TransferOutItemResponse
            {
                Id = x.Id,
                ProductId = x.ProductId,
                ProductName = x.Product!.Name,
                Movement = x.Movement
            }).ToListAsync(cancellationToken);

        return new GetTransferOutByIdResponse
        {
            Id = master.Id,
            AutoNumber = master.AutoNumber,
            TransferReleaseDate = master.TransferReleaseDate,
            Status = master.Status,
            Description = master.Description,
            WarehouseFromId = master.WarehouseFromId,
            WarehouseFromName = master.WarehouseFrom?.Name,
            WarehouseToId = master.WarehouseToId,
            WarehouseToName = master.WarehouseTo?.Name,
            CreatedAt = master.CreatedAt,
            CreatedBy = master.CreatedBy,
            UpdatedAt = master.UpdatedAt,
            UpdatedBy = master.UpdatedBy,
            Items = items
        };
    }
}