<?php

namespace OtomaticAi\Controllers\Settings;

use OtomaticAi\Controllers\Controller;
use OtomaticAi\Utils\Settings;
use WP_Error;

class GlobalSystemsController extends Controller
{
    /**
     * Get the api settings
     *
     * @return void
     */
    public function index()
    {
        $this->verifyNonce();

        $this->response(Settings::get('global_systems'));
    }

    /**
     * Update the global systems settings.
     * 
     * @return void
     */
    public function update()
    {
        $this->verifyNonce();

        $this->validate([
            "text_instructions" => ["nullable", "string"],
        ]);

        Settings::update($this->request, 'global_systems');
        if (Settings::save()) {
            $this->response([
                "success" => true,
            ]);
        } else {
            return new WP_Error('error', 'Unable to save the settings.', ['status' => 503]);
        }
    }
}
