using Moq;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Services;

namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests.Services;

public class InstanceServiceTests
{
    private readonly Mock<ITemplateEngineRepository> _repoMock = new();
    private readonly Mock<IAuditService> _auditMock = new();
    private readonly InstanceService _sut;

    public InstanceServiceTests()
    {
        _sut = new InstanceService(_repoMock.Object, _auditMock.Object);
    }

    [Fact]
    public async Task CreateInstance_ValidInput_ReturnsSuccess()
    {
        _repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new Template("TestTemplate") { Id = 1 });
        _repoMock.Setup(r => r.GetInstanceByUniqueNameAsync("Inst1", It.IsAny<CancellationToken>()))
            .ReturnsAsync((Instance?)null);
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        var result = await _sut.CreateInstanceAsync("Inst1", 1, 1, null, "admin");

        Assert.True(result.IsSuccess);
        Assert.Equal("Inst1", result.Value.UniqueName);
        Assert.Equal(InstanceState.Disabled, result.Value.State); // Starts disabled
        _repoMock.Verify(r => r.AddInstanceAsync(It.IsAny<Instance>(), It.IsAny<CancellationToken>()), Times.Once);
    }

    [Fact]
    public async Task CreateInstance_DuplicateName_ReturnsFailure()
    {
        _repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new Template("T") { Id = 1 });
        _repoMock.Setup(r => r.GetInstanceByUniqueNameAsync("Inst1", It.IsAny<CancellationToken>()))
            .ReturnsAsync(new Instance("Inst1") { Id = 99 });

        var result = await _sut.CreateInstanceAsync("Inst1", 1, 1, null, "admin");

        Assert.True(result.IsFailure);
        Assert.Contains("already exists", result.Error);
    }

    [Fact]
    public async Task CreateInstance_MissingTemplate_ReturnsFailure()
    {
        _repoMock.Setup(r => r.GetTemplateByIdAsync(999, It.IsAny<CancellationToken>()))
            .ReturnsAsync((Template?)null);

        var result = await _sut.CreateInstanceAsync("Inst1", 999, 1, null, "admin");

        Assert.True(result.IsFailure);
        Assert.Contains("not found", result.Error);
    }

    [Fact]
    public async Task SetAttributeOverride_LockedAttribute_ReturnsFailure()
    {
        var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.GetAttributesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<TemplateAttribute>
            {
                new("LockedAttr") { IsLocked = true }
            });

        var result = await _sut.SetAttributeOverrideAsync(1, "LockedAttr", "new", "admin");

        Assert.True(result.IsFailure);
        Assert.Contains("locked", result.Error, StringComparison.OrdinalIgnoreCase);
    }

    [Fact]
    public async Task SetAttributeOverride_NonExistentAttribute_ReturnsFailure()
    {
        var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.GetAttributesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<TemplateAttribute>());

        var result = await _sut.SetAttributeOverrideAsync(1, "Missing", "value", "admin");

        Assert.True(result.IsFailure);
        Assert.Contains("does not exist", result.Error);
    }

    [Fact]
    public async Task SetAttributeOverride_UnlockedAttribute_ReturnsSuccess()
    {
        var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.GetAttributesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<TemplateAttribute>
            {
                new("Threshold") { IsLocked = false }
            });
        _repoMock.Setup(r => r.GetOverridesByInstanceIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<InstanceAttributeOverride>());
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        var result = await _sut.SetAttributeOverrideAsync(1, "Threshold", "99", "admin");

        Assert.True(result.IsSuccess);
        Assert.Equal("Threshold", result.Value.AttributeName);
        Assert.Equal("99", result.Value.OverrideValue);
    }

    // --- TemplateEngine-008 regression: SetAlarmOverrideAsync validation ---

    private static Template TemplateWithAlarms(int id, params TemplateAlarm[] alarms)
    {
        var t = new Template($"T{id}") { Id = id };
        foreach (var a in alarms)
        {
            a.TemplateId = id;
            t.Alarms.Add(a);
        }
        return t;
    }

    [Fact]
    public async Task SetAlarmOverride_NonExistentAlarm_ReturnsFailure()
    {
        var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<Template>
            {
                TemplateWithAlarms(1, new TemplateAlarm("HighTemp") { Id = 10 })
            });

        var result = await _sut.SetAlarmOverrideAsync(1, "Missing", "{}", null, "admin");

        Assert.True(result.IsFailure);
        Assert.Contains("does not exist", result.Error);
        _repoMock.Verify(r => r.AddInstanceAlarmOverrideAsync(
            It.IsAny<InstanceAlarmOverride>(), It.IsAny<CancellationToken>()), Times.Never);
    }

    [Fact]
    public async Task SetAlarmOverride_ComposedLockedAlarm_ReturnsFailure()
    {
        // The locked alarm lives in a composed module, so it is NOT a direct
        // alarm of the instance's template — the old code skipped the lock check.
        var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);

        var module = TemplateWithAlarms(2, new TemplateAlarm("Fault") { Id = 20, IsLocked = true });
        var host = new Template("Host") { Id = 1 };
        host.Compositions.Add(new TemplateComposition("Pump") { Id = 1, ComposedTemplateId = 2 });

        _repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<Template> { host, module });

        var result = await _sut.SetAlarmOverrideAsync(1, "Pump.Fault", "{}", null, "admin");

        Assert.True(result.IsFailure);
        Assert.Contains("locked", result.Error, StringComparison.OrdinalIgnoreCase);
        _repoMock.Verify(r => r.AddInstanceAlarmOverrideAsync(
            It.IsAny<InstanceAlarmOverride>(), It.IsAny<CancellationToken>()), Times.Never);
    }

    [Fact]
    public async Task SetAlarmOverride_ComposedUnlockedAlarm_ReturnsSuccess()
    {
        var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);

        var module = TemplateWithAlarms(2, new TemplateAlarm("Fault") { Id = 20, IsLocked = false });
        var host = new Template("Host") { Id = 1 };
        host.Compositions.Add(new TemplateComposition("Pump") { Id = 1, ComposedTemplateId = 2 });

        _repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<Template> { host, module });
        _repoMock.Setup(r => r.GetAlarmOverrideAsync(1, "Pump.Fault", It.IsAny<CancellationToken>()))
            .ReturnsAsync((InstanceAlarmOverride?)null);
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        var result = await _sut.SetAlarmOverrideAsync(1, "Pump.Fault", "{}", 2, "admin");

        Assert.True(result.IsSuccess);
        _repoMock.Verify(r => r.AddInstanceAlarmOverrideAsync(
            It.IsAny<InstanceAlarmOverride>(), It.IsAny<CancellationToken>()), Times.Once);
    }

    [Fact]
    public async Task SetAlarmOverride_DirectLockedAlarm_ReturnsFailure()
    {
        var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<Template>
            {
                TemplateWithAlarms(1, new TemplateAlarm("HighTemp") { Id = 10, IsLocked = true })
            });

        var result = await _sut.SetAlarmOverrideAsync(1, "HighTemp", "{}", null, "admin");

        Assert.True(result.IsFailure);
        Assert.Contains("locked", result.Error, StringComparison.OrdinalIgnoreCase);
    }

    [Fact]
    public async Task Enable_ExistingInstance_SetsEnabled()
    {
        var instance = new Instance("Inst1") { Id = 1, State = InstanceState.Disabled };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        var result = await _sut.EnableAsync(1, "admin");

        Assert.True(result.IsSuccess);
        Assert.Equal(InstanceState.Enabled, result.Value.State);
    }

    [Fact]
    public async Task Disable_ExistingInstance_SetsDisabled()
    {
        var instance = new Instance("Inst1") { Id = 1, State = InstanceState.Enabled };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        var result = await _sut.DisableAsync(1, "admin");

        Assert.True(result.IsSuccess);
        Assert.Equal(InstanceState.Disabled, result.Value.State);
    }

    [Fact]
    public async Task SetConnectionBindings_BulkAssignment_Success()
    {
        var instance = new Instance("Inst1") { Id = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.GetBindingsByInstanceIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<InstanceConnectionBinding>());
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        var bindings = new List<ConnectionBinding> { new("Temp", 100), new("Pressure", 200) };
        var result = await _sut.SetConnectionBindingsAsync(1, bindings, "admin");

        Assert.True(result.IsSuccess);
        Assert.Equal(2, result.Value.Count);
        _repoMock.Verify(r => r.AddInstanceConnectionBindingAsync(It.IsAny<InstanceConnectionBinding>(), It.IsAny<CancellationToken>()), Times.Exactly(2));
    }

    // --- NJ-2: ElementDataType stamping on SetAttributeOverrideAsync ---

    [Fact]
    public async Task SetAttributeOverride_CreatePath_ListAttribute_StampsElementDataType()
    {
        var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.GetAttributesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<TemplateAttribute>
            {
                new("Tags")
                {
                    IsLocked = false,
                    DataType = DataType.List,
                    ElementDataType = DataType.String
                }
            });
        _repoMock.Setup(r => r.GetOverridesByInstanceIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<InstanceAttributeOverride>()); // no existing override → create path
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        InstanceAttributeOverride? captured = null;
        _repoMock.Setup(r => r.AddInstanceAttributeOverrideAsync(It.IsAny<InstanceAttributeOverride>(), It.IsAny<CancellationToken>()))
            .Callback<InstanceAttributeOverride, CancellationToken>((o, _) => captured = o);

        var result = await _sut.SetAttributeOverrideAsync(1, "Tags", "[\"a\"]", "admin");

        Assert.True(result.IsSuccess);
        Assert.NotNull(captured);
        Assert.Equal(DataType.String, captured.ElementDataType);
    }

    [Fact]
    public async Task SetAttributeOverride_UpdatePath_ListAttribute_StampsElementDataType()
    {
        var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.GetAttributesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<TemplateAttribute>
            {
                new("Tags")
                {
                    IsLocked = false,
                    DataType = DataType.List,
                    ElementDataType = DataType.String
                }
            });

        var existingOverride = new InstanceAttributeOverride("Tags") { Id = 42, InstanceId = 1, OverrideValue = "[\"old\"]" };
        _repoMock.Setup(r => r.GetOverridesByInstanceIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<InstanceAttributeOverride> { existingOverride }); // pre-existing → update path
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        InstanceAttributeOverride? captured = null;
        _repoMock.Setup(r => r.UpdateInstanceAttributeOverrideAsync(It.IsAny<InstanceAttributeOverride>(), It.IsAny<CancellationToken>()))
            .Callback<InstanceAttributeOverride, CancellationToken>((o, _) => captured = o);

        var result = await _sut.SetAttributeOverrideAsync(1, "Tags", "[\"new\"]", "admin");

        Assert.True(result.IsSuccess);
        Assert.NotNull(captured);
        Assert.Equal(DataType.String, captured.ElementDataType);
    }

    [Fact]
    public async Task SetAttributeOverride_CreatePath_ScalarAttribute_ElementDataTypeIsNull()
    {
        var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.GetAttributesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<TemplateAttribute>
            {
                new("Threshold")
                {
                    IsLocked = false,
                    DataType = DataType.Float,
                    ElementDataType = null
                }
            });
        _repoMock.Setup(r => r.GetOverridesByInstanceIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<InstanceAttributeOverride>());
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        InstanceAttributeOverride? captured = null;
        _repoMock.Setup(r => r.AddInstanceAttributeOverrideAsync(It.IsAny<InstanceAttributeOverride>(), It.IsAny<CancellationToken>()))
            .Callback<InstanceAttributeOverride, CancellationToken>((o, _) => captured = o);

        var result = await _sut.SetAttributeOverrideAsync(1, "Threshold", "3.14", "admin");

        Assert.True(result.IsSuccess);
        Assert.NotNull(captured);
        Assert.Null(captured.ElementDataType);
    }

    [Fact]
    public async Task AssignToArea_AreaInDifferentSite_ReturnsFailure()
    {
        var instance = new Instance("Inst1") { Id = 1, SiteId = 1 };
        _repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(instance);
        _repoMock.Setup(r => r.GetAreaByIdAsync(5, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new Area("WrongSiteArea") { Id = 5, SiteId = 99 });

        var result = await _sut.AssignToAreaAsync(1, 5, "admin");

        Assert.True(result.IsFailure);
        Assert.Contains("does not belong", result.Error);
    }
}
