<?php
chdir('../');
ob_start();
session_start();


require_once("public/connection.php");
require_once("public/functions.php");

$id = $_GET['id'] ?? 0;

$stmt = $connection->prepare("SELECT file_path FROM curriculum WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$res = $stmt->get_result();

if ($res->num_rows === 1) {
    $file = $res->fetch_assoc()['file_path'];

    if (file_exists($file)) {
        // Force download
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
        header('Content-Length: ' . filesize($file));
        flush();
        readfile($file);
        exit;
    } else {
        $_SESSION['msg'] = "File not found on server.";
        $_SESSION['type'] = "danger";
    }
} else {
    $_SESSION['msg'] = "Curriculum not found.";
    $_SESSION['type'] = "danger";
}

// Redirect back
header("Location: view_curriculum");
exit;
