<?php

namespace Breakdance\AI;

use CurlHandle;
use Orhanerday\OpenAi\OpenAi;

add_action('breakdance_loaded', function () {
    \Breakdance\AJAX\register_handler(
        'breakdance_ai_request_completion',
        'Breakdance\AI\requestCompletion',
        'edit',
        false,
        [
            'args' => [
                'messages' => FILTER_UNSAFE_RAW,
                'format' => FILTER_SANITIZE_SPECIAL_CHARS,
            ]
        ]
    );

    \Breakdance\AJAX\register_handler(
        'breakdance_ai_get_suggested_prompts',
        'Breakdance\AI\getSuggestedPrompts',
        'edit',
        false,
        [
            'args' => [
                'prompt' => FILTER_SANITIZE_SPECIAL_CHARS,
            ]
        ]
    );
});

/**
 * @param string $messages
 * @param string $format
 * @return array{data:string}
 * @throws \Exception
 */
function requestCompletion($messages, $format) {
    $response = talkToOpenAi(
        json_decode($messages, true),
        $format
    );

    return $response;
}

/**
 * @param array $messages
 * @param string $format
 * @param int|null $temperature
 * @param bool $stream
 * @return string|array{error:string}|array{data:string}
 * @throws \Exception
 */
function talkToOpenAi($messages, $format, $temperature = null, $stream = false) {
    $key = \Breakdance\APIKeys\getKey('openai');

    if (!$key) {
        return ['error' => 'Please enter an OpenAI API key in the settings page.'];
    }

    $open_ai = new OpenAi($key);

    $defaults = [
        'temperature' => 1.0,
    ];

    $formats = [
        'json' => [
            'temperature' => 1.0,
            'response_format' => [
                'type' => 'json_object'
            ]
        ],
        'css' => [
            'temperature' => 0.2
        ],
    ];

    $opts = array_merge(
        $defaults,
        $formats[$format] ?? [],
        [
            'model' => getModel(),
            'messages' => $messages,
            "stream" => $stream,
        ]
    );

    if ($temperature !== null) {
        $opts['temperature'] = $temperature;
    }

    if ($stream) {
        header('Content-type: text/event-stream');
        header('Cache-Control: no-cache');
        header("X-Accel-Buffering: no");

        $open_ai->chat($opts, function (CurlHandle $ch, $data) {
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

            if ($httpCode !== 200) {
                echo "event: error\n";
                echo "data: " . json_encode($data) . "\n\n";
                ob_flush();
                flush();
                return strlen($data);
            }

            echo $data;
            echo PHP_EOL;
            ob_flush();
            flush();
            return strlen($data);
        });
        exit();
    } else {
        $chat = $open_ai->chat($opts);
        $completion = json_decode($chat);

        if (isset($completion->error)) {
            return ['error' => $completion->error->message];
        }

        $data = $completion->choices[0]->message->content;
        return ['data' => $data];
    }
}

function getSuggestedPrompts($prompt) {
    $key = \Breakdance\APIKeys\getKey('openai');

    if (!$key) {
        return ['error' => 'Please enter an OpenAI API key in the settings page.'];
    }

    $messages = [
        [
            'content' => getSuggestionsPrompt(),
            'role' => 'system'
        ],
        [
            'content' => "Element name: $prompt",
            'role' => 'user'
        ]
    ];

    $open_ai = new OpenAi($key);

    $chat = $open_ai->chat([
        'model' => getModel(),
        'messages' => $messages,
        'max_tokens' => 500,
        "response_format" => [
            'type' => 'json_object'
        ]
    ]);

    $completion = json_decode($chat);

    if (isset($completion->error)) {
        return ['error' => $completion->error->message];
    }

    $data = $completion->choices[0]->message->content;
    return ['data' => json_decode($data)];
}

function streamEndpoint() {
    $messages = getPersistedMessages();
    $format = getPersistedFormat();
    $temperature = getPersistedTemperature();

    talkToOpenAi(
        json_decode($messages, true),
        $format,
        $temperature,
        true
    );
}

add_action('rest_api_init', function () {
    register_rest_route('breakdance/v1', '/ai/stream-completion', [
        'methods' => 'GET',
        'callback' => '\Breakdance\AI\streamEndpoint',
        'permission_callback' => function () {
            return \Breakdance\Permissions\hasMinimumPermission(getAiRole());
        }
    ]);
});
