<?php
/**
 * Variation-level Terms & Conditions handling.
 *
 * Owns the admin UI inputs, save logic, and frontend data exposure
 * for per-variation T&C URL/text. Extracted from
 * TACPP4_Terms_Conditions_Per_Product to keep the main class focused
 * on checkout/product-page rendering.
 *
 * @package Terms_Conditions_Per_Product
 */

/**
 * Exit if accessed directly.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'TACPP4_Terms_Conditions_Variations' ) ) {

	class TACPP4_Terms_Conditions_Variations {

		protected static $instance = null;

		public static function get_instance() {
			null === self::$instance and self::$instance = new self;

			return self::$instance;
		}

		public function __construct() {
			add_action( 'woocommerce_product_after_variable_attributes',
				array( $this, 'add_terms_and_conditions_input_to_variations' ), 20, 3 );

			add_action( 'woocommerce_save_product_variation',
				array( $this, 'save_terms_field_variations' ), 10, 2 );

			add_filter( 'woocommerce_available_variation',
				array( $this, 'add_terms_to_variable_data' ) );
		}

		/**
		 * Render T&C URL/text inputs on each variation row.
		 *
		 * @param int     $loop           Variation row index.
		 * @param array   $variation_data Variation data array (unused).
		 * @param WP_Post $variation      Variation post.
		 */
		public function add_terms_and_conditions_input_to_variations( $loop, $variation_data, $variation ) {
			woocommerce_wp_text_input( array(
				'id'    => 'variation_terms_url[' . $loop . ']',
				'class' => 'short',
				'label' => __( 'Terms And Conditions URL', 'terms-and-conditions-per-product' ),
				'value' => get_post_meta( $variation->ID, 'variation_terms_url', true )
			) );
			woocommerce_wp_text_input( array(
				'id'    => 'variation_terms_text[' . $loop . ']',
				'class' => 'short',
				'label' => __( 'Terms And Conditions Text (You can use [link][/link] tags to select the specific text to link)', 'terms-and-conditions-per-product' ),
				'value' => get_post_meta( $variation->ID, 'variation_terms_text', true )
			) );
		}

		/**
		 * Persist variation T&C URL/text on save.
		 *
		 * @param int $variation_id Variation ID.
		 * @param int $i            Loop index used for the POST array key.
		 */
		public function save_terms_field_variations( $variation_id, $i ) {

			// WC core verifies its own nonce before firing
			// woocommerce_save_product_variation; still gate on capability.
			if ( ! current_user_can( 'edit_product', $variation_id ) ) {
				return;
			}

			if ( isset( $_POST['variation_terms_url'][ $i ] ) ) {
				update_post_meta(
					$variation_id,
					'variation_terms_url',
					esc_url_raw( wp_unslash( $_POST['variation_terms_url'][ $i ] ) )
				);
			}

			if ( isset( $_POST['variation_terms_text'][ $i ] ) ) {
				update_post_meta(
					$variation_id,
					'variation_terms_text',
					sanitize_text_field( wp_unslash( $_POST['variation_terms_text'][ $i ] ) )
				);
			}
		}

		/**
		 * Expose variation T&C URL/text to frontend variation JSON.
		 *
		 * @param array $variations Variation data.
		 * @return array
		 */
		public function add_terms_to_variable_data( $variations ) {
			$variations['variation_terms_url']  = get_post_meta( $variations['variation_id'], 'variation_terms_url', true );
			$variations['variation_terms_text'] = get_post_meta( $variations['variation_id'], 'variation_terms_text', true );

			return $variations;
		}
	}

	new TACPP4_Terms_Conditions_Variations();
}
