<?php
/**
 * Provider for WooCommerce Products.
 *
 * @package Replyan_AI
 */

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

class Replyan_AI_Product_Source_Provider implements Replyan_AI_Source_Provider_Interface {
	public function getDocuments(): array {
		if ( ! class_exists( 'WooCommerce' ) ) {
			return array();
		}

		$settings         = Replyan_AI_Settings::get();
		$index_permalinks = ! isset( $settings['index_permalinks'] ) || '0' !== (string) $settings['index_permalinks'];

		$products = wc_get_products( array(
			'status' => 'publish',
			'limit'  => -1,
		) );

		$documents = array();
		foreach ( $products as $product ) {
			$post_id = $product->get_id();
			$url     = $product->get_permalink();
			
			$content_blocks = array();
			
			// Basic Info & Link
			$content_blocks[] = 'Product: ' . $product->get_name();
			if ( $index_permalinks && $url ) {
				$content_blocks[] = 'Link: ' . $url;
			}
			
			// Price
			$price_string = 'Price: ' . wp_strip_all_tags( wc_price( $product->get_price() ) );
			if ( $product->is_on_sale() ) {
				$price_string .= ' (Regular: ' . wp_strip_all_tags( wc_price( $product->get_regular_price() ) ) . ', Sale: ' . wp_strip_all_tags( wc_price( $product->get_sale_price() ) ) . ')';
			}
			$content_blocks[] = $price_string;
			
			// SKU
			if ( $product->get_sku() ) {
				$content_blocks[] = 'SKU: ' . $product->get_sku();
			}
			
			// Stock
			$stock_status = $product->get_stock_status();
			$stock_label = 'In Stock';
			if ( 'outofstock' === $stock_status ) {
				$stock_label = 'Out of Stock';
			} elseif ( 'onbackorder' === $stock_status ) {
				$stock_label = 'On Backorder';
			}
			$stock_string = 'Stock: ' . $stock_label;
			if ( $product->managing_stock() && $product->get_stock_quantity() !== null ) {
				$stock_string .= ' (' . $product->get_stock_quantity() . ' available)';
			}
			$content_blocks[] = $stock_string;
			
			// Weight & Dimensions
			if ( $product->has_weight() ) {
				$content_blocks[] = 'Weight: ' . $product->get_weight() . ' ' . get_option( 'woocommerce_weight_unit' );
			}
			if ( $product->has_dimensions() ) {
				$content_blocks[] = 'Dimensions: ' . $product->get_dimensions();
			}
			
			// Categories
			$categories = wc_get_product_category_list( $post_id, ', ' );
			if ( $categories ) {
				$content_blocks[] = 'Categories: ' . wp_strip_all_tags( $categories );
			}
			
			// Tags
			$tags = wc_get_product_tag_list( $post_id, ', ' );
			if ( $tags ) {
				$content_blocks[] = 'Tags: ' . wp_strip_all_tags( $tags );
			}
			
			// Attributes
			$attributes = $product->get_attributes();
			if ( ! empty( $attributes ) ) {
				$attr_strings = array();
				foreach ( $attributes as $attribute ) {
					$name = wc_attribute_label( $attribute->get_name() );
					if ( $attribute->is_taxonomy() ) {
						$terms = wc_get_product_terms( $post_id, $attribute->get_name(), array( 'fields' => 'names' ) );
						if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
							$attr_strings[] = $name . ': ' . implode( ', ', $terms );
						}
					} else {
						$options = $attribute->get_options();
						if ( ! empty( $options ) ) {
							$attr_strings[] = $name . ': ' . implode( ', ', $options );
						}
					}
				}
				if ( ! empty( $attr_strings ) ) {
					$content_blocks[] = 'Attributes: ' . implode( ' | ', $attr_strings );
				}
			}

			// Short Description
			$short_desc = $product->get_short_description();
			if ( $short_desc ) {
				$content_blocks[] = "\n[Short Description]\n" . wp_strip_all_tags( strip_shortcodes( $short_desc ) );
			}

			// Full Description
			$full_desc = $product->get_description();
			if ( $full_desc ) {
				$content_blocks[] = "\n[Full Description]\n" . wp_strip_all_tags( strip_shortcodes( $full_desc ) );
			}

			$final_content = implode( "\n", $content_blocks );

			$documents[] = array(
				'source_type' => 'product',
				'source_id'   => $post_id,
				'title'       => $product->get_name(),
				'url'         => $product->get_permalink(),
				'content'     => trim( $final_content ),
				'modified_at' => $product->get_date_modified() ? $product->get_date_modified()->date( 'Y-m-d H:i:s' ) : current_time( 'mysql' ),
			);
		}

		return $documents;
	}
}
