<?php
/**
 * OpenAI Provider
 *
 * Handles communication with OpenAI API.
 *
 * @package Askany
 * @since 1.0.0
 */

namespace Askany\Providers;

/**
 * OpenAI provider class.
 */
class OpenAIProvider extends AbstractProvider {
	/**
	 * {@inheritdoc}
	 */
	public function get_provider_name(): string {
		return 'openai';
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_provider_display_name(): string {
		return 'OpenAI';
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_available_models(): array {
		return array(
			'gpt-4o-mini'  => array(
				'name'               => 'GPT-4o Mini',
				'description'        => 'Fast and cost-effective for most tasks',
				'max_tokens'         => 16384,
				'cost_per_1k_tokens' => 0.00015,
				'recommended'        => true,
			),
			'gpt-4o'       => array(
				'name'               => 'GPT-4o',
				'description'        => 'Most capable general model for text and vision',
				'max_tokens'         => 128000,
				'cost_per_1k_tokens' => 0.005,
			),
			'gpt-4.1'      => array(
				'name'               => 'GPT-4.1',
				'description'        => 'Latest general-purpose model (text + vision) in OpenAI API',
				'max_tokens'         => 1048576,
				'cost_per_1k_tokens' => 0.00,
			),
			'gpt-4.1-mini' => array(
				'name'               => 'GPT-4.1 Mini',
				'description'        => 'Cost-efficient version of GPT-4.1 for simpler tasks',
				'max_tokens'         => 1048576,
				'cost_per_1k_tokens' => 0.00,
			),
			'gpt-4-turbo'  => array(
				'name'               => 'GPT-4 Turbo',
				'description'        => 'Latest GPT-4 Turbo with vision',
				'max_tokens'         => 128000,
				'cost_per_1k_tokens' => 0.01,
			),
			'gpt-4'        => array(
				'name'               => 'GPT-4',
				'description'        => 'Standard GPT-4 model',
				'max_tokens'         => 8192,
				'cost_per_1k_tokens' => 0.03,
			),
			'gpt-3.5-turbo' => array(
				'name'               => 'GPT-3.5 Turbo',
				'description'        => 'Fast and affordable',
				'max_tokens'         => 16385,
				'cost_per_1k_tokens' => 0.0005,
			),
			'o1-preview'   => array(
				'name'               => 'O1 Preview',
				'description'        => 'Reasoning model for complex tasks',
				'max_tokens'         => 128000,
				'cost_per_1k_tokens' => 0.015,
			),
			'o1-mini'      => array(
				'name'               => 'O1 Mini',
				'description'        => 'Faster reasoning model',
				'max_tokens'         => 128000,
				'cost_per_1k_tokens' => 0.003,
			),
			'o4-mini'      => array(
				'name'               => 'O4-Mini',
				'description'        => 'Reasoning-specialized model from OpenAI (text + vision)',
				'max_tokens'         => 128000,
				'cost_per_1k_tokens' => 0.00,
			),
		);
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_default_model(): string {
		return 'gpt-4o-mini';
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_default_endpoint(): string {
		return 'https://api.openai.com/v1/chat/completions';
	}

	/**
	 * {@inheritdoc}
	 */
	public function send_chat_request( array $messages, array $options = array() ) {
		$defaults = array(
			'model'             => 'gpt-4o-mini',
			'temperature'       => 0.3,
			'max_tokens'        => 800,
			'frequency_penalty' => 0.3,
		);

		$params = array_merge( $defaults, $options );
		$params['messages'] = $messages;

		$headers = array(
			'Authorization' => 'Bearer ' . $this->api_key,
		);

		$data = $this->make_request( $this->api_url, $params, $headers );

		if ( ! $data ) {
			// error_log( 'OpenAI API: No data returned from request' );
			return false;
		}

		// Check for error in response
		if ( isset( $data['error'] ) ) {
			$error_msg = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Unknown error';
			// error_log( 'OpenAI API Error: ' . $error_msg );
			return false;
		}

		if ( ! isset( $data['choices'][0]['message']['content'] ) ) {
			// error_log( 'OpenAI API: Unexpected response structure' );
			return false;
		}

		return array(
			'text' => $data['choices'][0]['message']['content'],
		);
	}
}
