import { SetMetadata, UseInterceptors, applyDecorators } from '@nestjs/common';
import { HttpCacheInterceptor } from 'src/core/interceptors/http-cache.interceptor';
import { PUBLIC_CACHE_TTL } from '../constants/cache-keys.constant';

export const CACHE_RESPONSE_KEY = 'cache_response';

export interface CacheResponseOptions {
  /** Cache key prefix; route params are appended (e.g. `public:foods:restaurant` + `:slug`) */
  keyPrefix: string;
  /** TTL in seconds */
  ttl?: number;
  /** Route param names to include in the cache key (defaults to all route params) */
  params?: string[];
}

export const CacheResponse = (options: CacheResponseOptions) =>
  applyDecorators(
    SetMetadata(CACHE_RESPONSE_KEY, { ttl: PUBLIC_CACHE_TTL, ...options }),
    UseInterceptors(HttpCacheInterceptor),
  );
