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

namespace CashLineERP.Api.Tests;

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

        var service = new SupplierStatementService(new SupplierStatementRepository(dbContext));

        var statement = service.GenerateStatement(3, new CreateSupplierStatementRequest(
            SupplierId: 1,
            DateFrom: new DateOnly(2026, 4, 1),
            DateTo: new DateOnly(2026, 5, 31),
            CurrencyCode: "EGP"));

        var delivery = service.RecordDelivery(statement.SupplierStatementId, 4, new RecordSupplierStatementDeliveryRequest(
            DeliveredAtUtc: new DateTime(2026, 5, 31, 12, 0, 0, DateTimeKind.Utc),
            ChannelCode: "portal",
            RecipientAddress: "supplier-admin",
            DeliveryStatusCode: "published",
            Notes: "Published to supplier portal"));

        Assert.Equal(statement.SupplierStatementId, delivery.SupplierStatementId);
        Assert.Equal("PORTAL", delivery.ChannelCode);

        var acknowledged = service.AcknowledgeDelivery(delivery.SupplierStatementDeliveryLogId, 5, new AcknowledgeSupplierStatementDeliveryRequest(
            AcknowledgmentStatusCode: "ACKNOWLEDGED",
            Notes: "Supplier confirmed receipt"));

        Assert.Equal("ACKNOWLEDGED", acknowledged.AcknowledgmentStatusCode);
        Assert.Equal(5L, acknowledged.AcknowledgedByUserId);

        var logs = service.GetDeliveryLogs(statement.SupplierStatementId);
        Assert.Single(logs);
        Assert.Equal("ACKNOWLEDGED", logs.Single().AcknowledgmentStatusCode);
    }
}
