// ***********************************************************************
// 
//     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 PermissionProvider : IPermissionProvider
    {
        private readonly CollectionRepository repository;
        private readonly ICacheSettings cacheSettings;
        private readonly IRedisCacheProvider cacheProvider;
        public PermissionProvider(
            CollectionRepository repository,
            IRedisCacheProvider cacheProvider,
            ICacheSettings cacheSettings)
        {
            this.repository = repository;
            this.repository.CollectionInitialization();
            this.cacheProvider = cacheProvider;
            this.cacheSettings = cacheSettings;
        }
        /// 
        /// Creates a new Permission.
        /// 
        /// The Permission to be created.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask CreatePermission(PermissionRequest newPermission, CancellationToken cancellationToken)
        {
            var permissionCollection = newPermission.Adapt();
            await repository.InsertOneAsync(permissionCollection);
            return permissionCollection;
        }
        /// 
        /// Gets a Permission by identifier.
        /// 
        /// The Permission Mongo identifier.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask GetPermissionById(string _id, CancellationToken cancellationToken)
        {
            var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetPermissionById", _id);
            var cachedData = await cacheProvider.GetAsync(cacheKey);
            if (cachedData is not null) return cachedData;
            var permission = await repository.FindByIdAsync(_id);
            await cacheProvider.SetAsync(cacheKey, permission);
            return permission;
        }
        /// 
        /// Gets all Permissions.
        /// 
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask> GetAllPermissions(CancellationToken cancellationToken)
        {
            var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissions");
            var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? [];
            //if (cachedData.Any()) return cachedData;
            var permissions = await repository.AsQueryable();
            await cacheProvider.SetAsync(cacheKey, permissions, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
            return permissions;
        }
        /// 
        /// Gets all Permissions by a list of identifiers.
        /// 
        /// The list of Permission identifiers.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask> GetAllPermissionsByList(string[] permissions, CancellationToken cancellationToken)
        {
            var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissionsByList", permissions);
            var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? [];
            if (cachedData.Any()) return cachedData;
            var builder = Builders.Filter;
            var filters = new List>();
            if (permissions is { Length: > 0 })
            {
                filters.Add(builder.In(x => x._Id, permissions));
            }
            var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty;
            var permissionsList = await repository.FilterByMongoFilterAsync(finalFilter);
            await cacheProvider.SetAsync(cacheKey, permissionsList, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
            return permissionsList;
        }
        /// 
        /// Changes the status of a Permission.
        /// 
        /// The Permission Mongo identifier.
        /// The new status of the Permission.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask ChangePermissionStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken)
        {
            var entity = await repository.FindByIdAsync(_id);
            entity.Status = newStatus;
            await repository.ReplaceOneAsync(entity);
            return entity;
        }
        /// 
        /// Updates a Permission.
        /// 
        /// The Permission to be updated.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask UpdatePermission(PermissionAdapter entity, CancellationToken cancellationToken)
        {
            await repository.ReplaceOneAsync(entity);
            return entity;
        }
        /// 
        /// Deletes a Permission by identifier.
        /// 
        /// The Permission Mongo identifier.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous deletion result.
        /// The deleted Permission entity if found; otherwise, null.
        /// 
        public async ValueTask DeletePermission(string _id, CancellationToken cancellationToken)
        {
            var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id);
            return entity;
        }
    }
}