<?php
/**
 * POST /api/tasks/execute
 * Execute a confirmed reservation.
 *
 * Request body:
 *   { "token": "EST_8b3c9f2e..." }
 *
 * Response (success):
 *   { ok:true, tasks_completed:N, tasks_failed:0,
 *     reserved_sdg:X, actual_sdg:Y, refunded_sdg:Z,
 *     results:[...] }
 *
 * Response (token invalid/expired):
 *   HTTP 400 { error:"invalid_or_expired_token"|"price_snapshot_expired" }
 *
 * Response (already activated):
 *   HTTP 400 { error:"reservation_already_activated" }
 */
require_once __DIR__ . '/../../config.php';
require_once __DIR__ . '/../../engine/Kernel.php';

$ctx  = Kernel::handle(['auth' => true, 'rate' => 'api']);
$body = Kernel::require(['token']);

$token = preg_replace('/[^A-Za-z0-9_]/', '', $body['token'] ?? '');
if (strlen($token) < 8) Kernel::fail('invalid_token_format', 'رمز التأكيد غير صالح', 400);

// Validate, debit, execute, reconcile — all inside Kernel
Kernel::executeWithReservation($token);
