<?php
namespace boru\ai\Cli;

use boru\ai\Conversation\ConversationReference;
use boru\ai\Persistence\Db\DbConfig;
use boru\ai\Persistence\Db\DbConnection;
use boru\ai\Persistence\Db\DbConversationInspector;
use boru\cli2\CLIContext;
use boru\cli2\Contracts\CommandDescriptionInterface;
use boru\cli2\Contracts\CommandInterface;
use boru\cli2\Contracts\CommandParamsInterface;
use boru\cli2\Params\Option;

class ConversationShowCommand implements CommandInterface, CommandParamsInterface, CommandDescriptionInterface
{
    public function name()
    {
        return 'conversation:show';
    }

    public function description()
    {
        return 'Show a persisted conversation and its turns';
    }

    public function commandParams()
    {
        return array(
            Option::create('conversation-id|conversation-id|Conversation id'),
            Option::create('reference|reference|External conversation reference'),
            Option::create('user-id|user-id|Optional user id for external reference lookup'),
        );
    }

    public function handle(CLIContext $ctx)
    {
        $reference = new ConversationReference();
        if ($ctx->get('conversation-id')) {
            $reference->id($ctx->get('conversation-id'));
        }
        if ($ctx->get('reference')) {
            $reference->externalReference($ctx->get('reference'));
        }
        if ($ctx->get('user-id')) {
            $reference->userId($ctx->get('user-id'));
        }

        $config = DbConfig::fromConfig($ctx->get('config'));
        $inspector = new DbConversationInspector(new DbConnection($config));
        $conversation = $inspector->loadConversationRecord($reference);

        if (!$conversation) {
            $ctx->output('Conversation not found.');
            return false;
        }

        $ctx->output('Conversation: ' . $conversation['conversation_id']);
        $ctx->output('  External reference: ' . $conversation['external_reference']);
        $ctx->output('  User id: ' . $conversation['user_id']);
        $ctx->output('  Provider: ' . $conversation['provider']);
        $ctx->output('  Model: ' . $conversation['model']);
        $ctx->output('  Created: ' . $conversation['created_at']);
        $ctx->output('  Updated: ' . $conversation['updated_at']);

        $turns = $inspector->listTurnRecords($conversation['id']);
        $ctx->output('  Turns: ' . count($turns));

        foreach ($turns as $turn) {
            $ctx->output('');
            $ctx->output('Turn #' . $turn['turn_number'] . ' [' . $turn['status'] . ']');
            $ctx->output('  Started: ' . $turn['started_at']);
            $ctx->output('  Completed: ' . $turn['completed_at']);
            $ctx->output('  Request JSON: ' . (is_string($turn['request_json']) ? strlen($turn['request_json']) . ' bytes' : 'null'));
            $ctx->output('  Response JSON: ' . (is_string($turn['response_json']) ? strlen($turn['response_json']) . ' bytes' : 'null'));
            $ctx->output('  Tool Results JSON: ' . (is_string($turn['tool_results_json']) ? strlen($turn['tool_results_json']) . ' bytes' : 'null'));
        }

        return true;
    }
}
