import sql from "../db/db";
import { Job } from "../types/job";

export const saveJobDescriptions = async (jobs: Job[]) => {
  if (jobs.length === 0) {
    console.log("No jobs to save descriptions for.");
    return;
  }

  try {
    const jobDescriptionsForDb = jobs.map((job) => ({
      job_id: job.id,
      description: job.description,
      job_criteria: job.criteria,
    }));

    await sql`
      INSERT INTO public.job_descriptions ${sql(
        jobDescriptionsForDb,
        "job_id",
        "description",
        "job_criteria"
      )}
      ON CONFLICT (job_id) DO UPDATE SET
        description = EXCLUDED.description,
        job_criteria = EXCLUDED.job_criteria,
        updated_at = CURRENT_TIMESTAMP
    `;
    console.log(`Updated descriptions for ${jobs.length} jobs.`);
  } catch (error) {
    console.error("Error updating job descriptions:", error);
  }
};
