<?php

use boru\ai\AI;
use boru\ai\Bootstrap;
use boru\ai\Config\Config;

function exampleBootstrap()
{
    /** @var Config $config */
    $config = require __DIR__ . '/config.php';
    Bootstrap::registerDefaults($config);

    return $config;
}

function exampleDefaultModels()
{
    return array(
        'openai' => 'gpt-5.2',
        'gemini' => 'gemini-2.5-pro',
    );
}

function exampleParseArgs(array $argv)
{
    $result = array(
        'flags' => array(),
        'positionals' => array(),
    );

    $count = count($argv);
    for ($i = 1; $i < $count; $i++) {
        $arg = $argv[$i];
        if (!is_string($arg) || $arg === '') {
            continue;
        }

        if (substr($arg, 0, 2) === '--') {
            $option = substr($arg, 2);
            if ($option === '') {
                continue;
            }

            $parts = explode('=', $option, 2);
            $name = $parts[0];
            $value = true;

            if (count($parts) === 2) {
                $value = $parts[1];
            } elseif (isset($argv[$i + 1]) && is_string($argv[$i + 1]) && substr($argv[$i + 1], 0, 2) !== '--') {
                $value = $argv[$i + 1];
                $i++;
            }

            $result['flags'][$name] = $value;
            continue;
        }

        $result['positionals'][] = $arg;
    }

    return $result;
}

function exampleFlag(array $parsedArgs, $name, $default = null)
{
    if (isset($parsedArgs['flags'][$name])) {
        return $parsedArgs['flags'][$name];
    }

    return $default;
}

function exampleHasFlag(array $parsedArgs, $name)
{
    return isset($parsedArgs['flags'][$name]);
}

function exampleResolveProvider(array $parsedArgs, $defaultProvider)
{
    $provider = exampleFlag($parsedArgs, 'provider', $defaultProvider);
    if (!is_string($provider) || trim($provider) === '') {
        fwrite(STDERR, "Provider must be a non-empty string.\n");
        exit(1);
    }

    $provider = strtolower(trim($provider));
    $models = exampleDefaultModels();
    if (!isset($models[$provider])) {
        fwrite(STDERR, 'Unsupported provider: ' . $provider . PHP_EOL);
        fwrite(STDERR, 'Supported providers: ' . implode(', ', array_keys($models)) . PHP_EOL);
        exit(1);
    }

    return $provider;
}

function exampleResolveModel(array $parsedArgs, $provider)
{
    $model = exampleFlag($parsedArgs, 'model');
    if (is_string($model) && trim($model) !== '') {
        return trim($model);
    }

    $models = exampleDefaultModels();
    return $models[$provider];
}

function exampleCreateAi(array $parsedArgs, $defaultProvider, $reference)
{
    $provider = exampleResolveProvider($parsedArgs, $defaultProvider);
    $model = exampleResolveModel($parsedArgs, $provider);

    $ai = AI::provider($provider)
        ->model($model);

    if ($reference !== null && $reference !== '') {
        $ai->reference($reference . '_' . $provider);
    }

    return $ai;
}

function exampleUsageHeader($scriptName, $description, $extraUsageLines)
{
    $lines = array();
    $lines[] = $description;
    $lines[] = '';
    $lines[] = 'Usage:';
    $lines[] = '  php ' . $scriptName . ' ' . $extraUsageLines;
    $lines[] = '';
    $lines[] = 'Options:';
    $lines[] = '  --provider=openai|gemini   Provider to use (default: openai)';
    $lines[] = '  --model=MODEL             Override the default model for the provider';
    $lines[] = '  --help                    Show this help message';

    return implode(PHP_EOL, $lines) . PHP_EOL;
}

function examplePrintUsageAndExit($scriptName, $description, $extraUsageLines, $exitCode)
{
    $stream = $exitCode === 0 ? STDOUT : STDERR;
    fwrite($stream, exampleUsageHeader($scriptName, $description, $extraUsageLines));
    exit($exitCode);
}
