<?php
namespace boru\ai\Tests\Provider\OpenAI;

use boru\ai\Http\HttpResponse;
use boru\ai\Provider\OpenAI\OpenAIClient;
use boru\ai\Tests\Fakes\FakeHttpClient;
use PHPUnit\Framework\TestCase;

class OpenAIClientTest extends TestCase
{
    public function testPostBuildsAuthenticatedJsonHttpRequest()
    {
        $httpClient = new FakeHttpClient();
        $httpResponse = new HttpResponse();
        $httpResponse->statusCode(200)->body('{"id":"resp_1","status":"completed"}');
        $httpClient->queueResponse($httpResponse);

        $client = new OpenAIClient('test-key', array('timeout' => 30), $httpClient);
        $result = $client->post('responses', array('model' => 'gpt-5.1'));

        $this->assertSame('resp_1', $result['id']);
        $this->assertCount(1, $httpClient->requests);
        $request = $httpClient->requests[0];
        $this->assertSame('POST', $request->method());
        $this->assertSame('https://api.openai.com/v1/responses', $request->url());
        $this->assertSame('Bearer test-key', $request->header('Authorization'));
        $this->assertSame('application/json', $request->header('Content-Type'));
        $this->assertSame('gpt-5.1', $request->json()['model']);
        $this->assertSame(30, $request->option('timeout'));
    }

    public function testPostThrowsOnErrorResponses()
    {
        $httpClient = new FakeHttpClient();
        $httpResponse = new HttpResponse();
        $httpResponse->statusCode(400)->body('{"error":{"message":"Bad request"}}');
        $httpClient->queueResponse($httpResponse);

        $client = new OpenAIClient('test-key', array(), $httpClient);

        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('Bad request');

        $client->post('responses', array('model' => 'gpt-5.1'));
    }
}
