<?php
/**
 * OpenAI Context Implementation.
 *
 * Handles conversation context formatting for OpenAI's GPT models.
 *
 * @package   Agenistrator
 * @since     1.0.0
 * @author    Alex Ruban <alex@maximum.guru>
 * @author    Maximum Software Inc <contact@maximum.software>
 * @license   GPL-3.0-only
 * @link      https://maximum.software/
 *
 * SPDX-FileCopyrightText: 2025-2026 Alex Ruban <alex@maximum.guru>
 * SPDX-FileCopyrightText: 2025-2026 Maximum Software Inc <contact@maximum.software>
 * SPDX-License-Identifier: GPL-3.0-only
 */

namespace Agenistrator;

// Direct access prevention
if ( ! defined( 'ABSPATH' ) ) {
	return;
}

/**
 * Context_OpenAI class.
 */
class Context_OpenAI extends Context {

	/**
	 * Format a Message instance according to OpenAI requirements
	 *
	 * @param Message $message The message instance to format
	 * @return array|null Formatted message or null if invalid
	 * @since     1.0.0
	 */
	protected function format_message( Message $message ) {
		switch ( $message->get_role() ) {
			case 'system':
				return array(
					'role'    => 'system',
					'content' => $message->get_content(),
				);

			case 'user':
				return array(
					'role'    => 'user',
					'content' => $message->get_content(),
				);

			case 'assistant':
				return array(
					'role'    => 'assistant',
					'content' => $message->get_content(),
				);

			case 'tool_call':
				if ( $message instanceof Tool_Call_Message ) {
					$call_id   = $message->get_call_id();
					$arguments = $message->get_arguments();

					// Ensure arguments are JSON string for OpenAI
					// Empty arrays must encode as {} (object) not [] (array)
					if ( is_array( $arguments ) ) {
						$arguments = empty( $arguments ) ? new \stdClass() : $arguments;
					}
					if ( is_array( $arguments ) || is_object( $arguments ) ) {
						$arguments = \wp_json_encode( $arguments, JSON_INVALID_UTF8_SUBSTITUTE );
						if ( false === $arguments ) {
							$arguments = '{"error": "Unable to encode tool arguments"}';
						}
					}

					return array(
						'role'       => 'assistant',
						'content'    => '', // Empty content as required by OpenAI for tool calls
						'tool_calls' => array(
							array(
								'id'       => $call_id,
								'type'     => 'function',
								'function' => array(
									'name'      => $message->get_tool_id(),
									'arguments' => $arguments,
								),
							),
						),
					);
				}
				break;

			case 'tool_result':
				if ( $message instanceof Tool_Result_Message ) {
					$call_id = $message->get_call_id();
					$result  = $message->get_result();

					// Convert result to string if needed
					if ( ! is_string( $result ) ) {
						$result = \wp_json_encode( $result, JSON_INVALID_UTF8_SUBSTITUTE );
						// Fallback if encoding still fails
						if ( false === $result ) {
							$result = '{"success": false, "error": "Unable to encode tool result"}';
						}
					}

					return array(
						'role'         => 'tool',
						'tool_call_id' => $call_id, // This is required by OpenAI
						'content'      => $result,
					);
				}
				break;
		}

		return null;
	}
}
