<?php

use auctionforge\inc\controllers\Settings;
use auctionforge\inc\Debug;
use auctionforge\inc\helpers\LotHelper;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\import\AuctionList;
use auctionforge\inc\import\LotList;
use auctionforge\inc\import\SilentMigrator;
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_START, 90) || WP_DEBUG) {
    Debug::logData(
        array(
            "info" => "Start \"Upcoming auction\" process",
            "is_debug_enable" => WP_DEBUG
        )
    );
    $autoCommit = SettingsHelper::isChecked(Settings::AUTO_COMMIT_DISABLED, 'off');
    $db = InstanceDB::getInstanceDB();

//    $migrator = new SilentMigrator();
//    $migrator->run();

    $auctionList = new AuctionList();
    $auctionList->init();
    if (!empty($auctionList->getErrors())) {
        $errors = $auctionList->getErrors();
        var_dump($errors);
        Debug::logData($errors, "auction-list");
    }

    $lotList = new LotList();
    $lotList->setPriority(4);
    $lotList->init();
    if (!empty($lotList->getErrors())) {
        $errors = $lotList->getErrors();
        var_dump($errors);
        Debug::logData($errors, "lot-list-new");
    }

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

    if ($autoCommit) {
        $db->query('COMMIT;');
    }

    $logData["info"] = "\"Upcoming auction\" process is finished";
    if (!empty($auctionList->getLoggedInfo())) {
        $logData["auctions"] = $auctionList->getLoggedInfo();
    }
    if (!empty($lotList->getLoggedInfo())) {
        $logData["lots"] = $lotList->getLoggedInfo();
    }
    Debug::logData($logData);
}
