using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase;

namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests;

public class NativeAlarmSourceSchemaTests
{
    private static ScadaBridgeDbContext NewContext()
    {
        var opts = new DbContextOptionsBuilder<ScadaBridgeDbContext>()
            .UseSqlite("DataSource=:memory:")
            .Options;
        var ctx = new ScadaBridgeDbContext(opts);
        ctx.Database.OpenConnection();
        ctx.Database.EnsureCreated();
        return ctx;
    }

    [Fact]
    public async Task TemplateNativeAlarmSource_PersistsViaTemplate()
    {
        using var ctx = NewContext();

        var t = new Template("T1");
        t.NativeAlarmSources.Add(new TemplateNativeAlarmSource("Src1") { ConnectionName = "C", SourceReference = "r" });
        ctx.Templates.Add(t);
        await ctx.SaveChangesAsync();

        Assert.Single(ctx.TemplateNativeAlarmSources);
    }

    [Fact]
    public async Task InstanceNativeAlarmSourceOverride_DbSetIsMapped()
    {
        using var ctx = NewContext();
        Assert.Equal(0, await ctx.InstanceNativeAlarmSourceOverrides.CountAsync());
    }
}
