<?php
@ini_set('display_errors','0');
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);
if (session_status() === PHP_SESSION_NONE) session_start();
header('Content-Type: application/json; charset=utf-8');
require_once __DIR__ . '/../helpers.php';
require_once __DIR__ . '/../auth.php';
require_once __DIR__ . '/../db_bootstrap.php';
require_once __DIR__ . '/../subscribers_queue_lib.php';
function outs(array $a,int $code=200){ http_response_code($code); echo json_encode($a,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); exit; }
require_login('index.php?login=1');
$conn=ensure_db(true);
try{
  sq_ensure_schema($conn);
  $jobId=(int)($_GET['job_id']??0);
  $job=$jobId>0 ? sq_get_job($conn,$jobId) : null;
  if($job){ $job['payload']=json_decode((string)($job['payload_json']??''),true); $job['result']=json_decode((string)($job['result_json']??''),true); unset($job['payload_json'],$job['result_json']); }
  outs(['ok'=>true,'job'=>$job,'latest'=>sq_latest_jobs($conn,8)]);
}catch(Throwable $e){ outs(['ok'=>false,'message'=>$e->getMessage()],500); }
