<?php
include 'config.php';

$docId = $_GET['id'] ?? 0;
$stmt = $db->prepare("SELECT * FROM documents WHERE id = ?");
$stmt->execute([$docId]);
$document = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$document || $document['status'] != 'signed' || !file_exists($document['signed_path'])) {
    die("Documento firmado no disponible.");
}

// Forzar descarga del PDF firmado
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($document['signed_path']) . '"');
header('Content-Length: ' . filesize($document['signed_path']));
readfile($document['signed_path']);
exit;
?>