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

namespace Indotalent.Features.Purchase.PurchaseReturn.Cqrs;

public class PurchaseReturnInvenTransResponse
{
    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 GetPurchaseReturnByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public DateTime? ReturnDate { get; set; }
    public Data.Enums.PurchaseReturnStatus Status { get; set; }
    public string? Description { get; set; }
    public string? GoodsReceiveId { get; set; }
    public string? GoodsReceiveNumber { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
    public List<PurchaseReturnInvenTransResponse> Items { get; set; } = new();
}

public record GetPurchaseReturnByIdQuery(string Id) : IRequest<GetPurchaseReturnByIdResponse?>;

public class GetPurchaseReturnByIdHandler : IRequestHandler<GetPurchaseReturnByIdQuery, GetPurchaseReturnByIdResponse?>
{
    private readonly AppDbContext _context;
    public GetPurchaseReturnByIdHandler(AppDbContext context) => _context = context;

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

        return new GetPurchaseReturnByIdResponse
        {
            Id = master.Id,
            AutoNumber = master.AutoNumber,
            ReturnDate = master.ReturnDate,
            Status = master.Status,
            Description = master.Description,
            GoodsReceiveId = master.GoodsReceiveId,
            GoodsReceiveNumber = master.GoodsReceive?.AutoNumber,
            CreatedAt = master.CreatedAt,
            CreatedBy = master.CreatedBy,
            UpdatedAt = master.UpdatedAt,
            UpdatedBy = master.UpdatedBy,
            Items = details
        };
    }
}