<?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_Runner as JobRunner;
use AC_SYNC\Includes\Classes\Logger\Action_Wear_Log as Log;

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

  if (!JobRunner::isLocked()) {
    $response->success(['released' => false, 'message' => 'No lock active']);
    return $response->getResponse();
  }

  JobRunner::forceRelease();
  Log::write("Sync lock manually released via REST API", Log::WARNING, Log::CONTEXT_CRONJOB);

  $response->success(['released' => true]);
  return $response->getResponse();
});
$route->registerRoute();
