<?php
namespace boru\ai\Tests\AI;

use boru\ai\AI;
use boru\ai\Runtime\RuntimeContext;
use boru\ai\Runtime\ToolRegistry;
use PHPUnit\Framework\TestCase;

class AIIntegrationTest extends TestCase
{
    protected function setUp(): void
    {
        AI::runtimeContext(new RuntimeContext());
    }

    public function testDefineToolRegistersToolAndAddsSchemaToRequest()
    {
        $ai = new AI();
        $ai->defineTool(
            'get_current_temperature',
            'Get current temperature',
            array(
                'type' => 'object',
                'properties' => array(
                    'location' => array('type' => 'string'),
                ),
            ),
            function () {
                return '69';
            }
        );

        $registry = AI::toolRegistry();
        $this->assertInstanceOf(ToolRegistry::class, $registry);
        $this->assertTrue($registry->has('get_current_temperature'));

        $tools = $ai->request()->tools();
        $this->assertCount(1, $tools);
        $this->assertSame('get_current_temperature', $tools[0]['name']);
    }

    public function testReferenceHelperStoresConversationReference()
    {
        $ai = new AI();
        $ai->reference('weatherbot_ref', 'user-1');

        $reference = $ai->conversationReference();
        $this->assertSame('weatherbot_ref', $reference->externalReference());
        $this->assertSame('user-1', $reference->userId());
    }

    public function testFluentOptionHelpersExposeRequestOptions()
    {
        $ai = new AI();

        $this->assertSame($ai, $ai->option('temperature', 0.2));
        $this->assertSame(0.2, $ai->option('temperature'));

        $this->assertSame($ai, $ai->options(array(
            'store' => false,
            'max_output_tokens' => 500,
        )));
        $this->assertSame(array(
            'store' => false,
            'max_output_tokens' => 500,
        ), $ai->options());
    }

    public function testReasoningHelperBuildsOpenAIReasoningOption()
    {
        $ai = AI::provider('openai')->model('gpt-5.6-sol');

        $this->assertSame($ai, $ai->reasoning('high', array(
            'mode' => 'pro',
            'context' => 'all_turns',
        )));
        $this->assertSame(array(
            'mode' => 'pro',
            'context' => 'all_turns',
            'effort' => 'high',
        ), $ai->reasoning());
    }
}
