<?php
/**
 * Postomatic Plugin - This file handles automatic
 * class loading for the Postomatic plugin.
 *
 *  PHP Version 5.6
 *
 * @package  Postomatic
 * @category Plugin
 * @license  GPL-2.0-or-later https://www.gnu.org/licenses/gpl-2.0.html
 * @link     https://buildomatics.com/postomatics
 * @since    1.0.0
 */

namespace Postomatic\Api;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


/**
 * Handles REST API endpoint for code validation and API key management.
 *
 * This class registers and manages a REST API endpoint for validating codes
 * and managing API keys. It processes incoming POST requests, validates the
 * provided codes against stored values, and handles the generation and
 * storage of new secure keys.
 *
 * @category API
 * @license  GPL-2.0-or-later https://www.gnu.org/licenses/gpl-2.0.html
 * @link     https://buildomatics.com/postomatics
 * @since    1.0.0
 */
class Code_Endpoint {

	/**
	 * Initialize the REST API endpoint for code validation and secure key
	 * management.
	 */
	public function __construct() {
		add_action(
			'rest_api_init',
			array( $this, 'register_endpoint' )
		);
	}

	/**
	 * Register the REST route for code validation.
	 *
	 * @return void
	 */
	public function register_endpoint() {
		register_rest_route(
			\Postomatic\Constants::API_WP_ENDPOINT_BASE,
			\Postomatic\Constants::API_WP_ENDPOINT_CODE,
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'handle_code' ),
				'permission_callback' => '__return_true',
			// No special permissions required.
			)
		);
	}

	/**
	 * Handle the code validation request.
	 *
	 * @param \WP_REST_Request $request The incoming request object.
	 *
	 * @return \WP_REST_Response Response object with validation results.
	 */
	public function handle_code( \WP_REST_Request $request ) {
		// Get parameters from the request.
		$api_key    = $request->get_param( 'api_key' );
		$code       = $request->get_param( 'code' );
		$wp_api_key = $request->get_header( 'Authorization-Code' );

		// Retrieve stored option and compare values.
		$option_code = \Postomatic\Utils::get_code_option();

		if ( $option_code !== $code
			|| \Postomatic\Utils::get_wp_api_secure_key() !== $wp_api_key
		) {

			$data = array(
				'success' => false,
				'message' => 'Invalid API Key',
			);

			return new \WP_REST_Response( $data, 401 );
		}

		// Generate a new secure key and update it.
		$wp_api_key = \Postomatic\Utils::generate_secure_key( 120 );
		\Postomatic\Utils::update_or_add_wp_api_secure_key( $wp_api_key );

		// Store the service API key.
		\Postomatic\Utils::update_or_add_api_service_key( $api_key );

		// Build the response data.
		$data = array(
			'success'    => true,
			'wp_api_key' => $wp_api_key,
		);

		// Update validation flag.
		\Postomatic\Utils::update_or_add_is_validating( false );

		return rest_ensure_response( $data );
	}

	/**
	 * Initialize the endpoint.
	 *
	 * @return void
	 */
	public static function init() {
		new self();
	}
}
