// ***********************************************************************
// 
//     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;
using StatusEnum = Core.Blueprint.Mongo.StatusEnum;
namespace Core.Thalos.Provider.Providers.Onboarding
{
    /// 
    /// Handles all services and business rules related to .
    /// 
    public class ModuleProvider : IModuleProvider
    {
        private readonly CollectionRepository repository;
        private readonly ICacheSettings cacheSettings;
        private readonly IRedisCacheProvider cacheProvider;
        public ModuleProvider(
            CollectionRepository repository,
            IRedisCacheProvider cacheProvider,
            ICacheSettings cacheSettings)
        {
            this.repository = repository;
            this.repository.CollectionInitialization();
            this.cacheProvider = cacheProvider;
            this.cacheSettings = cacheSettings;
        }
        /// 
        /// Creates a new Module.
        /// 
        /// The Module to be created.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask CreateModule(ModuleRequest newModule, CancellationToken cancellationToken)
        {
            var moduleCollection = newModule.Adapt();
            await repository.InsertOneAsync(moduleCollection);
            return moduleCollection;
        }
        /// 
        /// Gets a Module by identifier.
        /// 
        /// The Module Mongo identifier.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask GetModuleById(string _id, CancellationToken cancellationToken)
        {
            var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModuleById", _id);
            var cachedData = await cacheProvider.GetAsync(cacheKey);
            if (cachedData is not null) return cachedData;
            var module = await repository.FindByIdAsync(_id);
            await cacheProvider.SetAsync(cacheKey, module, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
            return module;
        }
        /// 
        /// Gets all the Modules.
        /// 
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask> GetAllModules(CancellationToken cancellationToken)
        {
            var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModules");
            var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? [];
            if (cachedData.Any()) return cachedData;
            var modules = await repository.AsQueryable();
            await cacheProvider.SetAsync(cacheKey, modules);
            return modules;
        }
        /// 
        /// Gets all the Modules by a list of identifiers.
        /// 
        /// The list of Module identifiers.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask> GetAllModulesByList(string[] modules, CancellationToken cancellationToken)
        {
            var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllModulesByList", modules);
            var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? [];
            if (cachedData.Any()) return cachedData;
            var builder = Builders.Filter;
            var filters = new List>();
            if (modules is { Length: > 0 })
            {
                filters.Add(builder.In(x => x._Id, modules));
            }
            var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty;
            var modulesList = await repository.FilterByMongoFilterAsync(finalFilter);
            await cacheProvider.SetAsync(cacheKey, modulesList, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
            return modulesList;
        }
        /// 
        /// Changes the status of the Module.
        /// 
        /// The Module Mongo identifier.
        /// The new status of the Module.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask ChangeModuleStatus(string _id, StatusEnum newStatus, CancellationToken cancellationToken)
        {
            var entity = await repository.FindByIdAsync(_id);
            entity.Status = newStatus;
            await repository.ReplaceOneAsync(entity);
            return entity;
        }
        /// 
        /// Updates a Module.
        /// 
        /// The Module to be updated.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous execution of the service.
        /// 
        public async ValueTask UpdateModule(ModuleAdapter entity, CancellationToken cancellationToken)
        {
            await repository.ReplaceOneAsync(entity);
            return entity;
        }
        /// 
        /// Deletes a Module by identifier.
        /// 
        /// The Module Mongo identifier.
        /// A token to cancel the asynchronous operation.
        /// 
        /// A  representing the asynchronous deletion result.
        /// The deleted Module entity if found; otherwise, null.
        /// 
        public async ValueTask DeleteModule(string _id, CancellationToken cancellationToken)
        {
            var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id);
            return entity;
        }
    }
}