<?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("/resync_prices");
$route->setMethods("POST");
$route->setCallback(function () {

    $response = new ApiResponse();

    // Guard: 409 if already active in price_sync conflict group
    if (JobQueue::hasRunning('price_sync') || JobQueue::hasPending('bulk_price_fetch')) {
        $response->error("Price sync is already running", 409);
        return $response->getResponse();
    }

    JobQueue::push('bulk_price_fetch', null, 30, null, 'price_sync');

    $response->success([], "Prices update task created!");
    return $response->getResponse();
});
$route->registerRoute();
