// ***********************************************************************
// 
//     AgileWebs
// 
// ***********************************************************************
using Core.Blueprint.Mongo;
using Core.Blueprint.Redis;
using Core.Blueprint.Redis.Helpers;
using Core.Thalos.Adapters;
using Core.Thalos.Domain.Contexts.Onboarding.Request;
using Core.Thalos.Provider.Contracts;
using Mapster;
using Microsoft.Extensions.Options;
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 CacheSettings cacheSettings;
        private readonly IRedisCacheProvider cacheProvider;
        public PermissionProvider(CollectionRepository repository,
            IRedisCacheProvider cacheProvider,
            IOptions cacheSettings
            )
        {
            this.repository = repository;
            this.repository.CollectionInitialization();
            this.cacheSettings = cacheSettings.Value;
            this.cacheProvider = cacheProvider;
        }
        /// 
        /// Creates a new Permission.
        /// 
        /// The Permission to be created.
        /// 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 an Permission by identifier.
        /// 
        /// The Permission identifier.
        /// A  representing
        /// the asynchronous execution of the service.0
        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 the permissions.
        /// 
        /// 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);
            return permissions;
        }
        /// 
        /// Gets all the permissions by permissions identifier list.
        /// 
        /// The list of permissions identifiers.
        /// 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 != null || !permissions.Any())
            {
                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);
            return permissionsList;
        }
        /// 
        /// Changes the status of the permission.
        /// 
        /// The permission identifier.
        /// The new status of the permission.
        /// A  representing
        /// the asynchronous execution of the service.
        public async ValueTask ChangePermissionStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken)
        {
            var entity = await repository.FindByIdAsync(id);
            entity.Status = newStatus;
            await repository.ReplaceOneAsync(entity);
            return entity;
        }
        /// 
        /// Updates a Permission by id.
        /// 
        /// The Permission to be updated.
        /// The Permission identifier.
        /// A  representing
        /// the asynchronous execution of the service.
        public async ValueTask UpdatePermission(PermissionAdapter entity, CancellationToken cancellationToken)
        {
            await repository.ReplaceOneAsync(entity);
            return entity;
        }
    }
}