<?php

namespace OtomaticAi\Controllers\Settings;

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

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

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

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

        $this->validate([
            "affiliate_id" => ["nullable", "string"],
            "store" => ["required", "string"],
        ]);

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