using e_suite.UnitTestCore;
using NUnit.Framework;

namespace e_suite.Database.SqlServer.UnitTests;

[TestFixture]
public class ESuiteDatabaseExtensionUnitTests : TestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [TearDown]
    public void TearDown()
    {
        Environment.SetEnvironmentVariable("SQL_SERVER", null);
        Environment.SetEnvironmentVariable("SQL_DATABASE", null);
        Environment.SetEnvironmentVariable("SQL_USER", null);
        Environment.SetEnvironmentVariable("SQL_PASSWORD", null);
        Environment.SetEnvironmentVariable("SQL_ENCRYPT", null);
        Environment.SetEnvironmentVariable("SQL_TRUSTSERVERCERTIFIATE", null);
    }

    [Test]
    public void ESuiteDatabaseExtension_BuildingConnectionString_ReturnsExpectedConnectionString()
    {
        //Arrange
        const string expectedConnectionString = "Data Source=(local);Initial Catalog=esuite;Integrated Security=True;Multiple Active Result Sets=True;Encrypt=True;Trust Server Certificate=True;Application Name=e-suite";

        //Act
        var connectionString = ESuiteDatabaseExtension.BuildConnectionString(_configuration);

        //Assert
        Assert.That(connectionString, Is.EqualTo(expectedConnectionString));
    }

    [Test]
    public void ESuiteDatabaseExtension_ServerNameSetInEnvironment_ReturnsExpectedConnectionString()
    {
        //Arrange
        const string serverName = "TestServer";
        Environment.SetEnvironmentVariable("SQL_SERVER",serverName);

        var expectedConnectionString = $"Data Source={serverName};Initial Catalog=esuite;Integrated Security=True;Multiple Active Result Sets=True;Encrypt=True;Trust Server Certificate=True;Application Name=e-suite";

        //Act
        var connectionString = ESuiteDatabaseExtension.BuildConnectionString(_configuration);

        //Assert
        Assert.That(connectionString, Is.EqualTo(expectedConnectionString));
    }

    [Test]
    public void ESuiteDatabaseExtension_DatabaseNameSetInEnvironment_ReturnsExpectedConnectionString()
    {
        //Arrange
        const string databaseName = "NewDatabase";
        Environment.SetEnvironmentVariable("SQL_DATABASE", databaseName);

        var expectedConnectionString = $"Data Source=(local);Initial Catalog={databaseName};Integrated Security=True;Multiple Active Result Sets=True;Encrypt=True;Trust Server Certificate=True;Application Name=e-suite";

        //Act
        var connectionString = ESuiteDatabaseExtension.BuildConnectionString(_configuration);

        //Assert
        Assert.That(connectionString, Is.EqualTo(expectedConnectionString));
    }

    [Test]
    public void ESuiteDatabaseExtension_UserNameAndPasswordSetInEnvironment_ReturnsExpectedConnectionString()
    {
        //Arrange
        const string userName = "Testy_McTester";
        const string password = "iu435g65£&^lih45[02387y";
        Environment.SetEnvironmentVariable("SQL_USER", userName);
        Environment.SetEnvironmentVariable("SQL_PASSWORD", password);

        var expectedConnectionString = $"Data Source=(local);Initial Catalog=esuite;User ID={userName};Password={password};Multiple Active Result Sets=True;Encrypt=True;Trust Server Certificate=True;Application Name=e-suite";

        //Act
        var connectionString = ESuiteDatabaseExtension.BuildConnectionString(_configuration);

        //Assert
        Assert.That(connectionString, Is.EqualTo(expectedConnectionString));
    }

    [Test]
    public void ESuiteDatabaseExtension_SqlEncryptSetInEnvironment_ReturnsExpectedConnectionString()
    {
        //Arrange
        const bool sqlEncrypt = false;
        Environment.SetEnvironmentVariable("SQL_ENCRYPT", sqlEncrypt.ToString());

        var expectedConnectionString =
            $"Data Source=(local);Initial Catalog=esuite;Integrated Security=True;Multiple Active Result Sets=True;Encrypt={sqlEncrypt};Trust Server Certificate=True;Application Name=e-suite";

        //Act
        var connectionString = ESuiteDatabaseExtension.BuildConnectionString(_configuration);

        //Assert
        Assert.That(connectionString, Is.EqualTo(expectedConnectionString));
    }

    [Test]
    public void ESuiteDatabaseExtension_TrustServerCertificateSetInEnvironment_ReturnsExpectedConnectionString()
    {
        //Arrange
        const bool sqlTrustCertificate = false;
        Environment.SetEnvironmentVariable("SQL_TRUSTSERVERCERTIFIATE", sqlTrustCertificate.ToString());

        var expectedConnectionString = $"Data Source=(local);Initial Catalog=esuite;Integrated Security=True;Multiple Active Result Sets=True;Encrypt=True;Trust Server Certificate={sqlTrustCertificate};Application Name=e-suite";

        //Act
        var connectionString = ESuiteDatabaseExtension.BuildConnectionString(_configuration);

        //Assert
        Assert.That(connectionString, Is.EqualTo(expectedConnectionString));
    }
}