import { Token } from '../../../domain/entities/token';
import { NotFoundError } from '../../../domain/errors/not-found-error';
import { ITokenRepository } from '../../../domain/repositories/token-repository';
import { INetworkRepository } from '../../../domain/repositories/network-repository';
import { ICacheService } from '../../ports/cache-service';

const CACHE_TTL_SECONDS = 5 * 60; // 5 minutes

export class GetTokensByNetworkUseCase {
  constructor(
    private readonly tokenRepository: ITokenRepository,
    private readonly networkRepository: INetworkRepository,
    private readonly cacheService: ICacheService,
  ) {}

  async execute(networkId: string): Promise<Token[]> {
    const network = await this.networkRepository.findById(networkId);
    if (!network) {
      throw new NotFoundError('Network', networkId);
    }

    const cacheKey = `tokens:network:${networkId}`;
    return this.cacheService.wrap<Token[]>(
      cacheKey,
      () => this.tokenRepository.findByNetworkId(networkId),
      CACHE_TTL_SECONDS,
    );
  }
}
