using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Deployment;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts;

namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.Deployment;

/// <summary>
/// Site Runtime S3: the pure deploy-time compile validator. It mirrors the
/// compile work <see cref="ZB.MOM.WW.ScadaBridge.SiteRuntime.Actors.InstanceActor"/>
/// does at runtime (scripts, on-trigger scripts, Expression triggers) so the
/// Deployment Manager can reject a non-compiling deployment before any actor is
/// created — WITHOUT touching actor state (safe on a Task.Run thread).
/// </summary>
public class DeployCompileValidatorTests
{
    private static DeployCompileValidator NewValidator() =>
        new(new ScriptCompilationService(NullLogger<ScriptCompilationService>.Instance));

    private static string ConfigWith(string scriptCode) => JsonSerializer.Serialize(new FlattenedConfiguration
    {
        Scripts = new List<ResolvedScript>
        {
            new() { CanonicalName = "S1", Code = scriptCode, TriggerType = "Call" }
        },
        Attributes = new List<ResolvedAttribute>(),
        Alarms = new List<ResolvedAlarm>(),
        NativeAlarmSources = new List<ResolvedNativeAlarmSource>()
    });

    [Fact]
    public void BrokenScript_ReturnsErrors()
    {
        var errors = NewValidator().Validate(ConfigWith("this is not C# ~~~"));
        Assert.NotEmpty(errors);
        Assert.Contains(errors, e => e.Contains("S1"));
    }

    [Fact]
    public void ValidScript_ReturnsNoErrors()
    {
        Assert.Empty(NewValidator().Validate(ConfigWith("return 1 + 1;")));
    }

    [Fact]
    public void BrokenTriggerExpression_ReturnsErrors()
    {
        // One Expression-triggered script whose trigger expression "1 +" does not compile.
        var cfg = JsonSerializer.Serialize(new FlattenedConfiguration
        {
            Scripts = new List<ResolvedScript>
            {
                new()
                {
                    CanonicalName = "S1",
                    Code = "return 1;",
                    TriggerType = "Expression",
                    TriggerConfiguration = JsonSerializer.Serialize(new { expression = "1 +" })
                }
            },
            Attributes = new List<ResolvedAttribute>(),
            Alarms = new List<ResolvedAlarm>(),
            NativeAlarmSources = new List<ResolvedNativeAlarmSource>()
        });

        var errors = NewValidator().Validate(cfg);
        Assert.NotEmpty(errors);
        Assert.Contains(errors, e => e.Contains("script-trigger-S1"));
    }

    [Fact]
    public void MalformedJson_ReturnsSingleDeserializeError()
    {
        var errors = NewValidator().Validate("{ this is not json");
        Assert.Single(errors);
        Assert.Contains("deserialize", errors[0], StringComparison.OrdinalIgnoreCase);
    }

    [Fact]
    public void MissingOnTriggerScript_ReturnsError()
    {
        var cfg = JsonSerializer.Serialize(new FlattenedConfiguration
        {
            Scripts = new List<ResolvedScript>(),
            Attributes = new List<ResolvedAttribute>(),
            Alarms = new List<ResolvedAlarm>
            {
                new()
                {
                    CanonicalName = "A1",
                    TriggerType = "HiLo",
                    OnTriggerScriptCanonicalName = "DoesNotExist"
                }
            },
            NativeAlarmSources = new List<ResolvedNativeAlarmSource>()
        });

        var errors = NewValidator().Validate(cfg);
        Assert.Contains(errors, e => e.Contains("A1") && e.Contains("DoesNotExist"));
    }
}
