<?php

namespace OtomaticAi\Controllers;

use Exception;
use OtomaticAi\Models\Presets\Preset;
use OtomaticAi\Utils\Language;
use OtomaticAi\Vendors\Illuminate\Support\Arr;
use OtomaticAi\Vendors\Illuminate\Support\Str;

class RewriteRequestController extends Controller
{
    public function __invoke()
    {
        $this->verifyNonce();

        $this->validate([
            "engine" => ["required", "string"],
            "title" => ["required_without:titles", "string"],
            "titles" => ["required_without:title", "array"],
            "language" => ["required", "string"],
        ]);

        $response = $this->makeResponse();

        $this->response($response);
    }

    private function makeResponse(): array
    {
        try {
            // get the openai preset
            $preset = Preset::findFromAPI("rewrite_title");

            if (!empty($this->input('title'))) {

                // run the preset
                $response = $preset->process([
                    "language" => Language::find($this->input('language', 'en'))->value,
                    "request" => $this->input('title'),
                ]);

                // get the response content
                $content = json_decode(Arr::get($response, 'choices.0.message.content'), true, 512, JSON_THROW_ON_ERROR);
                $content = Arr::get($content, "value");
                $content = Str::clean($content);
                
                if (!empty($content)) {
                    $content = ucfirst($content);
                    return [
                        "title" => $content,
                    ];
                }
            } else if (!empty($this->input('titles'))) {
                // make payloads
                $payloads = [];
                foreach ($this->input('titles', []) as $keyword) {
                    $payloads[] = [
                        "language" => Language::find($this->input('language', 'en'))->value,
                        "request" => $keyword,
                    ];
                }

                // run the pool preset
                $responses = $preset->processPool($payloads);

                // get the response content
                $output = [];
                foreach ($responses as $index => $response) {

                    $content = json_decode(Arr::get($response, 'choices.0.message.content'), true, 512, JSON_THROW_ON_ERROR);
                    $content = Arr::get($content, "value");
                    $content = Str::clean($content);
                    
                    if (!empty($content)) {
                        $content = ucfirst($content);
                        $output[$index] = [
                            "title" => $content,
                        ];
                    }
                }

                ksort($output);

                return $output;
            }
        } catch (Exception $e) {
            $this->response(["message" => "An error occurred", "error" => $e->getMessage()], 503);
        }

        return [
            "title" => $this->input('title'),
        ];
    }
}
