import { Injectable, BadRequestException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { JobInvitation } from './entities/job_invitations.entity';

@Injectable()
export class JobInvitationsService {
  constructor(
    @InjectRepository(JobInvitation)
    private jobInvitationRepository: Repository<JobInvitation>,
  ) {}

  async inviteCandidate(data: { job_post_id: number; seeker_id: number; recruiter_id: number }) {
    // Check if invitation already exists
    const existing = await this.jobInvitationRepository.findOne({
      where: {
        job_post_id: data.job_post_id,
        seeker_id: data.seeker_id,
      },
    });

    if (existing) {
      throw new BadRequestException('Candidate is already invited to this job.');
    }

    const invitation = this.jobInvitationRepository.create(data);
    await this.jobInvitationRepository.save(invitation);

    // TODO: Wire up email notification here in the future if needed

    return { success: true, message: 'Candidate successfully invited to the job.' };
  }
}
