<?php
namespace AI_BotKit\Integration;

/**
 * Enrich product post content with WooCommerce metadata.
 *
 * @package AI_BotKit\Integration
 */
class Product_Content_Enricher {

    /**
     * Register the content enrichment filter.
     */
    public static function register(): void {
        add_filter( 'ai_botkit_enrich_post_content', [ self::class, 'enrich_post_content' ], 10, 3 );
    }

    /**
     * Append product metadata to post content.
     *
     * @param string $content Existing content.
     * @param int    $post_id Post ID.
     * @param string $post_type Post type.
     * @return string Enriched content.
     */
    public static function enrich_post_content( string $content, int $post_id, string $post_type ): string {
        if ( $post_type !== 'product' || ! class_exists( 'WooCommerce' ) ) {
            return $content;
        }

        $product = wc_get_product( $post_id );
        if ( ! $product ) {
            return $content;
        }

        $enrichment = [];

        if ( $product->is_type( 'variable' ) ) {
            $variations = self::get_variable_product_variations( $product );
            if ( ! empty( $variations ) ) {
                $enrichment[] = 'Variations: ' . implode( '; ', $variations );
            }
        } else {
            $price = $product->get_price();
            if ( $price ) {
                $enrichment[] = 'Price: ' . self::format_price( $price );
            }
        }

        $categories = self::get_product_categories( $post_id );
        if ( ! empty( $categories ) ) {
            $enrichment[] = 'Categories: ' . implode( ', ', $categories );
        }

        $tags = self::get_product_tags( $post_id );
        if ( ! empty( $tags ) ) {
            $enrichment[] = 'Tags: ' . implode( ', ', $tags );
        }

        if ( ! empty( $enrichment ) ) {
            $content .= "\n\n" . implode( ' | ', $enrichment );
        }

        return $content;
    }

    /**
     * Get product category names.
     *
     * @param int $post_id Post ID.
     * @return array
     */
    public static function get_product_categories( int $post_id ): array {
        $terms = get_the_terms( $post_id, 'product_cat' );
        if ( is_wp_error( $terms ) || empty( $terms ) ) {
            return [];
        }
        return wp_list_pluck( $terms, 'name' );
    }

    /**
     * Get product tag names.
     *
     * @param int $post_id Post ID.
     * @return array
     */
    public static function get_product_tags( int $post_id ): array {
        $terms = get_the_terms( $post_id, 'product_tag' );
        if ( is_wp_error( $terms ) || empty( $terms ) ) {
            return [];
        }
        return wp_list_pluck( $terms, 'name' );
    }

    /**
     * Get product price string.
     *
     * @param int $post_id Post ID.
     * @return string
     */
    public static function get_product_price( int $post_id ): string {
        $product = wc_get_product( $post_id );
        return $product ? $product->get_price() : '';
    }

    /**
     * Check if a post is a variable product.
     *
     * @param int $post_id Post ID.
     * @return bool
     */
    public static function is_variable_product( int $post_id ): bool {
        $product = wc_get_product( $post_id );
        return $product && $product->is_type( 'variable' );
    }

    /**
     * Get formatted variation details for a variable product.
     *
     * @param \WC_Product_Variable $product Variable product instance.
     * @return array Array of variation strings.
     */
    private static function get_variable_product_variations( \WC_Product_Variable $product ): array {
        $variations = [];
        $children   = $product->get_children();

        foreach ( $children as $variation_id ) {
            $variation = wc_get_product( $variation_id );
            if ( ! $variation || ! $variation->is_type( 'variation' ) ) {
                continue;
            }

            $attributes = $variation->get_attributes();
            $attr_parts = [];
            foreach ( $attributes as $attr_name => $attr_value ) {
                $label = wc_attribute_label( $attr_name, $variation );
                $attr_parts[] = $label . ': ' . $attr_value;
            }

            $price = self::format_price( $variation->get_price() );
            $variations[] = implode( ', ', $attr_parts ) . ' — ' . $price;
        }

        return $variations;
    }

    /**
     * Format a price with the store currency symbol (no HTML entities).
     *
     * @param string|float $price Raw price.
     * @return string Formatted price with decoded currency symbol.
     */
    private static function format_price( $price ): string {
        $formatted = wp_strip_all_tags( wc_price( $price ) );
        return html_entity_decode( $formatted, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
    }
}
