<?php
/**
 * Smoke-test the AI module against the configured Ollama server.
 * Defaults to localhost:11434; override via OLLAMA_BASE_URL env var.
 *
 * Run from the project root:
 *   php application/module/ai/training/smoke-test.php
 */

namespace {
    require_once __DIR__ . '/../plugins/ollama.php';
    require_once __DIR__ . '/../plugins/chat.php';
    require_once __DIR__ . '/../plugins/embed.php';

    use Nibiru\Module\Ai\Plugins\Chat;
    use Nibiru\Module\Ai\Plugins\Embed;
    use Nibiru\Module\Ai\Plugins\Ollama;

    // Stand-in config. In real use this is loaded from settings/ai.ini via the
    // Registry. Override the URL via the OLLAMA_BASE_URL env var when testing
    // against a non-default Ollama instance.
    $cfg = (object) [
        'ollama_base_url'      => getenv('OLLAMA_BASE_URL') ?: 'http://localhost:11434',
        'ollama_timeout'       => 60,
        'ollama_retries'       => 1,
        'chat_model'           => getenv('OLLAMA_CHAT_MODEL') ?: 'qwen2.5-coder:14b',
        'chat_fallback_model'  => 'qwen2.5-coder:14b',
        'chat_system_prompt'   => 'You are a Nibiru framework expert. Be concise.',
        'chat_temperature'     => 0.3,
        'chat_max_tokens'      => 200,
        'embed_model'          => getenv('OLLAMA_EMBED_MODEL') ?: 'nomic-embed-text',
    ];

    echo "=== Ollama at ", $cfg->ollama_base_url, " ===\n";
    $ollama = new Ollama($cfg);
    try {
        $models = $ollama->listModels();
        $names = array_column($models['models'] ?? [], 'name');
        echo "  found ", count($names), " models\n";
        echo "  qwen2.5-coder:14b: ",
            in_array('qwen2.5-coder:14b', $names, true) ? "yes" : "NO",
            "\n";
    } catch (\Throwable $e) {
        echo "  FAILED: ", $e->getMessage(), "\n";
        exit(1);
    }

    echo "\n=== Chat::ask ===\n";
    $chat = new Chat($cfg);
    try {
        $reply = $chat->ask('In one sentence, what does Form::create() do in Nibiru?');
        echo "  reply: ", trim($reply), "\n";
    } catch (\Throwable $e) {
        echo "  FAILED: ", $e->getMessage(), "\n";
    }

    echo "\n=== Multi-turn chat ===\n";
    $chat2 = new Chat($cfg);
    try {
        $chat2->user('Name three Nibiru singletons.');
        $r1 = $chat2->complete();
        echo "  r1: ", trim($r1), "\n";
        $chat2->user('And what does the second one do?');
        $r2 = $chat2->complete();
        echo "  r2: ", trim($r2), "\n";
    } catch (\Throwable $e) {
        echo "  FAILED: ", $e->getMessage(), "\n";
    }

    echo "\n=== Embed::cosine ===\n";
    try {
        $embed = new Embed($cfg);
        $a = $embed->one('controller');
        $b = $embed->one('module');
        $c = $embed->one('hummingbird');
        echo "  dim(controller) = ", count($a), "\n";
        echo "  cos(controller, module)      = ", number_format(Embed::cosine($a, $b), 3), "\n";
        echo "  cos(controller, hummingbird) = ", number_format(Embed::cosine($a, $c), 3), "\n";
    } catch (\Throwable $e) {
        echo "  FAILED: ", $e->getMessage(), "\n";
        echo "  (expected if nomic-embed-text isn't pulled)\n";
    }

    echo "\nSmoke test done.\n";
}
