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

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

  try {
    const jobsForDb = jobs.map((job) => ({
      id: job.id,
      title: job.title,
      company: job.company,
      location: job.location,
      company_link: job.companyLink,
      job_link: job.jobLink,
      provider: job.provider,
      status: job.status,
      job_timestamp:
        job.jobPostTime instanceof Date && !isNaN(job.jobPostTime.getTime())
          ? job.jobPostTime
          : new Date(),
    }));

    const result = await sql`
      INSERT INTO public.jobs ${sql(
        jobsForDb,
        "id",
        "title",
        "company",
        "location",
        "company_link",
        "job_link",
        "provider",
        "status",
        "job_timestamp"
      )}
      ON CONFLICT (id) DO UPDATE SET
        title = EXCLUDED.title,
        company = EXCLUDED.company,
        company_link = EXCLUDED.company_link,
        location = EXCLUDED.location,
        job_link = EXCLUDED.job_link,
        job_timestamp = EXCLUDED.job_timestamp
    `;

    console.log(`Saved ${jobs.length} jobs to the database.`);
  } catch (error) {
    console.error("Error saving jobs to the database:", error);
  }
};
