using e_suite.API.Common.repository;
using e_suite.Database.Audit;
using e_suite.Database.Core.Extensions;
using e_suite.Database.Core.Tables.Domain;
using e_suite.Database.Core.Tables.UserManager;
using e_suite.UnitTestCore;
using eSuite.Core.Miscellaneous;
using MockQueryable;

namespace e_suite.Modules.RoleManager.UnitTests.Helpers;

public class FakeRoleManagerRepository : FakeRepository, IRoleManagerRepository
{
    public List<Role> Roles { get; } = [];
    public List<UserRole> RoleUsers { get; } = [];
    public Role? RoleEdited { get; private set; }

    public UserRole? UserRole { get; private set; }
    public UserRole? DeleteUserRoleItem { get; private set; }

    public List<RoleAccess> RoleAccess { get; } = [];

    public List<UserAccess> UserAccess { get; } = [];

    public IQueryable<Role> GetRolesList()
    {
        return Roles.BuildMock();
    }

    public Role? GetRoleById(IGeneralIdRef generalIdRef)
    {
        throw new NotImplementedException();
    }

    public Task<Role?> GetRoleByIdAsync(IGeneralIdRef generalIdRef, CancellationToken cancellationToken)
    {
        return Task.FromResult(GetRolesList().FindByGeneralIdRef(generalIdRef));
    }

    public Task<Role?> GetRoleByName(Domain domain, string sequenceName, CancellationToken cancellationToken)
    {
        return Task.FromResult(GetRolesList().SingleOrDefault(x => x.Name == sequenceName && x.DomainId == domain.Id));
    }

    public Task EditRole(AuditUserDetails auditUserDetails, Role existingSequence, CancellationToken cancellationToken)
    {
        RoleEdited = existingSequence;
        return Task.CompletedTask;
    }

    public Task AddRole(AuditUserDetails auditUserDetails, Role existingSequence, CancellationToken cancellationToken)
    {
        Roles.Add(existingSequence);
        return Task.CompletedTask;
    }

    public IQueryable<UserRole> GetUserRoles()
    {
        return RoleUsers.BuildMock();
    }

    public Task AddUserRole(
        AuditUserDetails auditUserDetails,
        Role existingRole,
        User existingUser,
        CancellationToken cancellationToken
    )
    {
        UserRole = new UserRole
        {
            Role = existingRole,
            User = existingUser
        };
        return Task.CompletedTask;
    }

    public Task DeleteUserRole(
        AuditUserDetails auditUserDetails,
        Role existingRole,
        User existingUser,
        CancellationToken cancellationToken
    )
    {
        DeleteUserRoleItem = new UserRole
        {
            Role = existingRole,
            User = existingUser
        };
        return Task.CompletedTask;
    }

    public IQueryable<RoleAccess> GetAccessForRole()
    {
        return RoleAccess.BuildMock();
    }

    public Task AddSecurityAccess(AuditUserDetails auditUserDetails, List<RoleAccess> rollAccessToAdd, CancellationToken cancellationToken)
    {
        RoleAccess.AddRange(rollAccessToAdd);
        return Task.CompletedTask;
    }

    public Task DeleteSecurityAccess(AuditUserDetails auditUserDetails, List<RoleAccess> rollAccessToDelete, CancellationToken cancellationToken)
    {
        foreach (var item in rollAccessToDelete)
            RoleAccess.Remove(item);
        return Task.CompletedTask;
    }

    public IQueryable<UserAccess> GetUserAccess()
    {
        return UserAccess.BuildMock();
    }
}