<?php

namespace auctionforge\ajax;

use auctionforge\inc\abs\Model;
use auctionforge\inc\Debug;
use auctionforge\inc\import\AuctionList;
use auctionforge\inc\import\InsertLotsBS;
use auctionforge\inc\import\LotList;
use auctionforge\ajax\abs\AjaxHandler;

class SyncNow extends AjaxHandler
{
    public function callback()
    {
        check_ajax_referer( 'do_sync_now', 'security' );

        $args = wp_parse_args($_POST, array(
            'do' => 'importBidSpirit',
            'mode' => 0,
            'type' => true
        ));

        if ($args['do'] == 'importBidSpirit') {
            $auctionList = new AuctionList();
            $auctionList->init();

            $errors = $auctionList->getErrors();
            if (!empty($errors)) {
                $logErrors = $auctionList->getErrors();
                Debug::logData($logErrors, "auction-list-ajax");
            }
            $debug["errors"] = $auctionList->getErrors();

            $lotList = new LotList();
            $lotList->setPriority(3);
            $lotList->init(Model::toBoolean($args["type"]));
            $errors = $lotList->getErrors();
            if (!empty($errors)) {
                $logErrors = $lotList->getErrors();
                Debug::logData($logErrors, "lot-list-new");
            }

            // Pull the BidSpirit Shop on the same button. No-op unless enabled.
            $shopList = new \auctionforge\inc\import\ShopList();
            $debug["shop"] = $shopList->init();

            wp_send_json_success([
                "success" => true,
                "data" => $auctionList->getErrors(),
                "debug" => $debug,
                "params" => $args
            ]);
        } elseif ($args['do'] == 'importData') {
            $insertLots = new InsertLotsBS();
            $insertLots->init($args);

            wp_send_json_success([
                "success" => true,
                "data" => [],
                "params" => $args
            ]);
        }
    }
}
$syncNow = new SyncNow();
$syncNow->init('do_sync_now');
