using ZB.MOM.WW.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Integration;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;

namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Messages.Integration;

/// <summary>
/// Audit Log (#23) telemetry handoff: envelope + pull request/response DTOs.
/// At-least-once from sites; idempotent at central on <see cref="AuditEvent.EventId"/>.
/// </summary>
public class AuditTelemetryMessagesTests
{
    // C3 (Task 2.5): canonical ZB.MOM.WW.Audit.AuditEvent built via the shared
    // factory (domain fields ride in DetailsJson). These tests assert only the
    // envelope/response DTO behaviour, so the audit row's contents are incidental.
    private static AuditEvent MakeEvent(Guid? id = null) =>
        ScadaBridgeAuditEventFactory.Create(
            channel: AuditChannel.ApiOutbound,
            kind: AuditKind.ApiCall,
            status: AuditStatus.Delivered,
            eventId: id ?? Guid.NewGuid());

    [Fact]
    public void AuditTelemetryEnvelope_ConstructsWithThreeEvents_AndIsEnumerable()
    {
        var envelopeId = Guid.NewGuid();
        var events = new List<AuditEvent> { MakeEvent(), MakeEvent(), MakeEvent() };

        var envelope = new AuditTelemetryEnvelope(envelopeId, "site-01", events);

        Assert.Equal(envelopeId, envelope.EnvelopeId);
        Assert.Equal("site-01", envelope.SourceSiteId);
        Assert.Equal(3, envelope.Events.Count);

        // Enumerable round-trip
        var collected = new List<AuditEvent>();
        foreach (var e in envelope.Events)
        {
            collected.Add(e);
        }
        Assert.Equal(3, collected.Count);
    }

    [Fact]
    public void AuditTelemetryEnvelope_IsImmutable_RecordEqualityOnReferenceIdentityOfList()
    {
        // The record's value equality compares the IReadOnlyList reference; two envelopes
        // built with the same list instance + same fields must be equal, but using a
        // different list instance (even with equal content) must NOT be equal.
        var events = new List<AuditEvent> { MakeEvent() } as IReadOnlyList<AuditEvent>;
        var envelopeId = Guid.NewGuid();
        var a = new AuditTelemetryEnvelope(envelopeId, "site-01", events);
        var b = new AuditTelemetryEnvelope(envelopeId, "site-01", events);

        Assert.Equal(a, b);
        Assert.Equal(a.GetHashCode(), b.GetHashCode());

        var withDifferentSite = a with { SourceSiteId = "site-02" };
        Assert.NotEqual(a, withDifferentSite);
        Assert.Equal("site-02", withDifferentSite.SourceSiteId);
        Assert.Equal("site-01", a.SourceSiteId);
    }

    [Fact]
    public void PullAuditEventsRequest_ConstructsAndIsImmutable()
    {
        var since = new DateTime(2026, 5, 20, 0, 0, 0, DateTimeKind.Utc);
        var request = new PullAuditEventsRequest("site-01", since, 100);

        Assert.Equal("site-01", request.SourceSiteId);
        Assert.Equal(since, request.SinceUtc);
        Assert.Equal(100, request.BatchSize);

        var bigger = request with { BatchSize = 500 };
        Assert.Equal(100, request.BatchSize);
        Assert.Equal(500, bigger.BatchSize);
    }

    [Fact]
    public void PullAuditEventsResponse_ConstructsWithMoreAvailableTrue_AndIsEnumerable()
    {
        var events = new List<AuditEvent> { MakeEvent(), MakeEvent() };
        var response = new PullAuditEventsResponse(events, MoreAvailable: true);

        Assert.True(response.MoreAvailable);
        Assert.Equal(2, response.Events.Count);

        var collected = new List<AuditEvent>();
        foreach (var e in response.Events)
        {
            collected.Add(e);
        }
        Assert.Equal(2, collected.Count);
    }

    [Fact]
    public void PullAuditEventsResponse_WithExpression_ChangesSingleField()
    {
        var response = new PullAuditEventsResponse(new List<AuditEvent>(), MoreAvailable: false);
        var updated = response with { MoreAvailable = true };

        Assert.False(response.MoreAvailable);
        Assert.True(updated.MoreAvailable);
    }
}
