<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/common/ops_service.php';

if (PHP_SAPI !== 'cli') {
    echo "CLI only.\n";
    exit(1);
}

function golder_job_start(PDO $master, string $jobName): int
{
    golder_ensure_job_runs_table($master);
    $st = $master->prepare('INSERT INTO job_runs (job_name, status, details_json, started_at) VALUES (:n, :s, :d, UTC_TIMESTAMP())');
    $st->execute([':n' => $jobName, ':s' => 'running', ':d' => '{}']);
    return (int)$master->lastInsertId();
}

function golder_job_finish(PDO $master, int $jobId, string $status, array $details = []): void
{
    $json = json_encode($details, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    if (!is_string($json)) {
        $json = '{}';
    }
    $st = $master->prepare('UPDATE job_runs SET status = :s, details_json = :d, finished_at = UTC_TIMESTAMP() WHERE id = :id');
    $st->execute([':s' => $status, ':d' => $json, ':id' => $jobId]);
}


