<?php

namespace AC_SYNC\Includes\Classes\Async\Handlers;

if (!defined('ABSPATH')) exit;

use AC_SYNC\Includes\Classes\Async\Action_Wear_Job_Handler_Interface;
use AC_SYNC\Includes\Classes\Async\Action_Wear_Job_Result as JobResult;
use AC_SYNC\Includes\Classes\Async\Action_Wear_Job_Queue as JobQueue;
use AC_SYNC\Includes\Classes\Differential\Entities\Action_Wear_Price as PriceEntity;
use AC_SYNC\Includes\Classes\Logger\Action_Wear_Log as Log;

class Action_Wear_Diff_Price_Handler implements Action_Wear_Job_Handler_Interface
{
    public function handle(object $job, array $payload): JobResult
    {
        $entity = new PriceEntity();
        $entity->execute();
        $entity->updateExecutionTime();

        Log::write("DiffPrice: Differential price sync completed, next in " . $entity->getUpdateTiming() . "s", Log::INFO, Log::CONTEXT_CRONJOB);

        // Self-reschedule
        $nextRun = gmdate('Y-m-d H:i:s', time() + $entity->getUpdateTiming());
        JobQueue::push('diff_price', null, 50, $nextRun, 'price_sync', 5, (int) $job->id);

        return JobResult::success();
    }
}
