<?php

namespace OtomaticAi\Controllers\SingleMode;

use OtomaticAi\Controllers\Controller;
use OtomaticAi\Models\SingleModeGeneration;
use OtomaticAi\Models\Template;

class TemplateController extends Controller
{
    public function store()
    {
        $this->validate([
            "name" => ["required", "string"],
            "generation" => ["required", "array"],
        ]);

        $payload = [
            "persona_id" => $this->input("generation.persona_id"),
            "modules" => SingleModeGeneration::settingsToModules($this->input("generation.settings", [])),
        ];

        $template = new Template([
            "name" => $this->input("name"),
            "plugin_version" => OTOMATIC_AI_VERSION,
            "payload" => $payload,
        ]);

        if ($template->save()) {
            $this->emptyResponse();
        }

        $this->response(["message" => "An error occurred", "error" => "Unable to create the template."], 503);
    }
}
