<?php

namespace OtomaticAi\Api\Anthropic;

use Exception;
use OtomaticAi\Api\Anthropic\Exceptions\ApiException;
use OtomaticAi\Models\Usages\Usage;
use OtomaticAi\Utils\Settings;
use OtomaticAi\Vendors\Illuminate\Support\Arr;
use OtomaticAi\Vendors\GuzzleHttp\Client as HttpClient;
use OtomaticAi\Vendors\GuzzleHttp\Pool;
use OtomaticAi\Vendors\GuzzleHttp\Psr7\Request;
use OtomaticAi\Vendors\GuzzleHttp\Psr7\Response;

class PoolClient
{
    /**
     * The Anthropic api endpoint
     *
     * @var string
     */
    private string $endpoint = "https://api.anthropic.com/v1";

    /**
     * The GuzzleHttp client
     *
     * @var HttpClient
     */
    private HttpClient $client;

    /**
     * Create a new Anthropic Api client
     *
     * @param string|null $key
     * @throws Exception
     */
    public function __construct(string $key = null)
    {
        // get the key
        if (empty($key)) {
            $key = Settings::get("api.anthropic.api_key");
        }
        if (empty($key)) {
            throw new Exception("No Anthropic Api Key provided.");
        }

        // create the http client
        $settings = [
            "base_uri" => rtrim($this->endpoint, "/") . '/',
            "headers" => [
                "x-api-key" => $key,
                "Content-Type" => "application/json",
                "Anthropic-Version" => "2023-06-01",
            ],
            "timeout" => 600
        ];

        $this->client = new HttpClient($settings);
    }

    /**
     * Call the messages api from openai
     *
     * @param array $payloads
     * @return array
     * @throws Exception
     */
    public function messages(array $payloads): array
    {
        // call the chat api
        $requests = [];
        foreach ($payloads as $payload) {
            $requests[] = [
                "request" => new Request(
                    'POST',
                    "messages",
                    [],
                    json_encode($payload)
                ),
                "after" => function ($complete) {

                    // store the usage
                    $usage = Arr::get($complete, "usage", []);
                    if (Arr::get($usage, 'input_tokens', 0) + Arr::get($usage, 'output_tokens', 0) > 0) {
                        Usage::create([
                            "provider" => "anthropic_messages",
                            "payload" => [
                                "model" => Arr::get($complete, 'model'),
                                "input_tokens" => Arr::get($usage, 'input_tokens', 0),
                                "output_tokens" => Arr::get($usage, 'output_tokens', 0),
                            ]
                        ]);
                    }
                }
            ];
        }

        $completes = $this->run($requests);

        return $completes;
    }

    /**
     * Perform an api request
     *
     * @param array $requests
     * @param stirng $uri
     * @param array $options
     * @return array
     * @throws Exception
     */
    private function run(array $requests)
    {
        $output = [];

        // get the http requests
        $httpRequests = Arr::pluck($requests, "request");

        // create the pool
        $pool = new Pool($this->client, $httpRequests, [
            'concurrency' => 10,
            'fulfilled' => function (Response $response, $index) use (&$output, $requests) {
                $response = $response->getBody()->getContents();
                $response = json_decode($response, true);
                $output[$index] = $response;

                // call after callback
                if (!empty($requests[$index]["after"]) && is_callable($requests[$index]["after"])) {
                    $requests[$index]["after"]($response);
                }
            },
            'rejected' => function (Exception $reason, $index) {
                $e = ApiException::make($reason);
                throw $e;
            },
        ]);

        // call the pool
        $promise = $pool->promise();
        $promise->wait();

        return $output;
    }
}
