<?php

namespace OtomaticAi\Api\Gemini;

use Exception;
use OtomaticAi\Api\Gemini\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 api endpoint
     *
     * @var string
     */
    private string $endpoint = "https://generativelanguage.googleapis.com/v1beta/models";

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

    /**
     * The api key
     *
     * @var string
     */
    private string $key;

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

        $this->key = $key;

        // create the http client
        $settings = [
            "base_uri" => rtrim($this->endpoint, "/") . '/',
            "headers" => [
                "Content-Type" => "application/json",
            ],
            "timeout" => 600
        ];

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

    /**
     * Call the chat api
     *
     * @param array $payloads
     * @return array
     * @throws Exception
     */
    public function chat(array $payloads): array
    {
        // call the chat api
        $requests = [];
        foreach ($payloads as $payload) {
            $requests[] = [
                "request" => new Request(
                    'POST',
                    rtrim($this->endpoint, "/") . '/' . $payload["model"] . ":generateContent?key=" . $this->key,
                    [],
                    json_encode($payload)
                ),
                "after" => function ($complete) {

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

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

        return $completes;
    }

    /**
     * Call the image api
     *
     * @param array $payloads
     * @return array
     * @throws Exception
     */
    public function image(array $payloads): array
    {
        // call the chat api
        $requests = [];
        foreach ($payloads as $payload) {
            $requests[] = [
                "request" => new Request(
                    'POST',
                    rtrim($this->endpoint, "/") . '/' . $payload["model"] . ":generateContent?key=" . $this->key,
                    [],
                    json_encode($payload)
                ),
                "after" => function ($complete) use ($payload) {

                    // store the usage
                    Usage::create([
                        "provider" => "gemini_image",
                        "payload" => [
                            "model" => $payload["model"],
                            "artifacts" => 1,
                        ]
                    ]);
                }
            ];
        }

        $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;
    }
}
