import { Cache, TTL } from '../cache/Cache.js'
import { Service } from '../core/Service.js'

export default class CacheService extends Service {
  static override key = 'api-service/cache'

  private cache: Cache

  get TTL(): typeof TTL {
    return TTL
  }

  constructor() {
    super()
    this.cache = new Cache()
  }

  getCache(): Cache {
    return this.cache
  }

  /**
   * Store data in the cache with optional TTL
   *
   * @param key - The key to store the data under
   * @param data - The data to store
   * @param ttl - Time to live in milliseconds
   * @returns A promise that resolves to true if the data was stored successfully, false otherwise
   */
  async set<T>(key: string, data: T, ttl?: number): Promise<boolean> {
    return this.cache.set(key, data, ttl)
  }

  /**
   * Retrieve data from the cache with type safety
   */
  async get<T = unknown>(key: string): Promise<T | null> {
    return this.cache.get(key)
  }

  /**
   * Delete a specific cache item
   */
  async delete(key: string): Promise<boolean> {
    return this.cache.delete(key)
  }

  async clear(): Promise<void> {
    await this.cache.clear()
  }

  override async destroy(): Promise<void> {
    await this.cache.close()
    await super.destroy()
  }
}
