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

namespace e_suite.Modules.RoleManager.UnitTests;

[TestFixture]
public class GetRoleUsersUnitTests : RoleManagerTestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }
    
    [Test]
    public async Task GetRoleUsers_WhenRoleIdNull_ReturnsAllUsersForAllRoles()
    {
        //Arrange
        var paging = new Paging();

        var role1 = new Role
        {
            Guid = new Guid("c68cf187-02fe-4937-9d63-2a23586c1c1c"),
            Id = 1,
            Name = "Role 1",
            IsAdministrator = false,
            IsSuperUser = false
        };
        RoleManagerRepository.Roles.Add(role1);

        var user1 = new User
        {
            Guid = new Guid("484382a7-4fe8-4517-8851-9f49acfb7529"),
            Id = 100,
            FirstName = "Testy",
            LastName = "McTester",
            Email = "testy@test.com"
        };

        var roleUser1 = new UserRole
        {
            Role = role1,
            RoleId = role1.Id,
            User = user1,
            UserId = user1.Id
        };
        RoleManagerRepository.RoleUsers.Add(roleUser1);
        
        var role2 = new Role
        {
            Guid = new Guid("adcbf8c1-96fa-4d3e-acea-573d600a3d33"),
            Id = 1,
            Name = "Role 2",
            IsAdministrator = false,
            IsSuperUser = false
        };
        RoleManagerRepository.Roles.Add(role2);

        var user2 = new User
        {
            Guid = new Guid("c43efd35-837f-41e1-bc16-0f947e2d50ab"),
            Id = 110,
            FirstName = "Tested",
            LastName = "McTestins",
            Email = "tested@test.com"
        };

        var roleUser2 = new UserRole
        {
            Role = role2,
            RoleId = role2.Id,
            User = user2,
            UserId = user2.Id
        };
        RoleManagerRepository.RoleUsers.Add(roleUser2);

        var roleGeneralIdRef = new GeneralIdRef();

        //Act
        var result = await RoleManager.GetRoleUsers(paging, roleGeneralIdRef, CancellationToken.None);
        
        //Assert
        Assert.That(result, Is.Not.Null);
        Assert.That(result.Count, Is.EqualTo(2));
        Assert.That(result.Data.Count(), Is.EqualTo(result.Count));
    }

    [Test]
    public async Task GetRoleUsers_WhenRoleIdIsRole1_ReturnsAllUsersForRole1()
    {
        //Arrange
        var paging = new Paging();

        var role1 = new Role
        {
            Guid = new Guid("61759f40-d6c7-4cb3-9766-2339468c646d"),
            Id = 1,
            Name = "Role 1",
            IsAdministrator = false,
            IsSuperUser = false
        };
        RoleManagerRepository.Roles.Add(role1);

        var user1 = new User
        {
            Guid = new Guid("09799a14-4c3c-49ff-a043-67a97e2d09e0"),
            Id = 100,
            FirstName = "Testy",
            LastName = "McTester",
            Email = "testy@test.com"
        };

        var roleUser1 = new UserRole
        {
            Role = role1,
            RoleId = role1.Id,
            User = user1,
            UserId = user1.Id
        };
        RoleManagerRepository.RoleUsers.Add(roleUser1);

        var role2 = new Role
        {
            Guid = new Guid("7ddfa5aa-a279-4133-85f8-2993d085caec"),
            Id = 1,
            Name = "Role 2",
            IsAdministrator = false,
            IsSuperUser = false
        };
        RoleManagerRepository.Roles.Add(role2);

        var user2 = new User
        {
            Guid = new Guid("c68cf187-02fe-4937-9d63-2a23586c1c1c"),
            Id = 110,
            FirstName = "Tested",
            LastName = "McTestins",
            Email = "tested@test.com"
        };

        var roleUser2 = new UserRole
        {
            Role = role2,
            RoleId = role2.Id,
            User = user2,
            UserId = user2.Id
        };
        RoleManagerRepository.RoleUsers.Add(roleUser2);

        var roleGeneralIdRef = new GeneralIdRef
        {
            Guid = role1.Guid
        };

        //Act
        var result = await RoleManager.GetRoleUsers(paging, roleGeneralIdRef, CancellationToken.None);

        //Assert
        Assert.That(result, Is.Not.Null);
        Assert.That(result.Count, Is.EqualTo(1));
        Assert.That(result.Data.Count(), Is.EqualTo(result.Count));

        var returnedUser = result.Data.First();
        Assert.That(returnedUser.Id, Is.EqualTo(user1.Id));
        Assert.That(returnedUser.Guid, Is.EqualTo(user1.Guid));
        Assert.That(returnedUser.DisplayName, Is.EqualTo(user1.DisplayName));
    }
}