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

namespace e_suite.Modules.RoleManager.UnitTests;

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

    [Test]
    public void DeleteRole_WhenRoleDoesNotExist_ThrowNotFoundException()
    {
        //Arrange
        var auditUserDetails = new AuditUserDetails
        {
            UserId = 2562,
            UserDisplayName = "Testy McTester",
        };

        var generalIdRef = new GeneralIdRef
        {
            Guid = new Guid("960474cb-0443-4a9f-a0c5-1f601d1d851b")
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
            {
                //Act
                await RoleManager.DeleteRole(auditUserDetails, generalIdRef, CancellationToken.None);
            }
        );

        Assert.That(actualResult!.Message, Is.EqualTo("A role with this Id does not exist"));
    }

    [Test]
    public async Task DeleteRole_WhenExists_RoleIsSoftDeleted()
    {
        //Arrange
        var auditUserDetails = new AuditUserDetails
        {
            UserId = 2562,
            UserDisplayName = "Testy McTester",
        };

        var domain = new Domain
        {
            Guid = new Guid("15600a5f-6b7e-4a57-bf89-00423cd25d3e"),
            Id = 1234,
            Name = "TestDomain"
        };

        var myTestingRole = new Role
        {
            Guid = new Guid("1d8331a8-51b2-4d94-b5f0-12b19beb39c8"),
            Id = 1,
            Name = "Role To Edit",
            DomainId = domain.Id,
            Domain = domain,
            Deleted = false
        };
        RoleManagerRepository.Roles.Add(myTestingRole);

        var generalIdRef = new GeneralIdRef()
        {
            Guid = myTestingRole.Guid
        };

        //Act
        await RoleManager.DeleteRole(auditUserDetails, generalIdRef, CancellationToken.None);

        //Assert
        Assert.That(RoleManagerRepository.RoleEdited, Is.Not.Null);
        Assert.That(RoleManagerRepository.RoleEdited!.Deleted, Is.True);
    }
}