<?php

namespace AC_SYNC\Includes\Classes\Setting {
  if (!defined('ABSPATH'))
    exit; // Exit if accessed directly
  ignore_user_abort(true);

  use AC_SYNC\Includes\Classes\Product\Action_Wear_Product as Product;
  use AC_SYNC\Includes\Classes\Logger\Action_Wear_Log as Log;
  use AC_SYNC\Includes\Classes\Async\Action_Wear_Job_Queue as JobQueue;

  /**
   * Elimina e re-sincronizza tutti i prodotti CAMAC
   *
   **/
  class Action_Wear_Setting_Resync_Products implements Action_Wear_Setting_Entity
  {

    private $onlyMissing = false;

    public function setOnlyMissing($onlyMissing)
    {
      $this->onlyMissing = $onlyMissing;
    }
    public function toggle()
    {
      return $this->set(!$this->get());
    }

    public function get()
    {
      return get_option("_ACTIONWEAR_LAST_RESYNCED_PRODUCTS", 0);
    }

    public function validate($v): bool
    {
      return is_bool($v);
    }

    public function set($value)
    {
      global $wpdb;
      if (!$this->validate($value))
        throw new \Exception("Only boolean values are accepted to this setting");

      @set_time_limit(300);

      try {
        Product::generateImages();
      } catch (\Throwable $e) {
        Log::write("Resync: generateImages failed: " . $e->getMessage(), Log::ERROR, Log::CONTEXT_PRODUCT_CREATION);
      }

      if (!$this->onlyMissing)
        $wpdb->query("UPDATE {$wpdb->prefix}actionwear_products SET wc_created = 0;");

      $query = "SELECT sku FROM " . $wpdb->prefix . "actionwear_products WHERE wc_to_create = 1 ";
      if ($this->onlyMissing) {
        $query .= "AND wc_created = 0";
      }
      $skus = $wpdb->get_col($query);

      $jobCount = 0;
      if (empty($skus)) {
        Log::write("Resync: No SKUs found with wc_to_create=1" . ($this->onlyMissing ? " AND wc_created=0" : ""), Log::WARNING, Log::CONTEXT_CRONJOB);
      } else {
        Log::write("Resync: Queuing " . count($skus) . " product_import jobs (onlyMissing=" . ($this->onlyMissing ? "true" : "false") . ")", Log::INFO, Log::CONTEXT_CRONJOB);
        $jobCount = JobQueue::batchPushProductImport($skus);
      }

      update_option("_ACTIONWEAR_LAST_RESYNCED_PRODUCTS", time());
      return $jobCount;
    }
  }
}