<?php

namespace OtomaticAi\Content\Image;

use DOMDocument;
use DOMElement;
use OtomaticAi\Utils\Image as UtilsImage;
use OtomaticAi\Utils\Support;
use OtomaticAi\Vendors\Illuminate\Support\Arr;

class Gemini extends Image
{
    const PROVIDER = "gemini";

    public ?string $prompt;
    public ?string $model;

    /**
     * @param string $src
     * @param string $title
     * @param string $description
     * @param string $legend
     * @param string $prompt
     * @param string $model
     * @param string $uid
     * @return self
     */
    static public function make(string $base64, string $title = null, string $description = null, string $legend = null, string $prompt = null, string $model = null, string $uid = null): self
    {
        $image = new self(UtilsImage::fromBase64($base64), $title, $description, $legend, $uid);

        $image->prompt = $prompt;
        $image->model = $model;

        return $image;
    }

    /**
     * @return DOMElement|null
     */
    public function toHtmlElement(DOMDocument $document): ?DOMElement
    {
        if (($attachmentId = $this->image->save($this->title, $this->description, $this->legend)) !== null) {
            $el = $document->createElement("otoimage");
            $el->setAttribute("attachment", $attachmentId);
            $el->setAttribute("prompt", mb_convert_encoding(Support::removeEmojis($this->prompt), "UTF-8"));
            $el->setAttribute("provider", self::PROVIDER);
            $el->setAttribute("model", $this->model);
            $el->setAttribute("uid", $this->uid);

            return $el;
        }

        return null;
    }

    /**
     * @return array|null
     */
    public function toArray(): ?array
    {
        if (($attachmentId = $this->image->save($this->title, $this->description, $this->legend)) !== null) {
            return [
                "attachment" => $attachmentId,
                "prompt" => mb_convert_encoding(Support::removeEmojis($this->prompt), "UTF-8"),
                "provider" => self::PROVIDER,
                "model" => $this->model,
                "uid" => $this->uid,
            ];
        }

        return null;
    }

    /**
     * @return self|null
     */
    static public function fromHtmlElement(DOMElement $node): ?self
    {
        $attachmentId = $node->getAttribute("attachment");

        $attachment = UtilsImage::fromAttachmentId($attachmentId);

        if ($attachment) {
            $image = new self($attachment, get_the_title($attachmentId), get_post_field("post_content", $attachmentId), get_post_field("post_excerpt", $attachmentId));
            $image->prompt = $node->getAttribute("prompt");
            $image->model = $node->getAttribute("model");
            $image->uid = $node->getAttribute("uid");

            return $image;
        }

        return null;
    }

    /**
     * @return self|null
     */
    static public function fromArray(array $data): ?self
    {
        $attachmentId = $data["attachment"];

        $attachment = UtilsImage::fromAttachmentId($attachmentId);

        if ($attachment) {
            $image = new self($attachment, get_the_title($attachmentId), get_post_field("post_content", $attachmentId), get_post_field("post_excerpt", $attachmentId));
            $image->prompt = $data["prompt"];
            $image->model = $data["model"];
            $image->uid = Arr::get($data, "uid");

            return $image;
        }

        return null;
    }
}
