<?php
declare(strict_types=1);

define('CLI_SCRIPT', true);

require __DIR__ . '/../../public/index.php'; // loads bootstrap + app (safe in this project)

use App\Core\Database;

$db = Database::getInstance();

$cid = (int)($argv[1] ?? 0);
$type = (string)($argv[2] ?? 'reviewer');

if ($cid <= 0) {
    fwrite(STDERR, "Usage: php generate_cert_pdfs.php <conference_id> [reviewer|tpc|attendance]\n");
    exit(1);
}

$st = $db->prepare("SELECT cert_code FROM certificates WHERE conference_id=:cid AND cert_type=:ct");
$st->execute([':cid'=>$cid, ':ct'=>$type]);
$codes = $st->fetchAll(PDO::FETCH_COLUMN);

echo "Found ".count($codes)." certificates\n";

$base = "https://www.jpu.edu.jo/easychair/public";
foreach ($codes as $code) {
    $url = $base . "/cert/pdf?code=" . urlencode((string)$code);
    // hit URL to generate cache (wkhtmltopdf will run server-side)
    @file_get_contents($url);
    echo ".";
}
echo "\nDone\n";
