<?php
/**
 * AbstractController class.
 *
 * Base REST Controller providing shared functionality.
 *
 * @package Ilabs\Firevert\Core\Api\Controller
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Api\Controller;

use Ilabs\Firevert\Core\Api\Validator\RequestValidator;
use WP_REST_Request;

/**
 * Class AbstractController.
 *
 * Provides a common base for all REST API controllers in Firevert.
 */
abstract class AbstractController {

	/**
	 * RequestValidator instance.
	 *
	 * @var RequestValidator
	 */
	protected RequestValidator $request_validator;

	/**
	 * AbstractController constructor.
	 */
	public function __construct() {
		$this->request_validator = firevert_container()->get( 'request_validator' );
	}

	/**
	 * Returns the permission callback for REST routes.
	 *
	 * Ensures that incoming requests originate from the allowed domain.
	 *
	 * @return callable The permission callback function.
	 */
	protected function get_permission_callback(): callable {
		return function ( WP_REST_Request $request ) {
			$validation = $this->request_validator->validate_request_origin( $request );

			if ( is_wp_error( $validation ) ) {
				return $validation;
			}

			return true;
		};
	}
}
