<?php

namespace OtomaticAi\RestControllers;

use OtomaticAi\RestControllers\RestController;
use OtomaticAi\Utils\Settings;

class ApiKeyRestController extends RestController
{
    public function update()
    {
        $this->validate([
            "openai" => ["nullable", "string"],
            "mistral_ai" => ["nullable", "string"],
            "stability_ai" => ["nullable", "string"],
            "unsplash" => ["nullable", "string"],
            "pexels" => ["nullable", "string"],
            "pixabay" => ["nullable", "string"],
            "anthropic" => ["nullable", "string"],
            "ideogram" => ["nullable", "string"],
            "flux" => ["nullable", "string"],
            "deepseek" => ["nullable", "string"],
            "gemini" => ["nullable", "string"],
            "x_ai" => ["nullable", "string"],
        ]);

        // openai
        if (!empty($this->input("openai"))) {
            Settings::update($this->input("openai"), 'api.openai.api_key');
        }

        // mistral ai
        if (!empty($this->input("mistral_ai"))) {
            Settings::update($this->input("mistral_ai"), 'api.mistral_ai.api_key');
        }

        // stability ai
        if (!empty($this->input("stability_ai"))) {
            Settings::update($this->input("stability_ai"), 'api.stability_ai.api_key');
        }

        // unsplash
        if (!empty($this->input("unsplash"))) {
            Settings::update($this->input("unsplash"), 'api.unsplash.api_key');
        }

        // pexels
        if (!empty($this->input("pexels"))) {
            Settings::update($this->input("pexels"), 'api.pexels.api_key');
        }

        // pixabay
        if (!empty($this->input("pixabay"))) {
            Settings::update($this->input("pixabay"), 'api.pixabay.api_key');
        }

        // anthropic
        if (!empty($this->input("anthropic"))) {
            Settings::update($this->input("anthropic"), 'api.anthropic.api_key');
        }

        // ideogram
        if (!empty($this->input("ideogram"))) {
            Settings::update($this->input("ideogram"), 'api.ideogram.api_key');
        }

        // flux
        if (!empty($this->input("flux"))) {
            Settings::update($this->input("flux"), 'api.flux.api_key');
        }

        // deepseek
        if (!empty($this->input("deepseek"))) {
            Settings::update($this->input("deepseek"), 'api.deepseek.api_key');
        }

        // gemini
        if (!empty($this->input("gemini"))) {
            Settings::update($this->input("gemini"), 'api.gemini.api_key');
        }

        // x_ai
        if (!empty($this->input("x_ai"))) {
            Settings::update($this->input("x_ai"), 'api.x_ai.api_key');
        }

        Settings::save();

        return [
            "success" => true,
        ];
    }
}
