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

use boru\ai\AI;
use boru\ai\Config\Config;
use boru\ai\Conversation\Conversation;
use boru\ai\Conversation\ConversationReference;
use boru\ai\Conversation\Turn;
use boru\ai\Provider\OpenAI\OpenAIMapper;
use boru\ai\Provider\OpenAI\OpenAIProvider;
use boru\ai\Runtime\RuntimeContext;
use boru\ai\Schema\Request;
use boru\ai\Schema\Response;
use boru\ai\Tests\Fakes\FakeOpenAIClient;
use PHPUnit\Framework\TestCase;

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

    public function testOpenAIProviderUsesPreviousResponseIdForResumedConversationWhenReplayEnabled()
    {
        AI::config(new Config(array(
            'providers' => array(
                'openai' => array(
                    'replay' => array(
                        'enabled' => true,
                    ),
                ),
            ),
        )));

        $client = new FakeOpenAIClient(array(
            array(
                'id' => 'resp_new',
                'model' => 'gpt-5.1',
                'status' => 'completed',
                'output' => array(
                    array(
                        'type' => 'message',
                        'content' => array(
                            array(
                                'type' => 'output_text',
                                'text' => 'Resumed ok',
                            ),
                        ),
                    ),
                ),
            ),
        ));

        $provider = new OpenAIProvider($client, new OpenAIMapper());
        $conversation = $this->buildConversationWithPreviousResponseId('resp_previous');

        $request = new Request();
        $request->provider('openai')->model('gpt-5.1')->addMessage('Tell me more');
        $request->metadata('conversation', $conversation);

        $response = $provider->execute($request);

        $this->assertSame('Resumed ok', $response->text());
        $this->assertSame('resp_previous', $client->requests[0]['payload']['previous_response_id']);
    }

    public function testOpenAIProviderSkipsPreviousResponseIdWhenReplayDisabled()
    {
        AI::config(new Config(array(
            'providers' => array(
                'openai' => array(
                    'replay' => array(
                        'enabled' => false,
                    ),
                ),
            ),
        )));

        $client = new FakeOpenAIClient(array(
            array(
                'id' => 'resp_new',
                'model' => 'gpt-5.1',
                'status' => 'completed',
                'output' => array(
                    array(
                        'type' => 'message',
                        'content' => array(
                            array(
                                'type' => 'output_text',
                                'text' => 'Fresh request ok',
                            ),
                        ),
                    ),
                ),
            ),
        ));

        $provider = new OpenAIProvider($client, new OpenAIMapper());
        $conversation = $this->buildConversationWithPreviousResponseId('resp_previous');

        $request = new Request();
        $request->provider('openai')->model('gpt-5.1')->addMessage('Tell me more');
        $request->metadata('conversation', $conversation);

        $response = $provider->execute($request);

        $this->assertSame('Fresh request ok', $response->text());
        $this->assertArrayNotHasKey('previous_response_id', $client->requests[0]['payload']);
    }

    protected function buildConversationWithPreviousResponseId($responseId)
    {
        $reference = new ConversationReference();
        $reference->externalReference('openai_replay_test');
        $conversation = new Conversation($reference);

        $previousRequest = new Request();
        $previousRequest->provider('openai')->model('gpt-5.1')->addMessage('Hello there');

        $previousResponse = new Response();
        $previousResponse->provider('openai')->model('gpt-5.1')->id($responseId)->status('completed')->text('Hi! How can I help?');

        $previousTurn = new Turn();
        $previousTurn->number(1)->status('completed')->request($previousRequest)->response($previousResponse);
        $conversation->addTurn($previousTurn);

        return $conversation;
    }
}
