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

namespace Askany\Providers;

/**
 * DeepSeek provider class.
 */
class DeepSeekProvider extends AbstractProvider {
	/**
	 * {@inheritdoc}
	 */
	public function get_provider_name(): string {
		return 'deepseek';
	}

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

	/**
	 * {@inheritdoc}
	 */
	public function get_available_models(): array {
		return array(
			'deepseek-chat' => array(
				'name'               => 'DeepSeek Chat',
				'description'        => 'High-performance chat model',
				'max_tokens'         => 4096,
				'cost_per_1k_tokens' => 0.00014,
				'recommended'        => true,
			),
		);
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_default_model(): string {
		return 'deepseek-chat';
	}

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

	/**
	 * {@inheritdoc}
	 */
	public function send_chat_request( array $messages, array $options = array() ) {
		$defaults = array(
			'model'             => 'deepseek-chat',
			'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( 'DeepSeek 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( 'DeepSeek API Error: ' . $error_msg );
			return false;
		}

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

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