<?php
/**
 * HJCyberX Checkout for Paddle Product Meta Boxes.
 *
 * Adds Paddle-specific fields to the WooCommerce Product Data panel
 * (General tab) so store owners can map each product to a Paddle
 * Price ID and optionally flag it as granting vault access.
 *
 * @package HJCyberX Checkout for Paddle Lite
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class PaddleKonnect_Lite_Product_Meta
 */
class PaddleKonnect_Lite_Product_Meta {

	/**
	 * Constructor — registers hooks.
	 */
	public function __construct() {

		// ── Render fields in the General tab ────.
		add_action(
			'woocommerce_product_options_general_product_data',
			array( $this, 'render_fields' )
		);

		// ── Save field values ───────────────────.
		add_action(
			'woocommerce_process_product_meta',
			array( $this, 'save_fields' )
		);
	}

	/**
	 * Get the meta key for Paddle price ID based on environment.
	 *
	 * @return string
	 */
	public static function get_price_id_key(): string {
		$options    = get_option( 'woocommerce_hjcyberx-checkout-for-paddle-lite_settings', array() );
		$is_sandbox = isset( $options['sandbox_mode'] ) && 'yes' === $options['sandbox_mode'];
		return $is_sandbox ? '_paddlekonnect_sandbox_price_id' : '_paddlekonnect_price_id';
	}

	// ──────────────────────────────────────────.
	// Render.
	// ──────────────────────────────────────────.

	/**
	 * Output Paddle fields inside the WooCommerce Product Data
	 * panel → General tab.
	 */
	public function render_fields(): void {

		$gateway    = PaddleKonnect_Lite_Loader::get_gateway_instance();
		$is_sandbox = $gateway ? ( 'yes' === $gateway->get_option( 'sandbox_mode', 'no' ) ) : false;

		echo '<div class="options_group paddlekonnect-product-fields">';

		// Section heading.
		echo '<p class="form-field paddlekonnect-heading">';
		echo '<strong>' . esc_html__( 'HJCyberX Checkout for Paddle Lite Integration', 'hjcyberx-checkout-for-paddle-lite' ) . '</strong>';
		if ( $gateway ) {
			$env_label = $is_sandbox ? __( 'Sandbox', 'hjcyberx-checkout-for-paddle-lite' ) : __( 'Live', 'hjcyberx-checkout-for-paddle-lite' );
			echo ' <span class="paddlekonnect-env-badge" style="background:#0073aa;color:#fff;padding:2px 8px;border-radius:3px;font-size:11px;margin-left:8px;">' . esc_html( $env_label ) . '</span>';
		}
		echo '</p>';

		// ── Paddle Live Price ID ────────────────────.
		woocommerce_wp_text_input(
			array(
				'id'                => '_paddlekonnect_price_id',
				'label'             => __( 'Paddle Live Price ID', 'hjcyberx-checkout-for-paddle-lite' ),
				'desc_tip'          => true,
				'description'       => __( 'Enter the Paddle Live Price ID (e.g. pri_abc123) for this product in production environment.', 'hjcyberx-checkout-for-paddle-lite' ),
				'placeholder'       => 'pri_...',
				'type'              => 'text',
				'custom_attributes' => ! $is_sandbox ? array( 'style' => 'border-color: #0073aa; background-color: #f0f8ff;' ) : array(),
			)
		);

		// ── Paddle Sandbox Price ID ────────────────────.
		woocommerce_wp_text_input(
			array(
				'id'                => '_paddlekonnect_sandbox_price_id',
				'label'             => __( 'Paddle Sandbox Price ID', 'hjcyberx-checkout-for-paddle-lite' ),
				'desc_tip'          => true,
				'description'       => __( 'Enter the Paddle Sandbox Price ID (e.g. pri_abc123) for this product in sandbox testing environment.', 'hjcyberx-checkout-for-paddle-lite' ),
				'placeholder'       => 'pri_...',
				'type'              => 'text',
				'custom_attributes' => $is_sandbox ? array( 'style' => 'border-color: #0073aa; background-color: #f0f8ff;' ) : array(),
			)
		);

		// Nonce field for save verification.
		wp_nonce_field( 'paddlekonnect_save_product_meta', 'paddlekonnect_product_nonce' );

		echo '</div>';
	}

	// ──────────────────────────────────────────.
	// Save.
	// ──────────────────────────────────────────.

	/**
	 * Save Paddle meta fields when the product is saved.
	 *
	 * @param int $post_id The product (post) ID.
	 */
	public function save_fields( int $post_id ): void {

		// ── Verify nonce ───────────────────────.
		$nonce = isset( $_POST['paddlekonnect_product_nonce'] )
			? sanitize_text_field( wp_unslash( $_POST['paddlekonnect_product_nonce'] ) )
			: '';

		if ( ! wp_verify_nonce( $nonce, 'paddlekonnect_save_product_meta' ) ) {
			return;
		}

		// ── Capability check ───────────────────.
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		// ── Save Price ID ──────────────────────.
		$price_id = isset( $_POST['_paddlekonnect_price_id'] )
			? sanitize_text_field( wp_unslash( $_POST['_paddlekonnect_price_id'] ) )
			: '';
		update_post_meta( $post_id, '_paddlekonnect_price_id', $price_id );

		$sandbox_price_id = isset( $_POST['_paddlekonnect_sandbox_price_id'] )
			? sanitize_text_field( wp_unslash( $_POST['_paddlekonnect_sandbox_price_id'] ) )
			: '';
		update_post_meta( $post_id, '_paddlekonnect_sandbox_price_id', $sandbox_price_id );

		PaddleKonnect_Lite_Logger::debug(
			sprintf(
				'Product #%d meta saved — Price ID: %s, Sandbox Price ID: %s',
				$post_id,
				$price_id ? $price_id : '(none)',
				$sandbox_price_id ? $sandbox_price_id : '(none)'
			)
		);
	}
}
