// ***********************************************************************
// 
//     AgileWebs
// 
// ***********************************************************************
using Core.Blueprint.Mongo;
using Core.Blueprint.Redis;
using Core.Blueprint.Redis.Helpers;
using Core.Thalos.BuildingBlocks;
using Core.Thalos.Domain.Contexts.Onboarding.Request;
using Core.Thalos.Provider.Contracts;
using Mapster;
using MongoDB.Driver;
namespace Core.Thalos.Provider.Providers.Onboarding
{
    /// 
    /// Handles all services and business rules related to .
    /// 
    public class RoleProvider : IRoleProvider
    {
        private readonly CollectionRepository repository;
        private readonly ICacheSettings cacheSettings;
        private readonly IRedisCacheProvider cacheProvider;
        public RoleProvider(
            CollectionRepository repository,
            IRedisCacheProvider cacheProvider,
            ICacheSettings cacheSettings)
        {
            this.repository = repository;
            this.repository.CollectionInitialization();
            this.cacheProvider = cacheProvider;
            this.cacheSettings = cacheSettings;
        }
        /// 
        /// Creates a new Role.
        /// 
        /// The Role to be created.
        /// A token to cancel the asynchronous operation.
        /// 
        /// 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 a Role by its identifier.
        /// 
        /// The Role Mongo identifier.
        /// A token to cancel the asynchronous operation.
        /// 
        /// 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, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
            return role;
        }
        /// 
        /// Gets all Roles.
        /// 
        /// A token to cancel the asynchronous operation.
        /// 
        /// 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, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
            return roles;
        }
        /// 
        /// Changes the status of a Role.
        /// 
        /// The Role Mongo identifier.
        /// The new status of the Role.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask ChangeRoleStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken)
        {
            var entity = await repository.FindByIdAsync(_id);
            if (entity is not null)
            {
                entity.Status = newStatus;
                return repository.ReplaceOneAsync(entity).Result;
            }
            else return null;
        }
        /// 
        /// Updates a Role.
        /// 
        /// The Role to be updated.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask UpdateRole(RoleAdapter entity, CancellationToken cancellationToken)
        {
            var updatedEntity = await repository.ReplaceOneAsync(entity);
            return updatedEntity;
        }
        /// 
        /// 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 token to cancel the asynchronous operation.
        /// 
        /// 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 == 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 token to cancel the asynchronous operation.
        /// 
        /// 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 == Blueprint.Mongo.StatusEnum.Active);
            var updatedApplications = role.Applications?
                .Where(c => c != application)
                .ToArray();
            role.Applications = updatedApplications;
            await repository.ReplaceOneAsync(role);
            return role;
        }
        /// 
        /// Deletes a Role by identifier.
        /// 
        /// The Role Mongo identifier.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous deletion result.
        /// The deleted Role entity if found; otherwise, null.
        /// 
        public async ValueTask DeleteRole(string _id, CancellationToken cancellationToken)
        {
            var entity = await repository.DeleteOneAsync(doc => doc._Id == _id);
            return entity;
        }
    }
}