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

    [Test]
    public void GetRole_RoleNotExisting_ThrowsNotFoundException()
    {
        //Arrange
        var generalIdRef = new GeneralIdRef
        {
            Guid = new Guid("8d5c8114-ad04-492c-a5d3-271584763126")
        };


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

        Assert.That(actualResult!.Message, Is.EqualTo("Role not found"));
    }

    [Test]
    public void GetRole_RoleIsSoftDeleted_ThrowsNotFoundException()
    {
        //Arrange
        var role = new Role
        { 
            Guid = new Guid("fa863c47-d043-4e82-9daf-22ed2997e54b"),
            Name = "Deleted Role",
            Deleted = true
        };
        RoleManagerRepository.Roles.Add(role);

        var generalIdRef = new GeneralIdRef
        {
            Guid = role.Guid
        };

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

        Assert.That(actualResult!.Message, Is.EqualTo("Role not found"));
    }

    [Test]
    public async Task GetRole_WhenDomainFound_ReturnsDomain()
    {
        //Arrange
        var domain = new Domain
        {
            Id = 234,
            Guid = new Guid("cab894c2-9a1d-4e58-a2fd-014d0dd826a4"),
            Name = "Test domain"
        };

        var role = new Role
        {
            Guid = new Guid("d9a2c28e-0877-44ef-852e-44243b49b8ac"),
            Name = "Test role",
            Domain = domain,
            DomainId = domain.Id,
            Deleted = false
        };
        RoleManagerRepository.Roles.Add(role);

        var generalIdRef = new GeneralIdRef
        {
            Guid = role.Guid
        };

        //Act
        var result = await RoleManager.GetRole(generalIdRef, CancellationToken.None);

        //Assert
        Assert.That(result, Is.Not.Null);
        Assert.That(result.Guid, Is.EqualTo(role.Guid));
    }
}