<?php

namespace auctionforge\ajax;

use Exception;
use auctionforge\ajax\abs\AjaxHandler;
use auctionforge\inc\BidSpiritRequest;
use auctionforge\inc\controllers\BidspiritLots;
use auctionforge\inc\controllers\PolylangSupport;
use auctionforge\inc\controllers\Settings;
use auctionforge\inc\Debug;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\model\LotMetaData;
use WP_Post;

class ShowcaseAction extends AJAXHandler
{
    function callback ()
    {
        check_ajax_referer( 'bp-ajax-string', 'security' );

        if (!is_plugin_active('woocommerce/woocommerce.php')) {
            wp_send_json_error(array(
                'message' => 'Showcase don\'t available'
            ));
        }

        $data = $_POST;

        $inCart = false;
        $productId = wc_get_product_id_by_sku($data['skuCode']);
        $carlUrl = wc_get_cart_url();
        if ($productId) {
            $product = wc_get_product($productId);
            $inCart = WC()->cart->add_to_cart($product->get_id());
        }

        $response = array(
            'in_cart' => $inCart,
            'product_id' => $productId,
            'code' => $data['skuCode'],
            'cart_url' => $carlUrl
        );
        if ($productId && $inCart) {
            wp_send_json_success($response);
        } else {
            wp_send_json_error(array(
                'notice' => 'Product not found for sku: ' . $data['skuCode']
            ));
        }
    }
}
$gate = new ShowcaseAction();
$gate->init('do_showcase');