<?php
if (!defined('ABSPATH'))
  exit; // Exit if accessed directly
use AC_SYNC\Includes\Classes\Api\Action_Wear_Api_Route;
use AC_SYNC\Includes\Classes\Api\Action_Wear_API_Response as ApiResponse;
use AC_SYNC\Includes\Classes\Async\Action_Wear_Job_Queue as JobQueue;

$route = new Action_Wear_Api_Route("/cancel_task");
$route->setMethods("POST");
$route->setCallback(function () {
  $response = new ApiResponse();

  $data = json_decode(file_get_contents("php://input"));
  $id = (int) ($data->id ?? 0);

  if ($id <= 0) {
    $response->fail("Invalid job ID");
    return new WP_REST_Response($response->getResponse(), 422);
  }

  JobQueue::cancel($id);

  $response->success(true);
  return $response->getResponse();
});
$route->registerRoute();
