using Moq;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Services;

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

public class SiteServiceTests
{
    private readonly Mock<ISiteRepository> _repoMock = new();
    private readonly Mock<IAuditService> _auditMock = new();
    private readonly SiteService _sut;

    public SiteServiceTests()
    {
        _sut = new SiteService(_repoMock.Object, _auditMock.Object);
    }

    [Fact]
    public async Task CreateSite_ValidInput_ReturnsSuccess()
    {
        _repoMock.Setup(r => r.GetSiteByIdentifierAsync("SITE-001", It.IsAny<CancellationToken>()))
            .ReturnsAsync((Site?)null);
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        var result = await _sut.CreateSiteAsync("Plant Alpha", "SITE-001", "Main plant", "admin");

        Assert.True(result.IsSuccess);
        Assert.Equal("Plant Alpha", result.Value.Name);
        Assert.Equal("SITE-001", result.Value.SiteIdentifier);
    }

    [Fact]
    public async Task CreateSite_DuplicateIdentifier_ReturnsFailure()
    {
        _repoMock.Setup(r => r.GetSiteByIdentifierAsync("SITE-001", It.IsAny<CancellationToken>()))
            .ReturnsAsync(new Site("Existing", "SITE-001") { Id = 1 });

        var result = await _sut.CreateSiteAsync("New", "SITE-001", null, "admin");

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

    [Fact]
    public async Task CreateSite_EmptyName_ReturnsFailure()
    {
        var result = await _sut.CreateSiteAsync("", "SITE-001", null, "admin");

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

    [Fact]
    public async Task DeleteSite_WithInstances_ReturnsFailure()
    {
        _repoMock.Setup(r => r.GetSiteByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new Site("Plant", "SITE-001") { Id = 1 });
        _repoMock.Setup(r => r.GetInstancesBySiteIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<Instance>
            {
                new("Inst1") { Id = 1, SiteId = 1 },
                new("Inst2") { Id = 2, SiteId = 1 }
            });

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

        Assert.True(result.IsFailure);
        Assert.Contains("2 instance(s)", result.Error);
    }

    [Fact]
    public async Task DeleteSite_NoInstances_Success()
    {
        _repoMock.Setup(r => r.GetSiteByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new Site("Plant", "SITE-001") { Id = 1 });
        _repoMock.Setup(r => r.GetInstancesBySiteIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new List<Instance>());
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

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

        Assert.True(result.IsSuccess);
    }

    [Fact]
    public async Task CreateDataConnection_ValidInput_Success()
    {
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        var result = await _sut.CreateDataConnectionAsync(1, "OPC-Server1", "OpcUa", "{\"url\":\"opc.tcp://localhost\"}", null, 3, "admin");

        Assert.True(result.IsSuccess);
        Assert.Equal("OPC-Server1", result.Value.Name);
        Assert.Equal("OpcUa", result.Value.Protocol);
        Assert.Equal(1, result.Value.SiteId);
    }

    [Fact]
    public async Task UpdateSite_ValidInput_Success()
    {
        _repoMock.Setup(r => r.GetSiteByIdAsync(1, It.IsAny<CancellationToken>()))
            .ReturnsAsync(new Site("Old", "S1") { Id = 1 });
        _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
            .ReturnsAsync(1);

        var result = await _sut.UpdateSiteAsync(1, "New Name", "New desc", "admin");

        Assert.True(result.IsSuccess);
        Assert.Equal("New Name", result.Value.Name);
    }
}
