<?php
declare(strict_types=1);
$authApp = 'autos';
session_start();
require __DIR__ . '/../../config/auth.php';
require __DIR__ . '/../../../../../config/db2.php';
require __DIR__ . '/../../lib/helpers.php';
header('Content-Type: application/json');

$rows = $pdo->query("
    SELECT * FROM agj_autos_apertura
    WHERE deleted_at IS NULL
      AND estado NOT IN ('Cancelado','Realizado','Abreviado')
")->fetchAll();

$upd   = $pdo->prepare("UPDATE agj_autos_apertura SET prioridad = ? WHERE id = ?");
$count = 0;
foreach ($rows as $aa) {
    $upd->execute([calcular_prioridad($aa), $aa['id']]);
    $count++;
}

echo json_encode(['ok' => true, 'actualizados' => $count]);
