<?php
/**
 * Product Meta Box Integration for Square Sync.
 *
 * @package FunnelKit_Square
 */

namespace FKWCSQ\Includes\Integrations;

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

/**
 * Product Meta Box Class.
 */
class Product_Meta_Box {

	/**
	 * Initialize the integration.
	 */
	public static function init() {
		add_action( 'woocommerce_product_options_general_product_data', array( __CLASS__, 'add_square_sync_field' ) );
		add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'save_square_sync_field' ) );
		add_action( 'woocommerce_product_quick_edit_end', array( __CLASS__, 'add_quick_edit_field' ) );
		add_action( 'woocommerce_product_quick_edit_save', array( __CLASS__, 'save_quick_edit_field' ) );
		add_action( 'woocommerce_product_bulk_edit_end', array( __CLASS__, 'add_bulk_edit_field' ) );
		add_action( 'woocommerce_product_bulk_edit_save', array( __CLASS__, 'save_bulk_edit_field' ) );
	}

	/**
	 * Add Square sync field to product meta box.
	 */
	public static function add_square_sync_field() {
		global $product_object;

		if ( ! $product_object instanceof \WC_Product ) {
			return;
		}

		// Don't show fields if Square is not configured.
		if ( ! \FKWCSQ\Square::is_configured() ) {
			return;
		}

		?>
		<div class="fkwcsq-sync-with-square options_group show_if_simple show_if_variable">
			<?php
			$sync_with_square = get_post_meta( $product_object->get_id(), '_wc_square_synced', true );

			woocommerce_wp_checkbox(
				array(
					'id'          => '_wc_square_synced',
					'label'       => __( 'Sync with Square', 'funnelkit-payment-gateway-square-for-woocommerce' ),
					'description' => __( 'Send product data to Square', 'funnelkit-payment-gateway-square-for-woocommerce' ),
					'value'       => $sync_with_square,
					'cbvalue'     => 'yes',
					'default'     => 'no',
				)
			);
			?>
		</div>
		<?php
	}

	/**
	 * Save Square sync field.
	 *
	 * @param int $post_id Product ID.
	 */
	public static function save_square_sync_field( $post_id ) {
		// WooCommerce core handles nonce verification for this hook.
		// Check user permissions
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		$sync_with_square = isset( $_POST['_wc_square_synced'] ) ? 'yes' : 'no';    //phpcs:ignore WordPress.Security.NonceVerification.Missing -- WooCommerce handles nonce verification
		update_post_meta( $post_id, '_wc_square_synced', $sync_with_square );

		// Auto-sync to Square if enabled.
		if ( 'yes' === $sync_with_square ) {
			do_action( 'fkwcsq_auto_sync_product_to_square', $post_id );
		}
	}

	/**
	 * Add field to quick edit.
	 */
	public static function add_quick_edit_field() {
		?>
		<label class="alignleft">
			<input type="checkbox" name="_wc_square_synced" value="yes" />
			<span class="checkbox-title"><?php esc_html_e( 'Sync with Square', 'funnelkit-payment-gateway-square-for-woocommerce' ); ?></span>
		</label>
		<?php
	}

	/**
	 * Save quick edit field.
	 *
	 * @param WC_Product $product Product object.
	 */
	public static function save_quick_edit_field( $product ) {
		// WooCommerce core handles nonce verification for this hook.
		// Check user permissions
		if ( ! current_user_can( 'edit_product', $product->get_id() ) ) {
			return;
		}

		$sync_with_square = isset( $_REQUEST['_wc_square_synced'] ) ? 'yes' : 'no'; //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- WooCommerce handles nonce verification
		update_post_meta( $product->get_id(), '_wc_square_synced', $sync_with_square );

		// Auto-sync to Square if enabled.
		if ( 'yes' === $sync_with_square ) {
			do_action( 'fkwcsq_auto_sync_product_to_square', $product->get_id() );
		}
	}

	/**
	 * Add field to bulk edit.
	 */
	public static function add_bulk_edit_field() {
		?>
		<div class="inline-edit-group wp-clearfix">
			<label class="alignleft">
				<span class="title"><?php esc_html_e( 'Sync with Square', 'funnelkit-payment-gateway-square-for-woocommerce' ); ?></span>
				<select name="_wc_square_synced">
					<option value=""><?php esc_html_e( '— No Change —', 'funnelkit-payment-gateway-square-for-woocommerce' ); ?></option>
					<option value="yes"><?php esc_html_e( 'Yes', 'funnelkit-payment-gateway-square-for-woocommerce' ); ?></option>
					<option value="no"><?php esc_html_e( 'No', 'funnelkit-payment-gateway-square-for-woocommerce' ); ?></option>
				</select>
			</label>
		</div>
		<?php
	}

	/**
	 * Save bulk edit field.
	 *
	 * @param WC_Product $product Product object.
	 */
	public static function save_bulk_edit_field( $product ) {
		// WooCommerce core handles nonce verification for this hook.
		// Check user permissions
		if ( ! current_user_can( 'edit_product', $product->get_id() ) ) {
			return;
		}

		if ( isset( $_REQUEST['_wc_square_synced'] ) && '' !== $_REQUEST['_wc_square_synced'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- WooCommerce handles nonce verification
			$sync_with_square = sanitize_text_field( wp_unslash( $_REQUEST['_wc_square_synced'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- WooCommerce handles nonce verification
		}

		if ( isset( $sync_with_square ) ) {
			update_post_meta( $product->get_id(), '_wc_square_synced', $sync_with_square );

			// Auto-sync to Square if enabled.
			if ( 'yes' === $sync_with_square ) {
				do_action( 'fkwcsq_auto_sync_product_to_square', $product->get_id() );
			}
		}
	}

	/**
	 * Check if product should sync with Square.
	 *
	 * @param int $product_id Product ID.
	 * @return bool
	 */
	public static function should_sync_with_square( $product_id ) {
		return 'yes' === get_post_meta( $product_id, '_wc_square_synced', true );
	}
}
