using Moq;
using ScadaLink.Commons.Entities.Instances;
using ScadaLink.Commons.Entities.Templates;
using ScadaLink.Commons.Interfaces.Repositories;
using ScadaLink.Commons.Interfaces.Services;
using ScadaLink.Commons.Types.Enums;
using ScadaLink.TemplateEngine.Services;

namespace ScadaLink.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);
    }

    [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<(string, int)> { ("Temp", 100), ("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));
    }

    [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);
    }
}
