<?php

namespace AC_SYNC\Includes\Classes\Differential\Entities;

if (!defined('ABSPATH'))
    exit; // Exit if accessed directly
use AC_SYNC\Includes\Classes\Differential\Entities\Action_Wear_Entities as Entities;
use AC_SYNC\Includes\Classes\Differential\Entities\Action_Wear_Entities_Interface as EntitiesInterface;
use AC_SYNC\Includes\Classes\Logger\Action_Wear_Log as Log;
use AC_SYNC\Includes\Classes\Product\Action_Wear_Product as Product;
use AC_SYNC\Includes\Classes\Async\Action_Wear_Job_Queue as JobQueue;

class Action_Wear_Product_Differential extends Entities implements EntitiesInterface
{

    const DRY_RUN = false;
    const UPDATE_TIMING = 3600 * 36;

    public function getUpdateTiming(): int
    {
        return self::UPDATE_TIMING;
    }

    public function execute(): bool
    {
        global $wpdb;
        try {
            // Truncates moved inside generateProducts() and generateImages()
            // so they only run AFTER the API has responded successfully
            Product::filterDeletedProducts();
            JobQueue::push('catalog_generate_categories', null, 80, null, 'catalog_sync');
            JobQueue::push('catalog_generate_products', null, 80, null, 'catalog_sync');
            JobQueue::push('catalog_generate_images', null, 80, null, 'catalog_sync');
            Log::write("Aggiunti gli aggiornamenti di brands, categorie, prodotti ed immagini alla coda", Log::INFO, Log::CONTEXT_CRONJOB);
        } catch (\Exception $e) {
            Log::write($e->getMessage(), Log::ERROR, Log::CONTEXT_PRODUCT_CREATION);
        }
        $this->updateExecutionTime();
        return true;
    }
}
