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

use boru\ai\Conversation\Conversation;
use boru\ai\Conversation\ConversationReference;
use boru\ai\Conversation\Turn;
use boru\ai\Runtime\Runtime;
use boru\ai\Runtime\RuntimeContext;
use boru\ai\Schema\Request;
use boru\ai\Schema\Response;
use PHPUnit\Framework\TestCase;

class RuntimeConversationReplayTest extends TestCase
{
    public function testBuildReplayMessagesReturnsPriorUserAndAssistantMessages()
    {
        $runtime = new Runtime(new RuntimeContext());
        $reference = new ConversationReference();
        $reference->externalReference('conversation_replay_test');

        $conversation = new Conversation($reference);
        $conversation->provider('gemini');
        $conversation->model('gemini-2.5-pro');

        $previousRequest = new Request();
        $previousRequest->provider('gemini')->model('gemini-2.5-pro')->addMessage('Hello there', 'user');

        $previousResponse = new Response();
        $previousResponse->provider('gemini')->model('gemini-2.5-pro')->status('completed')->text('Hi! How can I help?');

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

        $currentRequest = new Request();
        $currentRequest->provider('gemini')->model('gemini-2.5-pro')->addMessage('Tell me more', 'user');

        $replayed = $runtime->buildReplayMessages($conversation, $currentRequest);

        $this->assertCount(2, $replayed);
        $this->assertSame('user', $replayed[0]->role());
        $this->assertSame('Hello there', $replayed[0]->parts()[0]->value());
        $this->assertSame('assistant', $replayed[1]->role());
        $this->assertSame('Hi! How can I help?', $replayed[1]->parts()[0]->value());
    }

    public function testBuildReplayRequestPrependsReplayMessagesToCurrentRequest()
    {
        $runtime = new Runtime(new RuntimeContext());
        $reference = new ConversationReference();
        $reference->externalReference('conversation_replay_test');

        $conversation = new Conversation($reference);

        $previousRequest = new Request();
        $previousRequest->addMessage('Hello there', 'user');
        $previousResponse = new Response();
        $previousResponse->text('Hi! How can I help?');

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

        $currentRequest = new Request();
        $currentRequest->addMessage('Tell me more', 'user');

        $workingRequest = $runtime->buildReplayRequest($currentRequest, $conversation);

        $this->assertCount(3, $workingRequest->messages());
        $this->assertSame('Hello there', $workingRequest->messages()[0]->parts()[0]->value());
        $this->assertSame('assistant', $workingRequest->messages()[1]->role());
        $this->assertSame('Hi! How can I help?', $workingRequest->messages()[1]->parts()[0]->value());
        $this->assertSame('Tell me more', $workingRequest->messages()[2]->parts()[0]->value());
    }
}
