<?php

use auctionforge\inc\controllers\Settings;
use auctionforge\inc\Debug;
use auctionforge\inc\helpers\LotHelper;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\import\InsertLots;
use auctionforge\inc\import\InsertLotsBS;
use auctionforge\inc\InstanceDB;
use auctionforge\inc\model\Auction;

if (!empty($_SERVER['DOCUMENT_ROOT'])) {
    die("Access denied");
}

define('CRON_TASKS_BASE_PATH', __DIR__);
define('WP_USE_THEMES', false);
require(CRON_TASKS_BASE_PATH . '/../../../../wp-load.php');

if (!is_plugin_active(basename(dirname(__DIR__)) . '/' . basename(dirname(__DIR__)) . '.php')) {
    die("Plugin AuctionForge disabled\r\n");
}

if (LotHelper::startSync(AUCTIONFORGE_SYNC_DATA_START, 10) || WP_DEBUG) {
    Debug::logData(
        array(
            "info" => "Sync data from ImportHistory table to posts",
            "is_debug_enable" => WP_DEBUG
        )
    );
    $autoCommit = SettingsHelper::isChecked(Settings::AUTO_COMMIT_DISABLED, 'off');
    $db = InstanceDB::getInstanceDB();

    $insertLots = new InsertLotsBS();
    $insertLots->init();

    update_option(AUCTIONFORGE_SYNC_DATA_START, "0", 'no');

    if ($autoCommit) {
        $db->query('COMMIT;');
    }
    Debug::logData(array(
        "info" => "Sync data from ImportHistory finished",
        "insert" => $insertLots->getLoggedInfo()
    ));
} else {
    Debug::logData(array("info" => "ImportData in queue"));
}