<?php

use boru\ai\AI;
use boru\ai\Stream\Listener\OutputBufferListener;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/example_helpers.php';

$parsedArgs = exampleParseArgs($argv);
if (exampleHasFlag($parsedArgs, 'help')) {
    examplePrintUsageAndExit(
        'examples/simple.php',
        'Run a simple streaming chat example.',
        '[--provider=openai|gemini] [--model=MODEL]',
        0
    );
}

exampleBootstrap();

$ai = exampleCreateAi($parsedArgs, 'openai', 'simple_example')
    ->addMessage('This is a simple test message to verify basic chat functionality of this library. Please respond with a short confirmation plus enough text to demonstrate streaming output clearly.');

$outputBuffer = new OutputBufferListener();
$outputBuffer->onDelta(function ($event, $delta) {
    echo $delta;
});
$outputBuffer->onDone(function ($event, $text) {
    echo PHP_EOL . '[text complete]' . PHP_EOL;
});

$ai->addEventListener($outputBuffer);

echo 'Starting streaming response...' . PHP_EOL;
$response = $ai->run();
echo 'End of response.' . PHP_EOL . PHP_EOL . 'Final text:' . PHP_EOL;
echo rtrim($response->text()) . PHP_EOL;
