import { Token } from '../../../domain/entities/token';
import { ITokenRepository } from '../../../domain/repositories/token-repository';
import { ICacheService } from '../../ports/cache-service';
import { PaginationInput } from '../../dto/pagination.dto';

const CACHE_TTL_SECONDS = 5 * 60; // 5 minutes

export class GetAllTokensUseCase {
  constructor(
    private readonly tokenRepository: ITokenRepository,
    private readonly cacheService: ICacheService,
  ) {}

  async execute(input: PaginationInput): Promise<Token[]> {
    const cacheKey = `tokens:all:limit:${input.limit}:offset:${input.offset}`;
    return this.cacheService.wrap<Token[]>(
      cacheKey,
      () =>
        this.tokenRepository.findAll({
          limit: input.limit,
          offset: input.offset,
        }),
      CACHE_TTL_SECONDS,
    );
  }
}
