using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.CentralUI.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Schemas;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;

namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Design;

/// <summary>
/// Tests for the schema-library read-accessor (M9-T32c) that T30's schema-driven
/// value-entry forms reuse to resolve <c>{"$ref":"lib:Name"}</c> pointers. It projects
/// <see cref="ISharedSchemaRepository"/> onto a name → schema-JSON map (and a single-name
/// lookup) over a fresh DI scope. The repository is substituted and registered scoped so
/// the service's <c>IServiceScopeFactory</c> resolves it exactly as it would at runtime.
/// </summary>
public class SchemaLibraryQueryServiceTests
{
    private static (SchemaLibraryQueryService Service, ISharedSchemaRepository Repo) Build()
    {
        var repo = Substitute.For<ISharedSchemaRepository>();
        var services = new ServiceCollection();
        services.AddScoped(_ => repo);
        var provider = services.BuildServiceProvider();
        var service = new SchemaLibraryQueryService(provider.GetRequiredService<IServiceScopeFactory>());
        return (service, repo);
    }

    [Fact]
    public async Task GetSchemaMapAsync_MapsNameToSchemaJson()
    {
        var (service, repo) = Build();
        repo.ListAsync(Arg.Any<CancellationToken>()).Returns(new List<SharedSchema>
        {
            new() { Id = 1, Name = "Address", SchemaJson = "{\"type\":\"object\"}" },
            new() { Id = 2, Name = "GeoPoint", SchemaJson = "{\"type\":\"array\"}" },
        });

        var map = await service.GetSchemaMapAsync();

        Assert.Equal(2, map.Count);
        Assert.Equal("{\"type\":\"object\"}", map["Address"]);
        Assert.Equal("{\"type\":\"array\"}", map["GeoPoint"]);
    }

    [Fact]
    public async Task GetSchemaMapAsync_EmptyLibrary_ReturnsEmptyMap()
    {
        var (service, repo) = Build();
        repo.ListAsync(Arg.Any<CancellationToken>()).Returns(new List<SharedSchema>());

        var map = await service.GetSchemaMapAsync();

        Assert.Empty(map);
    }

    [Fact]
    public async Task ResolveAsync_KnownName_ReturnsSchemaJson()
    {
        var (service, repo) = Build();
        repo.GetByNameAsync("Address", Arg.Any<CancellationToken>())
            .Returns(new SharedSchema { Id = 1, Name = "Address", SchemaJson = "{\"type\":\"object\"}" });

        var json = await service.ResolveAsync("Address");

        Assert.Equal("{\"type\":\"object\"}", json);
    }

    [Fact]
    public async Task ResolveAsync_UnknownName_ReturnsNull()
    {
        var (service, repo) = Build();
        repo.GetByNameAsync("Missing", Arg.Any<CancellationToken>()).Returns((SharedSchema?)null);

        Assert.Null(await service.ResolveAsync("Missing"));
    }

    [Fact]
    public async Task ResolveAsync_EmptyName_ReturnsNull_WithoutHittingRepo()
    {
        var (service, repo) = Build();

        Assert.Null(await service.ResolveAsync(""));
        await repo.DidNotReceive().GetByNameAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
    }
}
