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

use boru\ai\Embedding\EmbeddingProviderInterface;
use boru\ai\Embedding\EmbeddingRequest;
use boru\ai\Embedding\EmbeddingResult;
use boru\ai\Provider\Capabilities;
use boru\ai\Provider\ProviderInterface;
use boru\ai\Schema\Request;
use boru\ai\Schema\Response;
use boru\ai\Schema\ToolCall;

class FakeProvider implements ProviderInterface, EmbeddingProviderInterface
{
    /** @var int */
    public $executeCalls = 0;

    /** @var int */
    public $continueCalls = 0;

    /** @var array */
    public $receivedToolResults = array();

    /** @var int */
    public $embedCalls = 0;

    public function name()
    {
        return 'fake';
    }

    public function capabilities()
    {
        return new Capabilities(array(
            'tools' => true,
            'embeddings' => true,
        ));
    }

    public function supportsModel($model)
    {
        return strpos((string)$model, 'fake-') === 0;
    }

    public function execute(Request $request, $eventCallback = null)
    {
        $this->executeCalls++;

        $response = new Response();
        $response->provider('fake');
        $response->model($request->model());
        $response->status('tool_calling');

        $toolCall = new ToolCall();
        $toolCall->id('toolcall-1');
        $toolCall->name('get_current_temperature');
        $toolCall->arguments(array(
            'location' => 'Atlanta, GA',
            'unit' => 'Fahrenheit',
        ));

        $response->addToolCall($toolCall);
        return $response;
    }

    public function continueResponse(Request $request, Response $previousResponse, array $toolResults, $eventCallback = null)
    {
        $this->continueCalls++;
        $this->receivedToolResults = $toolResults;

        $output = null;
        if (!empty($toolResults)) {
            $output = $toolResults[0]->output();
        }

        $response = new Response();
        $response->provider('fake');
        $response->model($request->model());
        $response->status('completed');
        $response->text('Weather resolved: ' . $output);
        return $response;
    }

    public function embed(EmbeddingRequest $request)
    {
        $this->embedCalls++;

        $result = new EmbeddingResult();
        $result->provider('fake');
        $result->model($request->model());

        foreach ($request->inputs() as $index => $input) {
            $result->addEmbedding(array((float)($index + 1), (float)strlen($input)), $input, $index);
        }

        return $result;
    }
}
