<?php

namespace Prodigy\Includes\Api\V1;

use Prodigy\Includes\Content\Prodigy_Settings_Parser;
use Prodigy\Includes\Helpers\Prodigy_Logger;
use Prodigy\Includes\Prodigy_Cache;
use WP_REST_Request;
use WP_REST_Response;

defined( 'ABSPATH' ) || exit;
/**
 * Class Prodigy_Api_Settings
 */
class Prodigy_Api_Settings extends Prodigy_Api_Main {

	/**
	 * CloudPlatform constructor.
	 */
	public function __construct() {
		parent::__construct();
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
	}

	/**
	 * Register routes
	 */
	public function register_routes() {

		register_rest_route(
			get_prodigy_plugin_directory_name() . '/v1',
			self::API_SETTINGS_PATH,
			array(
				'methods'             => 'PUT',
				'callback'            => array( $this, 'update' ),
				'permission_callback' => function ( WP_REST_Request $request ) {
					return $this->is_authorization( $request->get_header( 'x-Authorization' ) );
				},
			)
		);
	}

	/**
	 *
	 * @param WP_REST_Request $request_params
	 *
	 * @return WP_REST_Response
	 */
	public function update( WP_REST_Request $request_params ): WP_REST_Response {
		if ( PRODIGY_DEBUG_MODE ) {
			Prodigy_Logger::add_record( Prodigy_Logger::TYPE_INFO, 'Settings applications urls' . $request_params->get_param( 'message' ) );
		}

		$settings_parser = new Prodigy_Settings_Parser();
		$settings_parser->set_platform_settings( $request_params->get_params() );
		$cache = new Prodigy_Cache();
		$cache->reset_settings();
		$response['data']['success'] = true;

		return new WP_REST_Response( $response, \WP_Http::CREATED );
	}
}
