<?php
/**
 * Secure resume download
 *   download.php            → forces download
 *   download.php?view=1     → opens inline in the browser (PDF viewer)
 * Every hit is logged to storage/downloads.csv
 */
require __DIR__ . '/includes/helpers.php';

$file = realpath($CONFIG['resume_path']);
if (!$file || !is_readable($file)) {
    http_response_code(404);
    exit('Resume not found.');
}

$inline = isset($_GET['view']);

log_csv('downloads.csv', [
    'date'     => date('c'),
    'type'     => $inline ? 'view' : 'download',
    'ip'       => client_ip(),
    'agent'    => mb_substr($_SERVER['HTTP_USER_AGENT'] ?? '', 0, 200),
    'referrer' => mb_substr($_SERVER['HTTP_REFERER'] ?? '', 0, 200),
]);

if (ob_get_level()) {
    ob_end_clean();
}
header('Content-Type: application/pdf');
header('Content-Disposition: ' . ($inline ? 'inline' : 'attachment') . '; filename="' . $CONFIG['resume_name'] . '"');
header('Content-Length: ' . filesize($file));
header('X-Content-Type-Options: nosniff');
header('Cache-Control: private, max-age=0, must-revalidate');
readfile($file);
exit;
