<?php

namespace OtomaticAi\Models\Presets;

use OtomaticAi\Api\OtomaticAi\Client;
use OtomaticAi\Vendors\Illuminate\Database\Eloquent\Model;
use OtomaticAi\Vendors\Illuminate\Support\Arr;
use OtomaticAi\Vendors\Illuminate\Support\Str;

class Preset extends Model
{
    protected $fillable = ["model", "name", "messages", "temperature", "top_p", "presence_penalty", "frequency_penalty", "is_json"];

    protected $casts = [
        'is_json' => 'boolean',
        'messages' => 'array',
    ];

    /**
     * Create a new Eloquent model instance.
     *
     * @param  array  $attributes
     * @return void
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
    }

    static function findFromAPI(string $role, string $model = null)
    {
        $client = new Client;

        return self::make($client->findAIPreset($role, defined('OTOMATIC_AI_VERSION') ? OTOMATIC_AI_VERSION : null, $model));
    }

    static function make($params)
    {
        if ($params["model"] === "automatic-1") {
            $params["model"] = "gpt-4.1-nano";
        }

        if ($params["model"] === "automatic-2") {
            $params["model"] = "gpt-4.1-mini";
        }

        if ($params["model"] === "automatic-3") {
            $params["model"] = "gpt-4.1";
        }

        if (Str::startsWith($params["model"], ["groq-"])) {
            return new GroqPreset($params);
        }

        if (Str::startsWith($params["model"], "mistral-")) {
            return new MistralAIPreset($params);
        }

        if (Str::startsWith($params["model"], "claude-")) {
            return new AnthropicPreset($params);
        }

        if (Str::startsWith($params["model"], "deepseek-")) {
            return new DeepSeekPreset($params);
        }

        if (Str::startsWith($params["model"], "gemini-")) {
            return new GeminiPreset($params);
        }

        if (Str::startsWith($params["model"], "grok-")) {
            return new XAIPreset($params);
        }

        return new OpenAIPreset($params);
    }

    protected function replaceMessagesVariables($params)
    {
        $messages = [];

        foreach ($this->messages as $message) {

            foreach ($params as $key => $value) {
                if (empty($value)) {
                    $value = "";
                }

                $key = strtoupper($key);

                $message["content"] = str_replace("[" . $key . "]", $value, $message["content"]);

                $message["content"] = str_replace("\r\n", "\n", $message["content"]);

                $message["content"] = preg_replace_callback("/\[IF;" . $key . "\](.*?)(?:\[ELSE\](.*?))?\[ENDIF\]/s", function ($matches) use ($key, $value) {
                    $trueValue = Arr::get($matches, 1);
                    $falseValue = Arr::get($matches, 2);
                    if (boolval($value)) {
                        if (!empty($trueValue)) {
                            return $trueValue;
                        }
                    } else {
                        if (!empty($falseValue)) {
                            return $falseValue;
                        }
                    }
                    return "";
                }, $message["content"]);
            }

            $messages[] = $message;
        }

        return $messages;
    }
}
