// ***********************************************************************
// 
//     AgileWebs
// 
// ***********************************************************************
using Core.Thalos.Adapters;
using Core.Thalos.Adapters.Common.Enums;
using Core.Blueprint.Mongo;
using Core.Blueprint.Redis;
using Core.Blueprint.Redis.Helpers;
using Mapster;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Core.Thalos.Provider.Contracts;
using MongoDB.Bson;
using System.Text.RegularExpressions;
using MongoDB.Bson.Serialization;
using Core.Thalos.Domain.Contexts.Onboarding.Request;
using Microsoft.Graph;
using System.ComponentModel.Design;
namespace Core.Thalos.Provider.Providers.Onboarding
{
    /// 
    /// Handles all services and business rules related to .
    /// 
    public class RoleProvider : IRoleProvider
    {
        private readonly CollectionRepository repository;
        private readonly CacheSettings cacheSettings;
        private readonly IRedisCacheProvider cacheProvider;
        public RoleProvider(CollectionRepository repository,
        IRedisCacheProvider cacheProvider, IOptions cacheSettings)
        {
            this.repository = repository;
            this.repository.CollectionInitialization();
            this.cacheSettings = cacheSettings.Value;
            this.cacheProvider = cacheProvider;
        }
        /// 
        /// Creates a new Role.
        /// 
        /// The Role to be created.
        /// A  representing
        /// the asynchronous execution of the service.
        public async ValueTask CreateRole(RoleRequest newRole, CancellationToken cancellationToken)
        {
            var roleCollection = newRole.Adapt();
            await repository.InsertOneAsync(roleCollection);
            return roleCollection;
        }
        /// 
        /// Gets an Role by identifier.
        /// 
        /// The Role identifier.
        /// A  representing
        /// the asynchronous execution of the service.
        public async ValueTask GetRoleById(string _id, CancellationToken cancellationToken)
        {
            var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetRoleById", _id);
            var cachedData = await cacheProvider.GetAsync(cacheKey);
            if (cachedData is not null) { return cachedData; }
            var role = await repository.FindByIdAsync(_id);
            await cacheProvider.SetAsync(cacheKey, role);
            return role;
        }
        /// 
        /// Gets all the roles.
        /// 
        /// A  representing
        /// the asynchronous execution of the service.
        public async ValueTask> GetAllRoles(CancellationToken cancellationToken)
        {
            var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllRoles");
            var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? [];
            if (cachedData.Any()) return cachedData;
            var roles = await repository.AsQueryable();
            await cacheProvider.SetAsync(cacheKey, roles);
            return roles;
        }
        /// 
        /// Changes the status of the role.
        /// 
        /// The role identifier.
        /// The new status of the role.
        /// A  representing
        /// the asynchronous execution of the service.
        public async ValueTask ChangeRoleStatus(string id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken)
        {
            var entity = await repository.FindByIdAsync(id);
            entity.Status = newStatus;
            await repository.ReplaceOneAsync(entity);
            return entity;
        }
        /// 
        /// Updates a Role by id.
        /// 
        /// The Role to be updated.
        /// The Role identifier.
        /// A  representing
        /// the asynchronous execution of the service.
        public async ValueTask UpdateRole(RoleAdapter entity, CancellationToken cancellationToken)
        {
            await repository.ReplaceOneAsync(entity);
            return entity;
        }
        /// 
        /// Adds an application to the role's list of applications.
        /// 
        /// The identifier of the role to which the application will be added.
        /// The application enum value to add.
        /// A  representing the asynchronous operation, with the updated role object.
        public async ValueTask AddApplicationToRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken)
        {
            var role = await repository.FindOneAsync(
                u => u.Id == roleId &&
                u.Status == Core.Blueprint.Mongo.StatusEnum.Active);
            var updatedApplications = role.Applications.Append(application).Distinct().ToArray();
            role.Applications = updatedApplications;
            await repository.ReplaceOneAsync(role);
            return role;
        }
        /// 
        /// Removes an application from the role's list of applications.
        /// 
        /// The identifier of the role from which the application will be removed.
        /// The application enum value to remove.
        /// A  representing the asynchronous operation, with the updated role object.
        public async ValueTask RemoveApplicationFromRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken)
        {
            var role = await repository.FindOneAsync(
                u => u.Id == roleId &&
                u.Status == Core.Blueprint.Mongo.StatusEnum.Active);
            var updatedApplications = role.Applications
                    ?.Where(c => c != application)
                    .ToArray();
            role.Applications = updatedApplications;
            await repository.ReplaceOneAsync(role);
            return role;
        }
    }
}