<?php
/**
 * @file    includes/metaboxes/book-header/class-mbs-book-header-woocommerce.php
 * Book Header child panel — WooCommerce product link (PRO only).
 * @package MyBookShowroom
 * @since   1.1.0
 */

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

class MBS_Book_Header_Woocommerce { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	public function __construct() {
		add_action( 'mbs_book_header_render_woocommerce', array( $this, 'render' ) );
		add_action( 'mbs_book_header_save_woocommerce',   array( $this, 'save' ), 10, 2 );
	}

	/**
	 * Render the "Sell via WooCommerce" toggle row inside the Book Header metabox.
	 *
	 * PRO only. FREE tier: HTML comment only — no visible element.
	 *
	 * @param \WP_Post $post Current post.
	 * @return void
	 */
	public function render( \WP_Post $post ): void {
		if ( ! mbs_is_pro() ) {
			echo '<!-- MBS: WooCommerce integration requires PRO license -->';
			return;
		}

		$wc_enabled    = (bool) get_post_meta( $post->ID, 'mbs_wc_enabled', true );
		$wc_product_id = (int) get_post_meta( $post->ID, 'mbs_wc_product_id', true );
		?>
		<tr class="mbs-field-row mbs-wc-toggle-row">
			<th scope="row">
				<label for="mbs_wc_enabled">
					<?php esc_html_e( 'Sell via WooCommerce', 'my-book-showroom' ); ?>
				</label>
			</th>
			<td>
				<input type="checkbox"
				       id="mbs_wc_enabled"
				       name="mbs_wc_enabled"
				       value="1"
				       <?php checked( $wc_enabled, true ); ?>>
				<label for="mbs_wc_enabled">
					<?php esc_html_e( 'Enable WooCommerce add-to-cart for this book', 'my-book-showroom' ); ?>
				</label>
				<?php if ( $wc_product_id > 0 ) : ?>
					<p class="description">
						<?php
						printf(
							/* translators: %d: WooCommerce product ID */
							esc_html__( 'Linked WooCommerce product ID: %d', 'my-book-showroom' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- esc_html__() escapes format string; integer ID is safe with %d
							$wc_product_id // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- integer WooCommerce product ID
						);
						?>
					</p>
					<?php if ( $wc_enabled ) : ?>
					<p class="description" style="color:#2e7d32;font-weight:600;margin-top:4px;">
						&#10003; <?php esc_html_e( 'Ready for in-person sales (FooSales, Square, OpenPOS)', 'my-book-showroom' ); ?>
					</p>
					<?php endif; ?>
				<?php endif; ?>
			</td>
		</tr>
		<?php
	}

	/**
	 * Save the WooCommerce toggle and create/link a WC product when enabled.
	 *
	 * @param int      $post_id Current post ID.
	 * @param \WP_Post $post    Current post.
	 * @return void
	 */
	public function save( int $post_id, \WP_Post $post ): void {
		if ( ! mbs_is_pro() ) {
			return;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce checked in MBS_Metabox_Book_Header::save()
		$enabled = isset( $_POST['mbs_wc_enabled'] ) && '1' === $_POST['mbs_wc_enabled'];

		update_post_meta( $post_id, 'mbs_wc_enabled', $enabled ? '1' : '0' );

		if ( $enabled ) {
			// create_or_link_product() is idempotent — safe to call on every save.
			MBS_WooCommerce_Bridge::create_or_link_product( $post_id );
		}
	}
}
