<?php
// backend/api/export_pdf.php
require_once "../lib/pdf_generator.php";
require_once "../config/database.php";

session_start();
if (!isset($_SESSION['user_id'])) {
    http_response_code(401);
    exit;
}

$resume_id = $_GET['id'] ?? null;
if (!$resume_id) {
    http_response_code(400);
    exit;
}

$database = new Database();
$db = $database->getConnection();

$query = "SELECT data, template FROM resumes WHERE id = :id AND user_id = :user_id";
$stmt = $db->prepare($query);
$stmt->execute([":id" => $resume_id, ":user_id" => $_SESSION['user_id']]);
$resume = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$resume) {
    http_response_code(404);
    exit;
}

$pdf = new PDFGenerator();
$pdf->generate($resume['data'], $resume['template']);
$pdf->Output('resume.pdf', 'D');
?>