using e_suite.API.Common.exceptions;
using e_suite.Database.Audit;
using e_suite.Database.Core.Tables.Domain;
using e_suite.Modules.DomainManager.UnitTests.Helpers;
using eSuite.Core.Miscellaneous;
using NUnit.Framework;

namespace e_suite.Modules.DomainManager.UnitTests;

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

    [Test]
    public void DeleteDomainAsync_DomainNotFound_ThrowsNotFoundException()
    {
        //Arrange
        var auditUserDetails = new AuditUserDetails
        {
            UserId = 100,
            UserDisplayName = "Testy McTester",
            Comment = string.Empty
        };

        var deleteDomain = new GeneralIdRef
        {
            Guid = new Guid("55359f68-47f3-4b79-8bb2-e7a5e2f92cae")
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await DomainManager.DeleteDomainAsync(auditUserDetails, deleteDomain, CancellationToken.None);
        });

        Assert.That(actualResult!.Message, Is.EqualTo("Domain not found"));
    }

    [Test]
    public async Task DeleteDomainAsync_DomainNotDeleted_DomainGetsDeleted()
    {
        //Arrange
        var auditUserDetails = new AuditUserDetails
        {
            UserId = 100,
            UserDisplayName = "Testy McTester",
            Comment = string.Empty
        };

        var domain = new Domain
        {
            Guid = new Guid("8b7a3e33-9ab8-4c10-b0d5-8dbcade59569"),
            Id = 777,
            Name = "Delete me",
            Deleted = false
        };

        DomainManagerRepository.Domains.Add(domain);

        var deleteDomain = new GeneralIdRef
        {
            Guid = domain.Guid
        };

        //Act
        await DomainManager.DeleteDomainAsync(auditUserDetails, deleteDomain, CancellationToken.None);

        //Assert
        Assert.That(domain.Deleted, Is.True);
    }

    [Test]
    public void DeleteDomainAsync_AttemptToDeleteSunDomain_ThrowsException()
    {
        //Arrange
        var auditUserDetails = new AuditUserDetails
        {
            UserId = 100,
            UserDisplayName = "Testy McTester",
            Comment = string.Empty
        };

        var domain = new Domain
        {
            Guid = new Guid("3470d32f-8796-409d-8408-8121fe13b154"),
            Id = 1,
            Name = "Sun Branding Solutions",
            Deleted = false
        };

        DomainManagerRepository.Domains.Add(domain);

        var deleteDomain = new GeneralIdRef
        {
            Guid = domain.Guid
        };


        //Assert
        var actualResult = Assert.ThrowsAsync<InvalidOperationException>(async () =>
        {
            //Act
            await DomainManager.DeleteDomainAsync(auditUserDetails, deleteDomain, CancellationToken.None);
        });

        Assert.That(actualResult!.Message, Is.EqualTo("Not allowed to delete the Sun Branding Solutions domain"));
    }
}