<?php
require_once __DIR__ . '/../config/bootstrap.php';
admin_require_login();
require_method('POST');

$pdo = db();
$data = get_json_body();
$id = (int)($data['id'] ?? 0);
$status = (string)($data['status'] ?? '');

if ($id <= 0) json_response(['ok'=>false,'error'=>'ID inválido.'], 422);
if (!in_array($status, ['draft','published','archived'], true)) {
  json_response(['ok'=>false,'error'=>'Status inválido.'], 422);
}

if ($status === 'published') {
  $st = $pdo->prepare("UPDATE eventos SET status='published', published_at=IFNULL(published_at,NOW()), updated_at=NOW() WHERE id=?");
  $st->execute([$id]);
} else {
  $st = $pdo->prepare('UPDATE eventos SET status=?, updated_at=NOW() WHERE id=?');
  $st->execute([$status, $id]);
}

json_response(['ok'=>true]);
