<?php
/**
 * Deepseek Provider Class
 *
 * Integration with Deepseek API
 *
 * @package SmartyPress_AI_Engine
 * @since 1.0.0
 */

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

/**
 * Deepseek Provider class
 */
class SmartyPress_AI_Deepseek_Provider extends SmartyPress_AI_AI_Provider {

	/**
	 * Constructor
	 *
	 * @param string $api_key API key.
	 * @param string $model Model name.
	 */
	public function __construct( $api_key, $model = 'deepseek-v4-flash' ) {
		parent::__construct( $api_key, $model );
		$this->api_endpoint = 'https://api.deepseek.com/v1/chat/completions';
	}

	/**
	 * Generate content using Deepseek
	 *
	 * @param string $prompt The prompt to send to AI.
	 * @param array  $params Additional parameters.
	 * @return array Response with content or error.
	 */
	public function generate_content( $prompt, $params = array() ) {
		$system_prompt = $params['system_prompt'] ?? __( 'You are a helpful assistant for content generation.', 'smartypress-ai-engine' );
		$messages      = $this->build_messages( $system_prompt, $prompt );
		$model         = ! empty( $params['model'] ) ? $params['model'] : $this->model;

		$api_params = array(
			'model'       => $model,
			'messages'    => $messages,
			'temperature' => $params['temperature'] ?? 0.7,
			'max_tokens'  => $params['max_tokens'] ?? 2000,
		);

		if ( ! empty( $params['response_format'] ) && is_array( $params['response_format'] ) ) {
			$api_params['response_format'] = $params['response_format'];
		}

		if ( ! empty( $params['thinking'] ) && is_array( $params['thinking'] ) ) {
			$api_params['thinking'] = $params['thinking'];
		} elseif ( $this->should_disable_thinking_for_json( $model, $api_params ) ) {
			// JSON metadata must be returned in message.content; thinking can spend the token budget before final output.
			$api_params['thinking'] = array( 'type' => 'disabled' );
		}

		if ( ! empty( $params['reasoning_effort'] ) ) {
			$api_params['reasoning_effort'] = sanitize_text_field( $params['reasoning_effort'] );
		}

		if ( ! empty( $params['request_timeout'] ) ) {
			$api_params['request_timeout'] = absint( $params['request_timeout'] );
		}

		return $this->make_request( $messages, $api_params );
	}

	/**
	 * Determine whether DeepSeek V4 thinking mode should be disabled for JSON output.
	 *
	 * @param string $model      Model name.
	 * @param array  $api_params Request parameters.
	 * @return bool
	 */
	private function should_disable_thinking_for_json( $model, $api_params ) {
		$json_mode = isset( $api_params['response_format']['type'] ) && 'json_object' === $api_params['response_format']['type'];

		if ( ! $json_mode ) {
			return false;
		}

		return in_array(
			$model,
			array( 'deepseek-v4-flash', 'deepseek-v4-pro' ),
			true
		);
	}

	/**
	 * Validate API key
	 *
	 * @return bool True if valid, false otherwise.
	 */
	public function validate_api_key() {
		$messages = $this->build_messages(
			__( 'You are a helpful assistant.', 'smartypress-ai-engine' ),
			__( 'Say "Hello"', 'smartypress-ai-engine' )
		);

		$result = $this->make_request( $messages, array( 'max_tokens' => 10 ) );

		return $result['success'] ?? false;
	}

	/**
	 * Get available models
	 *
	 * @return array List of available models.
	 */
	public function get_available_models() {
		return SmartyPress_AI_Model_Map::get_text_models( 'deepseek' );
	}
}

