import { corePool } from "@/lib/db/mysql";
import SafeText from "@/components/ui/safetext";

type Job = {
  id: number;
  title: string;
  company_name: string;
  description: string;
  missions: string | null;
  profile: string | null;
  location: string | null;
};

async function getJob(jobid: number): Promise<Job | null> {
  const [[job]]: any = await corePool.query(
    `
    SELECT id, title, company_name, description, missions, profile, location
    FROM jobs
    WHERE id = ? AND status = 'published'
    `,
    [jobid]
  );

  return job ?? null;
}

export default async function JobPage({
  params,
}: {
  params: { jobid: string };
}) {
  const jobid = Number(params.jobid);
  if (!jobid) return null;

  const job = await getJob(jobid);
  if (!job) return null;

  return (
    <main className="max-w-4xl mx-auto px-4 py-12 space-y-8">
      <header>
        <h1 className="text-3xl font-semibold">{job.title}</h1>
        <p className="text-slate-600 mt-1">
          {job.company_name} â€” {job.location}
        </p>
      </header>

      <section>
        <h2 className="text-lg font-semibold mb-2">Description du poste</h2>
        <SafeText
          value={job.description}
          className="space-y-3 text-slate-800"
        />
      </section>

      {job.missions && (
        <section>
          <h2 className="text-lg font-semibold mb-2">Missions</h2>
          <SafeText
            value={job.missions}
            className="space-y-2 text-slate-800"
          />
        </section>
      )}

      {job.profile && (
        <section>
          <h2 className="text-lg font-semibold mb-2">Profil recherchÃ©</h2>
          <SafeText
            value={job.profile}
            className="space-y-2 text-slate-800"
          />
        </section>
      )}
    </main>
  );
}
