<?php

namespace OtomaticAi\Api\Ideogram;

use Exception;
use OtomaticAi\Api\Ideogram\Exceptions\ApiException;
use OtomaticAi\Vendors\GuzzleHttp\Exception\ClientException;
use OtomaticAi\Models\Usages\Usage;
use OtomaticAi\Utils\Settings;
use OtomaticAi\Vendors\GuzzleHttp\Client as HttpClient;
use OtomaticAi\Vendors\Illuminate\Support\Arr;

class Client
{
    /**
     * The ideogram api endpoint
     *
     * @var string
     */
    private string $endpoint = "https://api.ideogram.ai";

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

    /**
     * The auth key
     *
     * @var HttpClient
     */
    private string $key;

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

        $this->key = $key;

        // create the http client
        $this->client = new HttpClient(
            [
                "base_uri" => rtrim($this->endpoint, "/") . '/',
                "headers" => [
                    "Api-Key" => $this->key,
                    "Accept" => "application/json",
                    "Content-Type" => "application/json",
                ],
                "timeout" => 600
            ]
        );
    }

    /**
     * Call the generate api endpoint
     *
     * @param array $settings
     * @return array
     * @throws Exception
     */
    public function generate(array $settings = []): array
    {
        // make the payload
        $settings = array_merge([
            "rendering_speed" => "TURBO",
            "magic_prompt" => "AUTO",
            "resolution" => "1280x768",
        ], $settings);

        // call the api endpoint
        $response = $this->request("POST", 'v1/ideogram-v3/generate', [
            "json" => $settings
        ]);

        // store the usage
        Usage::create([
            "provider" => "ideogram",
            "payload" => [
                "model" => "ideogram-v3",
                "quality" => $settings["rendering_speed"],
                "artifacts" => 1,
            ]
        ]);

        return $response;
    }

    /**
     * Perform an api request
     *
     * @param string $method
     * @param stirng $uri
     * @param array $options
     * @return array
     * @throws Exception
     */
    private function request(string $method, string $uri, array $options = [])
    {
        try {
            $response = $this->client->request($method, $uri, $options);
            $response = $response->getBody()->getContents();
            $response = json_decode($response, true);

            return $response;
        } catch (ClientException $e) {
            $e = ApiException::make($e);

            throw $e;
        }
    }
}
