<?php

namespace Limb_Chatbot\Includes\AI_Providers\Open_Router\Services;

use Limb_Chatbot\Includes\Data_Objects\AI_Model;
use Limb_Chatbot\Includes\Data_Objects\Message;
use Limb_Chatbot\Includes\Services\Helper;

/**
 * Class Message_Service
 *
 * Prepares message payloads for sending to OpenRouter's Chat Completion API by formatting mixed content types
 * like plain text and image attachments according to the capabilities of the selected model.
 *
 * @package Limb_Chatbot\Includes\AI_Providers\Open_Router\Services
 * @since 1.0.14
 */
class Message_Service {

	/**
	 * Transforms a message object into a structured array suitable for OpenRouter API requests.
	 *
	 * Attachment content is ignored; only supported text-based content is sent to the provider.
	 *
	 * @param Message   $message The message to prepare.
	 * @param AI_Model  $model   The AI model in use.
	 *
	 * @return array Returns an array with the message `role` and `content` prepared for OpenRouter.
	 * @since 1.0.14
	 */
	public function prepare_message( Message $message, AI_Model $model ) {
		$content = Helper::maybe_json_decode( $message->get_content() );
		if ( is_array( $content ) ) {
			$has_wc_context_text = false;
			foreach ( $content as $index => &$part ) {
				$part_type = $part['type'] ?? '';
				if (
					$part_type === Message::CONTENT_TYPE_PARAMETER
					|| $part_type === Message::CONTENT_TYPE_PARAMETER_VALUE
					|| $part_type === 'wc_products'
					|| $part_type === 'wc_product'
					|| $part_type === 'wc_cart'
					|| $part_type === 'wc_choices'
					|| $part_type === 'wc_products_variants'
					|| $part_type === 'wc_checkout_redirect'
					|| $part_type === 'wc_browse_more'
					|| $part_type === 'wc_product_confirmation'
					|| $part_type === 'wc_add_to_cart'
				) {
					$filtered_text = apply_filters(
						"limb_chatbot_message_content_part_{$part_type}",
						$part['text']['value'] ?? '',
						$part
					);
					$filtered_text = is_string( $filtered_text ) ? trim( $filtered_text ) : '';
					if ( '' === $filtered_text ) {
						unset( $content[ $index ] );
						continue;
					}
					$part = [
						'type'         => 'text',
						'text'         => [ 'value' => $filtered_text ],
						'__wc_context' => true,
					];
					$has_wc_context_text = true;
				}

				if ( $part['type'] === 'text' ) {
					if ( count( $content ) === 1 ) {
						$content = $part['text']['value'];
						break;
					} else {
						$part['text'] = $part['text']['value'];
					}
				} elseif ( $part['type'] === 'attachment' ) {
					unset( $content[ $index ] );
					continue;
				} elseif ( $part['type'] === Message::CONTENT_TYPE_ACTION_SUBMISSION ) {
					$part['type'] = 'text';
					$part['text'] = $part[ Message::CONTENT_TYPE_ACTION_SUBMISSION ]['message'] ?? 'Action submitted';
					unset( $part[ Message::CONTENT_TYPE_ACTION_SUBMISSION ] );
				} elseif ( $part['type'] === Message::CONTENT_TYPE_ACTION_CANCELLATION ) {
					$part['type'] = 'text';
					$part['text'] = ! empty( $part[ Message::CONTENT_TYPE_ACTION_CANCELLATION ]['text'] )
						? $part[ Message::CONTENT_TYPE_ACTION_CANCELLATION ]['text']
						: __( 'Cancel submission of this action', 'limb-chatbot' );
					unset( $part[ Message::CONTENT_TYPE_ACTION_CANCELLATION ] );
				}
			}
			unset( $part );

			if ( is_array( $content ) && $has_wc_context_text ) {
				$content = array_values(
					array_filter(
						$content,
						static function ( $part ) {
							return is_array( $part ) && ! empty( $part['__wc_context'] );
						}
					)
				);
			}

			if ( is_array( $content ) ) {
				foreach ( $content as &$part ) {
					if ( is_array( $part ) ) {
						unset( $part['__wc_context'] );
					}
				}
				unset( $part );

				if ( 1 === count( $content ) && isset( $content[0]['type'] ) && 'text' === $content[0]['type'] ) {
					$text_value = $content[0]['text'] ?? '';
					if ( is_array( $text_value ) ) {
						$text_value = $text_value['value'] ?? '';
					}
					$content = is_string( $text_value ) ? $text_value : '';
				}
			}
		}

		return array(
			'role'    => $message->get_role(),
			'content' => $content,
		);
	}
}
