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

namespace Indotalent.Features.Sales.SalesReturn.Cqrs;

public class SalesReturnInvenTransResponse
{
    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 GetSalesReturnByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public DateTime? ReturnDate { get; set; }
    public Data.Enums.SalesReturnStatus Status { get; set; }
    public string? Description { get; set; }
    public string? DeliveryOrderId { get; set; }
    public string? DeliveryOrderNumber { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
    public List<SalesReturnInvenTransResponse> Items { get; set; } = new();
}

public record GetSalesReturnByIdQuery(string Id) : IRequest<GetSalesReturnByIdResponse?>;

public class GetSalesReturnByIdHandler : IRequestHandler<GetSalesReturnByIdQuery, GetSalesReturnByIdResponse?>
{
    private readonly AppDbContext _context;
    public GetSalesReturnByIdHandler(AppDbContext context) => _context = context;

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

        return new GetSalesReturnByIdResponse
        {
            Id = master.Id,
            AutoNumber = master.AutoNumber,
            ReturnDate = master.ReturnDate,
            Status = master.Status,
            Description = master.Description,
            DeliveryOrderId = master.DeliveryOrderId,
            DeliveryOrderNumber = master.DeliveryOrder?.AutoNumber,
            CreatedAt = master.CreatedAt,
            CreatedBy = master.CreatedBy,
            UpdatedAt = master.UpdatedAt,
            UpdatedBy = master.UpdatedBy,
            Items = details
        };
    }
}