<?php

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

$parsedArgs = exampleParseArgs($argv);
if (exampleHasFlag($parsedArgs, 'help')) {
    examplePrintUsageAndExit(
        'examples/weatherbot.php',
        'Run a weatherbot example using an inline callable tool.',
        '[--provider=openai|gemini] [--model=MODEL]',
        0
    );
}

exampleBootstrap();

$ai = exampleCreateAi($parsedArgs, 'openai', 'weatherbot_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., San Francisco, CA',
                ),
                'unit' => array(
                    'type' => 'string',
                    'description' => 'The temperature unit to use. Infer this from the user\'s location.',
                    '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\'s the weather in Atlanta right now in Fahrenheit?')
    ->onEvent(function ($event) {
        if ($event->type() === 'response.output_text.delta') {
            echo $event->delta();
        }
    });

$response = $ai->run();
echo rtrim($response->text()) . PHP_EOL;
