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

use boru\ai\AI;
use boru\ai\Tests\Fakes\FakeHttpClient;

class BootstrapProviderIntegrationTest extends IntegrationTestCase
{
    public function testBootstrapOpenAIChatFlowUsesRealClientMapperAndFacade()
    {
        $httpClient = new FakeHttpClient();
        $httpClient->queueResponse($this->jsonResponse(array(
            'id' => 'resp_openai_1',
            'model' => 'gpt-5.1',
            'status' => 'completed',
            'output' => array(
                array(
                    'type' => 'message',
                    'content' => array(
                        array(
                            'type' => 'output_text',
                            'text' => 'OpenAI integrated hello',
                        ),
                    ),
                ),
            ),
        )));

        $this->bootstrapWithHttpClients($httpClient, null);

        $response = AI::provider('openai')
            ->model('gpt-5.1')
            ->instructions('Be concise.')
            ->addMessage('Say hello')
            ->run();

        $this->assertSame('OpenAI integrated hello', $response->text());
        $this->assertSame('POST', $httpClient->requests[0]->method());
        $this->assertSame('https://api.openai.com/v1/responses', $httpClient->requests[0]->url());
        $this->assertSame('gpt-5.1', $httpClient->requests[0]->json()['model']);
        $this->assertSame('Be concise.', $httpClient->requests[0]->json()['instructions']);
        $this->assertSame('Say hello', $httpClient->requests[0]->json()['input'][0]['content'][0]['text']);
    }

    public function testBootstrapGeminiChatFlowUsesRealClientMapperAndFacade()
    {
        $httpClient = new FakeHttpClient();
        $httpClient->queueResponse($this->jsonResponse(array(
            'candidates' => array(
                array(
                    'content' => array(
                        'role' => 'model',
                        'parts' => array(
                            array(
                                'text' => 'Gemini integrated hello',
                            ),
                        ),
                    ),
                    'finishReason' => 'STOP',
                ),
            ),
            'usageMetadata' => array(
                'promptTokenCount' => 4,
            ),
        )));

        $this->bootstrapWithHttpClients(null, $httpClient);

        $response = AI::provider('gemini')
            ->model('gemini-2.5-pro')
            ->instructions('Be concise.')
            ->addMessage('Say hello')
            ->run();

        $this->assertSame('Gemini integrated hello', $response->text());
        $this->assertStringContainsString('/models/gemini-2.5-pro:generateContent', $httpClient->requests[0]->url());
        $payload = $httpClient->requests[0]->json();
        $this->assertSame('Say hello', $payload['contents'][0]['parts'][0]['text']);
        $this->assertSame('Be concise.', $payload['systemInstruction']['parts'][0]['text']);
    }
}
