import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { BaseRepository } from '@edr/api-common';
import { ExternalProfile } from './entities/external-profile.entity';

@Injectable()
export class ExternalProfileRepository extends BaseRepository<ExternalProfile> {
  constructor(
    @InjectRepository(ExternalProfile)
    repo: Repository<ExternalProfile>,
  ) {
    super(repo);
  }

  async findByUserId(userId: string): Promise<ExternalProfile | null> {
    return this.repository.findOne({
      where: { userId } as any,
      relations: ['company'],
    });
  }

  async findByCompanyId(companyId: string): Promise<ExternalProfile[]> {
    return this.repository.find({ where: { companyId } as any });
  }

  async findByEmail(email: string): Promise<ExternalProfile | null> {
    return this.repository.findOne({ where: { email } as any });
  }
}
