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 eSuite.Core.Security;
using NUnit.Framework;

namespace e_suite.Modules.RoleManager.UnitTests;

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

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

        var accessToAdd = new AddRoleSecurityAccess
        {
            RoleId = new GeneralIdRef()
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await RoleManager.AddRoleSecurityAccess(auditUserDetails, accessToAdd, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Role Not Found"));
    }

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

        var role = new Role
        {
            Guid = new Guid("32301f97-2cec-4633-87f9-f87d3270aea5"),
            Id = 2314,
            IsAdministrator = false,
            IsSuperUser = false
        };
        RoleManagerRepository.Roles.Add(role);

        var accessToAdd = new AddRoleSecurityAccess
        {
            RoleId = new GeneralIdRef
            {
                Guid = role.Guid
            },
            SecurityAccess = new List<SecurityAccess>
            {
                SecurityAccess.AddUser, SecurityAccess.EditUser
            }
        };
        
        //Act
        await RoleManager.AddRoleSecurityAccess(auditUserDetails, accessToAdd, CancellationToken.None);

        //Assert
        Assert.That(RoleManagerRepository.RoleAccess.Count, Is.EqualTo(2));
    }
}