<?php

use boru\ai\Bootstrap;
use boru\ai\Config\Config;
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/replay_summarize_history.php',
        'Demonstrate conversation replay using the summarize_history strategy.',
        '[--provider=openai|gemini] [--model=MODEL] [--keep-turns=N] [--summary-max-chars=N]',
        0
    );
}

/** @var Config $config */
$config = exampleBootstrap();
$provider = exampleResolveProvider($parsedArgs, 'gemini');
$model = exampleResolveModel($parsedArgs, $provider);
$keepTurns = (int)exampleFlag($parsedArgs, 'keep-turns', 2);
$summaryMaxChars = (int)exampleFlag($parsedArgs, 'summary-max-chars', 1200);

if ($keepTurns < 0) {
    fwrite(STDERR, "--keep-turns must be zero or a positive integer.\n");
    exit(1);
}

if ($summaryMaxChars <= 0) {
    fwrite(STDERR, "--summary-max-chars must be a positive integer.\n");
    exit(1);
}

$config->provider($provider, array(
    'replay' => array(
        'enabled' => true,
        'strategy' => 'summarize_history',
        'summary' => array(
            'keep_turns' => $keepTurns,
            'max_chars' => $summaryMaxChars,
        ),
    ),
));
Bootstrap::registerDefaults($config);

$reference = 'replay_summarize_history_example_' . $provider;

$prompts = array(
    'My name is Daniel. Please acknowledge this briefly.',
    'I live in Atlanta, Georgia. Please acknowledge this briefly.',
    'I lead engineering for a small product team. Please acknowledge this briefly.',
    'We are evaluating replay strategies because long conversations are getting expensive. Please acknowledge this briefly.',
    'My favorite color is green. Please acknowledge this briefly.',
    'Without guessing beyond what is in memory, summarize what you know about me and mention that replay strategy is summarize_history with keep_turns=' . $keepTurns . ' and summary_max_chars=' . $summaryMaxChars . '.',
);

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

echo 'Provider: ' . $provider . PHP_EOL;
echo 'Model: ' . $model . PHP_EOL;
echo 'Replay strategy: summarize_history' . PHP_EOL;
echo 'Keep recent turns verbatim: ' . $keepTurns . PHP_EOL;
echo 'Summary max chars: ' . $summaryMaxChars . PHP_EOL . PHP_EOL;

foreach ($prompts as $index => $prompt) {
    $ai = exampleCreateAi($parsedArgs, $provider, 'replay_summarize_history_example')
        ->model($model)
        ->reference($reference)
        ->addMessage($prompt)
        ->addEventListener($outputBuffer);

    echo '--- Turn ' . ($index + 1) . ' ---' . PHP_EOL;
    echo 'User: ' . $prompt . PHP_EOL;
    $response = $ai->run();
    echo 'Final text:' . PHP_EOL;
    echo rtrim($response->text()) . PHP_EOL . PHP_EOL;
}
