using adas_core.Domain.Models.GroupedObservations;
using MongoDB.Bson;
namespace adas_core.Application.Services.Interfaces
{
public interface ICacheService
{
//Métodos básicos
///
/// Stores the specified value associated with the given key.
///
/// The identifier used to associate the value.
/// The value to store for the specified key.
void SetValue(string key, string value);
///
/// Retrieves the string value associated with the specified key.
///
/// The key used to look up the value.
/// The value associated with the key, or null if the key is not found.
string? GetValue(string key);
///
/// Asynchronously retrieves an object associated with the specified , returning null if no entry is found.
/// When is true, the expiration of the retrieved entry is extended upon access.
///
/// The unique identifier of the object to retrieve.
/// Indicates whether the expiration of the entry should be extended when it is successfully retrieved. Defaults to true.
/// A task that represents the asynchronous retrieval operation. The task result contains the object of type associated with the key, or null if no matching entry exists.
Task GetObjectAsync(string key, bool updateExpiration = true);
///
/// Asynchronously stores an object of type using the specified key, optionally updating its expiration time.
/// When is true, the entry's expiration is refreshed; otherwise the existing expiration is preserved.
///
/// The identifier under which the object will be stored.
/// The object to be stored.
/// Specifies whether the expiration time of the entry should be refreshed. Defaults to true.
Task SetObjectAsync(string key, T obj, bool updateExpiration = true);
///
/// Asynchronously retrieves an object of type associated with the specified .
/// Supports an optional to apply a custom time-to-live and an flag
/// to control whether the entry's expiration is refreshed on access.
///
/// The unique identifier of the object to retrieve.
/// An optional time-to-live value that overrides the default expiration period; if , the default TTL is used.
/// A value indicating whether the object's expiration should be extended when it is successfully retrieved.
/// A that represents the asynchronous operation, containing the retrieved object of type , or if the object is not found.
Task GetObjectAsync(string key, TimeSpan? ttlOverride, bool updateExpiration);
///
/// Asynchronously stores the specified object associated with the given key, using an optional time-to-live override and expiration update behavior.
///
/// The identifier under which the object will be stored.
/// The object to store.
/// An optional that overrides the default time-to-live for the stored object.
/// A value indicating whether the expiration of the stored object should be updated based on the provided TTL.
/// A that represents the asynchronous storage operation.
Task SetObjectAsync(string key, T obj, TimeSpan? ttlOverride, bool updateExpiration);
///
/// Asynchronously deletes an object identified by the specified key.
///
/// The unique identifier of the object to delete.
Task DeleteObjectAsync(string key);
///
/// Asynchronously deletes entries matching the specified pattern and returns the number of deleted items.
///
/// The pattern used to match the entries to be deleted.
/// A task that represents the asynchronous delete operation. The task result contains the total number of entries that were deleted.
Task DeleteByPatternAsync(string pattern);
///
/// Clears the application cache, removing all cached entries.
///
void CleanCache();
// Métodos para transparencia y gestión de locks
// GetOrSet (string key)
///
/// Asynchronously retrieves a cached value for the specified key, or loads and stores it using the provided loader function when the key is not present.
///
/// The cache key used to identify the stored value.
/// The asynchronous function invoked to produce the value when no cached entry exists for the specified key.
/// An optional time-to-live duration that overrides the default expiration for the cached entry; when null, the default TTL is used.
/// A task that represents the asynchronous operation. The result is the cached or freshly loaded string value, or null when no value is available.
Task GetOrSetValueAsync(string key, Func> loader, TimeSpan? ttlOverride = null);
///
/// Asynchronously retrieves an object associated with the specified key from the cache, or invokes the factory to create and cache a new one when the key is not found.
///
/// The cache key used to identify the stored object.
/// The asynchronous function executed to produce the object when no cached value exists for the given key.
/// The optional expiration period for the cached entry; if null, the default cache lifetime is applied.
/// The token used to cancel the asynchronous operation.
/// A task that resolves to the cached or newly created object of type .
Task GetOrSetObjectAsync(
string key,
Func> factory,
TimeSpan? ttl = null,
CancellationToken cancellationToken = default);
// GetOrSet especializado para GroupedObservations
///
/// Asynchronously retrieves a cached object identified by the patient and grouped field, or creates and stores it via the supplied factory when no cached value exists.
///
/// The grouped field used to categorize and identify the cached object.
/// The identifier of the patient the object is associated with.
/// The asynchronous factory delegate invoked to produce the object when it is not found in the cache.
/// The optional time-to-live duration applied to the cached entry.
/// The token to monitor for cancellation requests.
/// A task containing the cached or newly created object of type .
Task GetOrSetObjectAsync(
GroupedField groupedField,
ObjectId patientId,
Func> factory,
TimeSpan? ttl = null,
CancellationToken cancellationToken = default);
}
}