<?php
/**
 * SNS Activewear Filter Hook
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Hooks
 * @author     miniOrange <info@miniorange.com>
 * @license    MIT/Expat
 * @link       https://plugins.miniorange.com/woocommerce-api-product-sync-with-woocommerce-rest-apis
 */

namespace MOWCPS\Common\Hooks;

use MOWCPS\Common\Constants\Plugin_Filters;
use MOWCPS\Common\Interfaces\Hookable_Interface;
use MOWCPS\Common\Suppliers\Sns_Activewear as Sns_Activewear_Supplier;

if ( ! defined( 'ABSPATH' ) ) {
	die;
}

/**
 * Class Sns_Activewear
 *
 * Handles SNS Activewear filter hook registration
 */
class Sns_Activewear implements Hookable_Interface {

	/**
	 * Initialize the Sns_Activewear filter hooks
	 */
	public function __construct() {
		$this->register_hooks();
	}

	/**
	 * Register WordPress hooks
	 *
	 * @return void
	 */
	public function register_hooks() {
		add_filter( Plugin_Filters::FILTER_API_CALL_ALTERATION_FILTER, array( Sns_Activewear_Supplier::class, 'handle_api_response_cache' ), 10, 1 );
		add_filter( Plugin_Filters::FILTER_DATA_ALTERATION_FILTER, array( Sns_Activewear_Supplier::class, 'store_outbound_specification_data' ), 10, 1 );
		add_filter( Plugin_Filters::FILTER_DATA_ALTERATION_FILTER, array( Sns_Activewear_Supplier::class, 'generate_specification_table' ), 10, 1 );
		add_filter( Plugin_Filters::FILTER_DATA_ALTERATION_FILTER, array( Sns_Activewear_Supplier::class, 'fetch_name_description' ), 10, 1 );
		add_filter( Plugin_Filters::FILTER_DATA_ALTERATION_FILTER, array( Sns_Activewear_Supplier::class, 'get_sub_categories' ), 10, 1 );
		add_filter( Plugin_Filters::FILTER_ADD_SNS_THUMBNAIL_IMAGE, array( Sns_Activewear_Supplier::class, 'add_thumbnail_image' ), 10, 1 );
		add_filter( Plugin_Filters::FILTER_SUPPLIER_PROCESS_COMPLETION, array( Sns_Activewear_Supplier::class, 'handle_batch_completion' ), 10, 1 );
		add_filter( Plugin_Filters::FILTER_ALTER_PRODUCT_SYNC_AUDIT, array( Sns_Activewear_Supplier::class, 'handle_alter_product_sync_audit' ), 10, 1 );
		add_filter( Plugin_Filters::FILTER_PREVIEW_PRODUCTS_FETCH, array( Sns_Activewear_Supplier::class, 'handle_preview_products_fetch' ), 10, 1 );
		add_filter( Plugin_Filters::FILTER_PREVIEW_ATTRIBUTE_MAPPING, array( Sns_Activewear_Supplier::class, 'handle_preview_attribute_mapping' ), 10, 1 );
		add_action( 'mowcps_stop_sync', array( Sns_Activewear_Supplier::class, 'handle_stop_sync' ), 10, 1 );
	}

	/**
	 * Handle export supplier details
	 *
	 * @param string $supplier_table The name of the supplier table.
	 * @param int    $supplier_id The supplier ID.
	 * @return string The supplier details JSON.
	 */
	public static function handle_export_supplier_details( $supplier_table, $supplier_id ) {
		global $wpdb;
		$supplier_details_json = $wpdb->get_var( //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->prepare(
				"SELECT details FROM `{$supplier_table}` WHERE id = %d",
				$supplier_id ?? 0
			)
		);
		return $supplier_details_json;
	}
}
