import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { OfacScreening } from '../../domain/entities/ofac-screening.entity';
import { IOfacScreeningRepository } from '../../domain/repositories/ofac-screening.repository.interface';

@Injectable()
export class OfacScreeningRepository implements IOfacScreeningRepository {
  constructor(
    @InjectRepository(OfacScreening)
    private readonly repo: Repository<OfacScreening>,
  ) {}

  create(data: Partial<OfacScreening>): OfacScreening {
    return this.repo.create(data);
  }

  async save(screening: OfacScreening): Promise<OfacScreening> {
    return this.repo.save(screening);
  }

  async findByUserId(userId: string): Promise<OfacScreening[]> {
    return this.repo.find({ where: { userId }, order: { screenedAt: 'DESC' } });
  }

  async count(options?: { where?: Record<string, any> }): Promise<number> {
    return this.repo.count(options);
  }

  async findMatchesPaginated(page: number, limit: number): Promise<[OfacScreening[], number]> {
    return this.repo.findAndCount({
      where: { isMatch: true },
      order: { screenedAt: 'DESC' },
      skip: (page - 1) * limit,
      take: limit,
    });
  }
}
