import type { IRefreshTokenRepository } from '../../../domain/ports/refresh-token-repository'
import type { ITokenService } from '../../../domain/ports/token-service'

export class LogoutStaff {
  constructor(
    private readonly tokenService: ITokenService,
    private readonly refreshTokenRepo: IRefreshTokenRepository,
  ) {}

  async execute(rawToken: string): Promise<void> {
    const tokenHash = this.tokenService.hashToken(rawToken)
    const record = await this.refreshTokenRepo.findValidByHash(tokenHash)
    // Si el token no existe o ya estaba revocado, no se devuelve error (no leak)
    if (record) {
      await this.refreshTokenRepo.revoke(record.id)
    }
  }
}
