<?php

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

$parsedArgs = exampleParseArgs($argv);
if (exampleHasFlag($parsedArgs, 'help')) {
    examplePrintUsageAndExit(
        'examples/chat_callbacks.php',
        'Run a chat-style example using the fluent stream, tool, and turn callback helpers.',
        '[--provider=openai|gemini] [--model=MODEL]',
        0
    );
}

exampleBootstrap();

$ai = exampleCreateAi($parsedArgs, 'gemini', 'chat_callbacks_example')
    ->setToolContextValue('temp', 69)
    ->defineTool(
        'get_current_temperature',
        'Get the current temperature for a specific location',
        array(
            'type' => 'object',
            'properties' => array(
                'location' => array(
                    'type' => 'string',
                    'description' => 'The city and state, e.g., Atlanta, GA',
                ),
                'unit' => array(
                    'type' => 'string',
                    'description' => 'The temperature unit to use.',
                    'enum' => array('Fahrenheit', 'Celsius'),
                ),
            ),
            'required' => array('location', 'unit'),
        ),
        function (array $arguments, $context) {
            $temp = $context ? $context->get('temp', 69) : 69;

            return json_encode(array(
                'location' => isset($arguments['location']) ? $arguments['location'] : null,
                'unit' => isset($arguments['unit']) ? $arguments['unit'] : null,
                'temperature' => $temp,
            ));
        }
    )
    ->addMessage('What is the weather in Atlanta right now in Fahrenheit? Give me the temperature first, then a short friendly explanation.')
    ->onStreamDelta(function ($delta) {
        if (!is_string($delta) || $delta === '') {
            return;
        }

        echo $delta;
    })
    ->onStreamComplete(function ($text) {
        echo PHP_EOL . '[stream complete]' . PHP_EOL;
    })
    ->onToolStart(function ($toolCallId, $toolName, $arguments) {
        echo PHP_EOL . '[tool:start] ' . $toolName;
        if (is_array($arguments)) {
            echo ' ' . json_encode($arguments);
        }
        echo PHP_EOL;
    })
    ->onToolComplete(function ($toolCallId, $toolName, $output) {
        echo '[tool:done] ' . $toolName;
        if ($output !== null && $output !== '') {
            echo ' => ' . (is_string($output) ? $output : json_encode($output));
        }
        echo PHP_EOL;
    })
    ->onToolFailed(function ($toolCallId, $toolName, $output) {
        echo '[tool:failed] ' . $toolName;
        if ($output !== null && $output !== '') {
            echo ' => ' . (is_string($output) ? $output : json_encode($output));
        }
        echo PHP_EOL;
    })
    ->onTurnComplete(function ($conversationId, $turnNumber) {
        echo '[turn:complete] conversation=' . $conversationId . ' turn=' . $turnNumber . PHP_EOL;
    })
    ->onTurnFailed(function ($conversationId, $turnNumber, $error) {
        echo '[turn:failed] conversation=' . $conversationId . ' turn=' . $turnNumber;
        if (is_array($error) && isset($error['message'])) {
            echo ' error=' . $error['message'];
        }
        echo PHP_EOL;
    });

echo 'Starting chat callback example...' . PHP_EOL;
$response = $ai->run();
echo PHP_EOL . 'Final text:' . PHP_EOL;
echo rtrim($response->text()) . PHP_EOL;
