<?php
// job_status.php
declare(strict_types=1);

require __DIR__ . '/db.php';

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) { http_response_code(400); header('Content-Type: application/json; charset=utf-8'); echo json_encode(["ok"=>false,"error"=>"id gerekli"], JSON_UNESCAPED_UNICODE); exit; }


$stmt = $pdo->prepare("SELECT id,status,progress,total,last_b,last_g,sheet_ok,sheet_error,created_at,started_at,finished_at,last_error FROM a3_jobs WHERE id=?");
$stmt->execute([$id]);
$row = $stmt->fetch();

if (!$row) { http_response_code(404); header('Content-Type: application/json; charset=utf-8'); echo json_encode(["ok"=>false,"error"=>"job bulunamadı"], JSON_UNESCAPED_UNICODE); exit; }

header('Content-Type: application/json; charset=utf-8');
echo json_encode([
  "ok"=>true,
  "id"=>(int)$row["id"],
  "status"=>$row["status"],
  "progress"=>(int)$row["progress"],
  "total"=>(int)$row["total"],
  "sheet_ok"=>$row["sheet_ok"]===null?null:(bool)$row["sheet_ok"],
  "sheet_error"=>$row["sheet_error"],
  "created_at"=>$row["created_at"],
  "started_at"=>$row["started_at"],
  "finished_at"=>$row["finished_at"],
  "last_error"=>$row["last_error"],
  "last_b" => $row["last_b"],
"last_g" => $row["last_g"],
"percent" => ($row["total"] > 0) ? round(($row["progress"]/$row["total"])*100, 1) : 0
  
], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
