using e_suite.Database.Core.Tables.Domain;
using e_suite.Database.Core.Tables.UserManager;
using e_suite.Modules.RoleManager.UnitTests.Helpers;
using eSuite.Core.Security;
using NUnit.Framework;

namespace e_suite.Modules.RoleManager.UnitTests;

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

    [Test]
    public async Task HasAnyAccess_WhenNoAccess_ReturnsFalse()
    {
        //Arrange
        UserManagerRepository.Users.Add(new Database.Core.Tables.UserManager.User
        {
            Id = 123,
            Guid = new Guid("9698F096-1F3F-4698-8DDC-38E9CDB962CE"),
            DomainId = 1
        });
        //Act
        var result = await RoleManager.HasAnyAccess(123, SecurityAccess.AddUser);

        //Assert
        Assert.That(result, Is.False);
    }



    [Test]
    public async Task HasAnyAccess_WhenUserHasExplicitAccess_ReturnsTrue()
    {
        //Arrange
        RoleManagerRepository.UserAccess.Add( new UserAccess
        {
            UserId = 123,
            AccessKey = (int)SecurityAccess.AddUser,
            DomainId = 1
        });

        UserManagerRepository.Users.Add(new User
        {
            Id = 123,
            Guid = new Guid("9698F096-1F3F-4698-8DDC-38E9CDB962CE"),
            DomainId = 1
        });

        //Act
        var result = await RoleManager.HasAnyAccess(123, SecurityAccess.AddUser);

        //Assert
        Assert.That(result, Is.True);
    }

    [Test]
    public async Task HasAnyAccess_WhenUserIsNotActive_ReturnsFalse()
    {
        //Arrange
        RoleManagerRepository.UserAccess.Add(new UserAccess
        {
            UserId = 123,
            AccessKey = (int)SecurityAccess.AddUser,
            DomainId = 1,
        });

        UserManagerRepository.Users.Add(new User
        {
            Id = 123,
            Guid = new Guid("9698F096-1F3F-4698-8DDC-38E9CDB962CE"),
            DomainId = 1,
            Active = false
        });

        //Act
        var result = await RoleManager.HasAnyAccess(123, SecurityAccess.AddUser);

        //Assert
        Assert.That(result, Is.False);
    }

    [Test]
    public async Task HasAnyAccess_WhenAccessKeyIsEveryoneAndUserActive_ReturnsTrue()
    {
        //Arrange
        UserManagerRepository.Users.Add(new User
        {
            Id = 123,
            Guid = new Guid("9698F096-1F3F-4698-8DDC-38E9CDB962CE"),
            DomainId = 1
        });

        //Act
        var result = await RoleManager.HasAnyAccess(123, SecurityAccess.Everyone);

        //Assert
        Assert.That(result, Is.True);
    }

    [Test]
    public async Task HasAnyAccess_WhenUserHasOtherAccess_ReturnsFalse()
    {
        //Arrange
        RoleManagerRepository.UserAccess.Add(new UserAccess
        {
            UserId = 123,
            AccessKey = (int)SecurityAccess.AddUser
        });

        UserManagerRepository.Users.Add(new Database.Core.Tables.UserManager.User
        {
            Id = 123,
            Guid = new Guid("9698F096-1F3F-4698-8DDC-38E9CDB962CE"),
            DomainId = 1
        });
        //Act
        var result = await RoleManager.HasAnyAccess(123, SecurityAccess.EditUser);

        //Assert
        Assert.That(result, Is.False);
    }

    [Test]
    public async Task HasAnyAccess_WhenUserIsAdministrator_ReturnsTrue()
    {
        //Arrange
        var adminRole = new Role
        {
            Guid = new Guid("893de157-fc23-4505-9f1b-9a225fc2bb90"),
            Id = 456,
            Name = "Admin",
            IsAdministrator = true,
            IsSuperUser = false
        };

        UserManagerRepository.Users.Add(new Database.Core.Tables.UserManager.User
        {
            Id = 123,
            Guid = new Guid("9698F096-1F3F-4698-8DDC-38E9CDB962CE"),
            DomainId = 1
        });

        RoleManagerRepository.RoleUsers.Add( new UserRole
        {
            UserId = 123,
            Role = adminRole
        });

        //Act
        var result = await RoleManager.HasAnyAccess(123, SecurityAccess.EditUser);

        //Assert
        Assert.That(result, Is.True);
    }

    [Test]
    public async Task HasAnyAccess_WhenUserIsSuperUser_ReturnsTrue()
    {
        //Arrange
        var adminRole = new Role
        {
            Guid = new Guid("893de157-fc23-4505-9f1b-9a225fc2bb90"),
            Id = 456,
            Name = "Admin",
            IsAdministrator = false,
            IsSuperUser = true
        };

        UserManagerRepository.Users.Add(new Database.Core.Tables.UserManager.User
        {
            Id = 123,
            Guid = new Guid("9698F096-1F3F-4698-8DDC-38E9CDB962CE"),
            DomainId = 1
        });

        RoleManagerRepository.RoleUsers.Add(new UserRole
        {
            UserId = 123,
            Role = adminRole
        });

        //Act
        var result = await RoleManager.HasAnyAccess(123, SecurityAccess.EditUser);

        //Assert
        Assert.That(result, Is.True);
    }

    [Test]
    public async Task HasAnyAccess_WhenUserNormalUser_ReturnsFalse()
    {
        //Arrange
        var adminRole = new Role
        {
            Guid = new Guid("893de157-fc23-4505-9f1b-9a225fc2bb90"),
            Id = 456,
            Name = "Admin",
            IsAdministrator = false,
            IsSuperUser = false
        };

        UserManagerRepository.Users.Add(new Database.Core.Tables.UserManager.User
        {
            Id = 123,
            Guid = new Guid("9698F096-1F3F-4698-8DDC-38E9CDB962CE"),
            DomainId = 1
        });

        RoleManagerRepository.RoleUsers.Add(new UserRole
        {
            UserId = 123,
            Role = adminRole
        });

        //Act
        var result = await RoleManager.HasAnyAccess(123, SecurityAccess.EditUser);

        //Assert
        Assert.That(result, Is.False);
    }
}