using ScadaLink.Commons.Messages.Integration;
using ScadaLink.Commons.Messages.RemoteQuery;

namespace ScadaLink.Communication.Tests;

/// <summary>
/// WP-1: Tests that message contracts have correlation IDs and proper structure.
/// </summary>
public class MessageContractTests
{
    [Fact]
    public void IntegrationCallRequest_HasCorrelationId()
    {
        var msg = new IntegrationCallRequest(
            "corr-123", "site1", "inst1", "ExtSys1", "GetData",
            new Dictionary<string, object?>(), DateTimeOffset.UtcNow);

        Assert.Equal("corr-123", msg.CorrelationId);
    }

    [Fact]
    public void IntegrationCallResponse_HasCorrelationId()
    {
        var msg = new IntegrationCallResponse(
            "corr-123", "site1", true, "{}", null, DateTimeOffset.UtcNow);

        Assert.Equal("corr-123", msg.CorrelationId);
    }

    [Fact]
    public void EventLogQueryRequest_HasCorrelationId()
    {
        var msg = new EventLogQueryRequest(
            "corr-456", "site1", null, null, null, null, null, null, null, 25, DateTimeOffset.UtcNow);

        Assert.Equal("corr-456", msg.CorrelationId);
    }

    [Fact]
    public void EventLogQueryResponse_HasCorrelationId()
    {
        var msg = new EventLogQueryResponse(
            "corr-456", "site1", [], null, false, true, null, DateTimeOffset.UtcNow);

        Assert.Equal("corr-456", msg.CorrelationId);
    }

    [Fact]
    public void ParkedMessageQueryRequest_HasCorrelationId()
    {
        var msg = new ParkedMessageQueryRequest(
            "corr-789", "site1", 1, 25, DateTimeOffset.UtcNow);

        Assert.Equal("corr-789", msg.CorrelationId);
    }

    [Fact]
    public void ParkedMessageQueryResponse_HasCorrelationId()
    {
        var msg = new ParkedMessageQueryResponse(
            "corr-789", "site1", [], 0, 1, 25, true, null, DateTimeOffset.UtcNow);

        Assert.Equal("corr-789", msg.CorrelationId);
    }

    [Fact]
    public void AllMessagePatterns_ExistAsRecordTypes()
    {
        // Verify all 8 patterns have proper request/response types
        // Pattern 1: Deployment
        Assert.True(typeof(Commons.Messages.Deployment.DeployInstanceCommand).IsValueType == false);
        Assert.True(typeof(Commons.Messages.Deployment.DeploymentStatusResponse).IsValueType == false);

        // Pattern 2: Lifecycle
        Assert.True(typeof(Commons.Messages.Lifecycle.DisableInstanceCommand).IsValueType == false);
        Assert.True(typeof(Commons.Messages.Lifecycle.InstanceLifecycleResponse).IsValueType == false);

        // Pattern 3: Artifacts
        Assert.True(typeof(Commons.Messages.Artifacts.DeployArtifactsCommand).IsValueType == false);
        Assert.True(typeof(Commons.Messages.Artifacts.ArtifactDeploymentResponse).IsValueType == false);

        // Pattern 4: Integration
        Assert.True(typeof(IntegrationCallRequest).IsValueType == false);
        Assert.True(typeof(IntegrationCallResponse).IsValueType == false);

        // Pattern 5: Debug View
        Assert.True(typeof(Commons.Messages.DebugView.SubscribeDebugViewRequest).IsValueType == false);
        Assert.True(typeof(Commons.Messages.DebugView.DebugViewSnapshot).IsValueType == false);

        // Pattern 6: Health
        Assert.True(typeof(Commons.Messages.Health.SiteHealthReport).IsValueType == false);

        // Pattern 7: Remote Queries
        Assert.True(typeof(EventLogQueryRequest).IsValueType == false);
        Assert.True(typeof(EventLogQueryResponse).IsValueType == false);
        Assert.True(typeof(ParkedMessageQueryRequest).IsValueType == false);
        Assert.True(typeof(ParkedMessageQueryResponse).IsValueType == false);

        // Pattern 8: Heartbeat
        Assert.True(typeof(Commons.Messages.Health.HeartbeatMessage).IsValueType == false);
    }
}
