cat > lib/db/queries.js <<'JS'
import { getPool } from "./pool.js";

function clampInt(v, def, min, max) {
  const n = Number.parseInt(v, 10);
  if (Number.isNaN(n)) return def;
  return Math.min(Math.max(n, min), max);
}

export async function listJobs({ limit = 50, offset = 0 } = {}) {
  const lim = clampInt(limit, 50, 1, 500);
  const off = clampInt(offset, 0, 0, 1000000);

  const pool = getPool();
  const [rows] = await pool.query(
    "SELECT * FROM jobs ORDER BY id DESC LIMIT ? OFFSET ?",
    [lim, off]
  );
  return rows;
}

export async function getJobById(id) {
  const pool = getPool();
  const [rows] = await pool.query(
    "SELECT * FROM jobs WHERE id = ? LIMIT 1",
    [Number(id)]
  );
  return rows[0] || null;
}

export async function listCandidates({ limit = 50, offset = 0 } = {}) {
  const lim = clampInt(limit, 50, 1, 500);
  const off = clampInt(offset, 0, 0, 1000000);

  const pool = getPool();
  const [rows] = await pool.query(
    "SELECT * FROM candidates ORDER BY id DESC LIMIT ? OFFSET ?",
    [lim, off]
  );
  return rows;
}
JS

