import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { JobPostAiSettings } from './entities/job_post_ai_settings.entity';

@Injectable()
export class JobPostAiSettingsService {
  constructor(
    @InjectRepository(JobPostAiSettings)
    private readonly jobPostAiSettingsRepository: Repository<JobPostAiSettings>,
  ) {}

  async createOrUpdate(data: Partial<JobPostAiSettings>) {
    const existing = await this.jobPostAiSettingsRepository.findOne({
      where: { job_post_id: data.job_post_id },
    });

    if (existing) {
      return this.jobPostAiSettingsRepository.save({
        ...existing,
        ...data,
      });
    }

    const newSettings = this.jobPostAiSettingsRepository.create(data);
    return this.jobPostAiSettingsRepository.save(newSettings);
  }

  async findByJobPostId(jobPostId: number) {
    return this.jobPostAiSettingsRepository.findOne({
      where: { job_post_id: jobPostId },
    });
  }
}
