<?php
/**
 * @package Hafenstudios\Kurato
 */

namespace Hafenstudios\Kurato\Cockpit\Rest;

use Hafenstudios\Kurato\Cockpit\SignatureGuard;

defined( 'ABSPATH' ) || exit;

/**
 * GET kurato-cockpit/v1/verify - Pairing-Rueckruf des Dienstes.
 *
 * Der Dienst beweist mit einem signierten Aufruf, dass der Code-Einloeser die
 * Site kontrolliert (und HTTPS + REST funktionieren). Antwort: ok + contract.
 */
final class VerifyController {

	public const REST_NAMESPACE = 'kurato-cockpit/v1';

	private SignatureGuard $guard;

	/**
	 * @param SignatureGuard $guard Server-Signatur-Pruefung.
	 */
	public function __construct( SignatureGuard $guard ) {
		$this->guard = $guard;
	}

	/**
	 * @return void
	 */
	public function register_routes(): void {
		register_rest_route(
			self::REST_NAMESPACE,
			'/verify',
			array(
				'methods'             => \WP_REST_Server::READABLE,
				'callback'            => array( $this, 'verify' ),
				'permission_callback' => array( $this->guard, 'permission' ),
			)
		);
	}

	/**
	 * @return \WP_REST_Response
	 */
	public function verify(): \WP_REST_Response {
		return new \WP_REST_Response(
			array(
				'ok'       => true,
				'contract' => 'v1',
			),
			200
		);
	}
}
