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.Database.Core.Tables.UserManager;
using e_suite.Modules.RoleManager.UnitTests.Helpers;
using eSuite.Core.Miscellaneous;
using NUnit.Framework;

namespace e_suite.Modules.RoleManager.UnitTests;

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

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

        var userRoleIds = new UserRoleIds
        {
            UserId = new GeneralIdRef(),
            RoleId = new GeneralIdRef
            {
                Guid = new Guid("3511954b-a9aa-4361-ace7-0a9f5b9f0e5e")
            }
        };

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

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

        var role = new Role
        {
            Guid = new Guid("491985ba-19e1-4022-a093-5e0ba93878da")
        };
        RoleManagerRepository.Roles.Add(role);

        var userRoleIds = new UserRoleIds
        {
            UserId = new GeneralIdRef(),
            RoleId = new GeneralIdRef
            {
                Guid = role.Guid
            }
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await RoleManager.DeleteRoleUser(auditDetails, userRoleIds, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("User not Found"));
    }

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

        var role = new Role
        {
            Guid = new Guid("491985ba-19e1-4022-a093-5e0ba93878da")
        };
        RoleManagerRepository.Roles.Add(role);

        var user = new User
        {
            Guid = new Guid("5eb2813f-27c5-4e0a-9b85-515de0d6ab37")
        };
        UserManagerRepository.Users.Add(user);

        var userRoleIds = new UserRoleIds
        {
            UserId = new GeneralIdRef
            {
                Guid = user.Guid
            },
            RoleId = new GeneralIdRef
            {
                Guid = role.Guid
            }
        };

        //Act
        await RoleManager.DeleteRoleUser(auditDetails, userRoleIds, CancellationToken.None);

        //Assert
        Assert.That(RoleManagerRepository.DeleteUserRoleItem, Is.Not.Null);
        Assert.That(RoleManagerRepository.DeleteUserRoleItem!.User, Is.EqualTo(user));
        Assert.That(RoleManagerRepository.DeleteUserRoleItem!.Role, Is.EqualTo(role));
    }
}