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 AddRoleUserUnitTests : RoleManagerTestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

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

        var userRole = new UserRoleIds
        {
            UserId = new GeneralIdRef(),
            RoleId = new GeneralIdRef()
        };

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

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

        var role = new Role
        {
            Id = 20,
            Guid = new Guid("225cd31e-f448-4d30-b2b7-b64ad4a2066a"),
            Name = "Test Role"
        };
        RoleManagerRepository.Roles.Add(role);

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

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

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

        var role = new Role
        {
            Id = 20,
            Guid = new Guid("225cd31e-f448-4d30-b2b7-b64ad4a2066a"),
            Name = "Test Role"
        };
        RoleManagerRepository.Roles.Add(role);

        var user = new User
        {
            Guid = new Guid("d51133e6-886b-4021-879b-cb962f375c55"),
            Id = 713,
            Email = "User@SomePlace.Com"
        };
        UserManagerRepository.Users.Add(user);

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

        //Act
        await RoleManager.AddRoleUser(auditDetails, userRole, CancellationToken.None);

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