import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ApiKey } from '../../domain/entities/api-key.entity';

@Injectable()
export class ApiKeyRepository {
  constructor(
    @InjectRepository(ApiKey)
    private readonly repo: Repository<ApiKey>,
  ) {}

  async findByKeyHash(keyHash: string): Promise<ApiKey | null> {
    return this.repo.findOneBy({ keyHash });
  }

  async findByUserId(userId: string): Promise<ApiKey[]> {
    return this.repo.find({ where: { userId } });
  }

  async save(apiKey: ApiKey): Promise<ApiKey> {
    return this.repo.save(apiKey);
  }
}
