<?php
/**
 * Handles a download click: increments the counter, then redirects to the
 * uploaded file (or back to the downloads page with a notice if no file
 * has been uploaded yet for that entry).
 */
require_once __DIR__ . '/includes/functions.php';

$id = (int) ($_GET['id'] ?? 0);
if ($id <= 0) {
    redirect('downloads.php');
}

$stmt = db()->prepare('SELECT * FROM downloads WHERE id = ? AND is_active = 1');
$stmt->execute([$id]);
$download = $stmt->fetch();

if (!$download) {
    redirect('downloads.php');
}

db()->prepare('UPDATE downloads SET download_count = download_count + 1 WHERE id = ?')->execute([$id]);

if (!empty($download['file_path'])) {
    redirect(assetUrl($download['file_path']));
}

flash('downloads_notice', 'That file has not been uploaded yet — please check back soon or contact us for a copy.');
redirect('downloads.php');
