import { multiLayerCache } from './multi-layer-cache';

/**
 * EN: Cache service wrapper for common operations
 * VI: Service cache wrapper cho các thao tác thông thường
 */
export class CacheService {
  /**
   * EN: Get value from cache
   * VI: Lấy giá trị từ cache
   */
  async get<T>(key: string): Promise<T | null> {
    return multiLayerCache.get<T>(key);
  }

  /**
   * EN: Set value in cache
   * VI: Lưu giá trị vào cache
   */
  async set(key: string, value: any, ttlSeconds?: number): Promise<void> {
    return multiLayerCache.set(key, value, ttlSeconds);
  }

  /**
   * EN: Get from cache or fetch from source
   * VI: Lấy từ cache hoặc lấy từ nguồn
   */
  async getOrSet<T>(
    key: string,
    fetchFn: () => Promise<T>,
    ttlSeconds: number = 300
  ): Promise<T> {
    return multiLayerCache.getOrSet(key, fetchFn, ttlSeconds);
  }

  /**
   * EN: Delete from cache
   * VI: Xóa khỏi cache
   */
  async del(key: string): Promise<void> {
    return multiLayerCache.del(key);
  }

  /**
   * EN: Delete multiple keys
   * VI: Xóa nhiều keys
   */
  async delMany(keys: string[]): Promise<void> {
    return multiLayerCache.delMany(keys);
  }

  /**
   * EN: Invalidate cache by pattern
   * VI: Làm mất hiệu lực cache theo pattern
   */
  async invalidatePattern(pattern: string): Promise<void> {
    return multiLayerCache.invalidatePattern(pattern);
  }

  /**
   * EN: Cache key generators
   * VI: Tạo cache keys
   */
  keys = {
    user: (userId: string) => `user:${userId}`,
    userPermissions: (userId: string) => `user:${userId}:permissions`,
    userRoles: (userId: string) => `user:${userId}:roles`,
    token: (token: string) => `token:${token}`,
    session: (sessionId: string) => `session:${sessionId}`,
    permission: (permissionId: string) => `permission:${permissionId}`,
    role: (roleId: string) => `role:${roleId}`,
  };
}

export const cacheService = new CacheService();
