import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CompanyRecruiters } from './entities/company_recruiters.entity';

@Injectable()
export class CompanyRecruitersService {
  [x: string]: any;
  constructor(
    @InjectRepository(CompanyRecruiters)
    private readonly companyRecruitersRepo: Repository<CompanyRecruiters>,
  ) {}

  async findAll(): Promise<CompanyRecruiters[]> {
    return this.companyRecruitersRepo.find();
  }

  async findOne(id: number): Promise<CompanyRecruiters> {
    const record = await this.companyRecruitersRepo.findOne({ where: { id } });
    if (!record) throw new NotFoundException('CompanyRecruiter not found');
    return record;
  }

  async create(data: Partial<CompanyRecruiters>): Promise<CompanyRecruiters> {
    const newRecord = this.companyRecruitersRepo.create(data);
    return this.companyRecruitersRepo.save(newRecord);
  }

  async update(
    id: number,
    data: Partial<CompanyRecruiters>,
  ): Promise<CompanyRecruiters> {
    const record = await this.findOne(id);
    Object.assign(record, data);
    return this.companyRecruitersRepo.save(record);
  }

  async remove(id: number): Promise<{ message: string }> {
    const record = await this.findOne(id);
    await this.companyRecruitersRepo.remove(record);
    return { message: 'Company recruiter record deleted successfully' };
  }
}
