using CashLineERP.Application.Sales;
using CashLineERP.Contracts.Sales;
using CashLineERP.Infrastructure.Persistence.Repositories;

namespace CashLineERP.Api.Tests;

public sealed class DispatchProofOfDeliveryServiceQaTests
{
    [Fact]
    public void Dispatch_Proof_Of_Delivery_Can_Be_Captured_After_Posting()
    {
        using var dbContext = QaDbContextFactory.CreateContext();

        var service = new DispatchOrderService(new DispatchOrderRepository(dbContext));

        var postedDispatch = service.PostDispatchOrder(1, new PostDispatchOrderRequest(
            ActorUserId: 3,
            ShippedAtUtc: new DateTime(2026, 5, 12, 8, 30, 0, DateTimeKind.Utc),
            DeliveredAtUtc: null,
            ProofOfDeliveryReceiver: null));

        Assert.Equal("POSTED", postedDispatch.StatusCode);

        var podDispatch = service.RecordProofOfDelivery(1, 4, new RecordProofOfDeliveryRequest(
            DeliveredAtUtc: new DateTime(2026, 5, 13, 11, 0, 0, DateTimeKind.Utc),
            ProofOfDeliveryReceiver: "Warehouse Receiver",
            DeliveryConditionCode: "ACCEPTED",
            ProofOfDeliveryReference: "POD-001",
            Notes: "Delivered in good condition"));

        Assert.Equal("Warehouse Receiver", podDispatch.ProofOfDeliveryReceiver);
        Assert.Equal("ACCEPTED", podDispatch.DeliveryConditionCode);
        Assert.Equal("POD-001", podDispatch.ProofOfDeliveryReference);
        Assert.Equal(4L, podDispatch.ProofOfDeliveryCapturedByUserId);
        Assert.All(podDispatch.Lines, line => Assert.Equal(line.QuantityDispatched, line.QuantityDelivered));
    }
}
