<?php
/**
 * WooCommerce product generation extension.
 *
 * @package SmartyPress_AI_Engine
 */

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

/**
 * Adds WooCommerce-specific generation rules and metadata.
 */
class SmartyPress_AI_WooCommerce_Product_Extension implements SmartyPress_AI_Post_Type_Extension_Interface {

	/**
	 * Check support.
	 *
	 * @param string $post_type Post type.
	 * @param array  $settings Generation settings.
	 * @return bool
	 */
	public function supports( $post_type, array $settings ) {
		return 'product' === sanitize_key( $post_type ) && post_type_exists( 'product' );
	}

	/**
	 * Get product generation rules.
	 *
	 * @param array $settings Generation settings.
	 * @return array
	 */
	public function get_generation_rules( array $settings ) {
		return array(
			__( 'WooCommerce product rules:', 'smartypress-ai-engine' ),
			__( '- Generate a real product name, not a marketing headline.', 'smartypress-ai-engine' ),
			__( '- Keep product identity in the title: brand, model, product type, and provided attributes.', 'smartypress-ai-engine' ),
			__( '- Put benefits, buyer intent, and SEO wording into description, excerpt, and SEO metadata.', 'smartypress-ai-engine' ),
			__( '- Do not invent product attributes that are not present in the topic or row context.', 'smartypress-ai-engine' ),
		);
	}

	/**
	 * Get product-focused SEO rules.
	 *
	 * @param array $settings Generation settings.
	 * @return array
	 */
	public function get_seo_rules( array $settings ) {
		return array(
			'Choose product-focused keywords that match buyer intent.',
			'Use the real product type, brand, model, and provided attributes when available.',
			'Write the meta description as a product search snippet, not a generic article summary.',
			'Do not invent prices, stock status, warranty, shipping, or unsupported product claims.',
		);
	}

	/**
	 * Build product row context.
	 *
	 * @param array $item Queue item.
	 * @param array $settings Generation settings.
	 * @return string
	 */
	public function build_item_context( array $item, array $settings ) {
		$parts = $this->get_generation_rules( $settings );

		if ( ! empty( $item['sku'] ) ) {
			$parts[] = __( 'SKU:', 'smartypress-ai-engine' ) . ' ' . sanitize_text_field( $item['sku'] );
		}

		if ( ! empty( $item['product_attributes'] ) ) {
			$parts[] = __( 'Product attributes:', 'smartypress-ai-engine' ) . ' ' . sanitize_textarea_field( $item['product_attributes'] );
		}

		return implode( "\n", $parts );
	}

	/**
	 * Prepare product post data.
	 *
	 * @param array $post_data Generated post data.
	 * @param array $item Queue item.
	 * @param array $settings Generation settings.
	 * @return array
	 */
	public function prepare_post_data( array $post_data, array $item, array $settings ) {
		$post_data['product_meta'] = array(
			'sku'        => sanitize_text_field( $item['sku'] ?? '' ),
			'attributes' => sanitize_textarea_field( $item['product_attributes'] ?? '' ),
		);

		return $post_data;
	}

	/**
	 * Apply product metadata.
	 *
	 * Core product defaults are still written by Post_Persistence_Service for compatibility.
	 *
	 * @param int   $post_id Post ID.
	 * @param array $post_data Generated post data.
	 * @param array $item Queue item.
	 * @param array $settings Generation settings.
	 * @return void
	 */
	public function apply_metadata( $post_id, array $post_data, array $item, array $settings ) {
		$post_id = absint( $post_id );
		if ( ! $post_id ) {
			return;
		}

		$product_meta = is_array( $post_data['product_meta'] ?? null ) ? $post_data['product_meta'] : array();
		if ( ! empty( $product_meta['sku'] ) ) {
			update_post_meta( $post_id, '_sku', sanitize_text_field( $product_meta['sku'] ) );
		}

		if ( ! empty( $product_meta['attributes'] ) ) {
			update_post_meta( $post_id, '_smartypress_ai_product_attributes', sanitize_textarea_field( $product_meta['attributes'] ) );
		}
	}
}
