using e_suite.API.Common.exceptions;
using e_suite.API.Common.models;
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 EditRoleUnitTests : RoleManagerTestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

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

        var editedRole = new EditRole
        {

        };

        //Assert
        var actualResult = Assert.ThrowsAsync<InvalidOperationException>( async () =>
        {
            //Act
            await RoleManager.EditRole(auditUserDetails, editedRole, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("GeneralIdRef cannot be null"));       
    }

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

        var editedRole = new EditRole
        {
            GeneralIdRef = new GeneralIdRef
            {
                Guid = new Guid("b6bc3c63-1d4a-4d27-b598-74dd61063a96")
            }
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await RoleManager.EditRole(auditUserDetails, editedRole, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("A role with this Id doesn't exist"));
    }

    [Test]
    public void EditRole_EditingRoleNameWillClashWithExistingRole_ThrowsExpectedException()
    {
        //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
        };
        RoleManagerRepository.Roles.Add(myTestingRole);

        var preExistingRole = new Role
        {
            Guid = new Guid("c757cbc7-a27d-40ba-afcf-c37245cbb710"),
            Id = 2,
            Name = "My Testing Role",
            DomainId = domain.Id,
            Domain = domain
        };
        RoleManagerRepository.Roles.Add(preExistingRole);


        var editedRole = new EditRole
        {
            GeneralIdRef = new GeneralIdRef
            {
                Guid = myTestingRole.Guid
            },
            Name = "My Testing Role"
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<ExistsException>(async () =>
        {
            //Act
            await RoleManager.EditRole(auditUserDetails, editedRole, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo($"Role with name '{preExistingRole.Name}' already exists"));
    }

    [Test]
    public async Task EditRole_EditingRoleName_RoleNameEdited()
    {
        //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
        };
        RoleManagerRepository.Roles.Add(myTestingRole);
        
        var editedRole = new EditRole
        {
            GeneralIdRef = new GeneralIdRef
            {
                Guid = myTestingRole.Guid
            },
            Name = "My Testing Role"
        };

        //Act
        await RoleManager.EditRole(auditUserDetails, editedRole, CancellationToken.None);

        //Assert
        Assert.That(RoleManagerRepository.RoleEdited, Is.Not.Null);
        Assert.That(RoleManagerRepository.RoleEdited!.Name, Is.EqualTo(editedRole.Name));
    }
}