<?php
/**
 * Shared base for REST controllers.
 *
 * @package Backreload\Api
 */

namespace Backreload\Api;

use Backreload\Plugin;
use WP_REST_Response;

if ( ! defined( 'ABSPATH' ) ) { exit; }

abstract class BaseController {

	protected Plugin $plugin;

	public function __construct( Plugin $plugin ) {
		$this->plugin = $plugin;
	}

	abstract public function register( string $namespace ): void;

	public function check_perm(): bool {
		return RestRouter::check_permissions();
	}

	protected function ok( $data, int $code = 200 ): WP_REST_Response {
		return new WP_REST_Response( $data, $code );
	}

	protected function error( string $code, string $message, int $status = 400, array $extra = [] ): WP_REST_Response {
		return new WP_REST_Response( array_merge( [
			'code'    => $code,
			'message' => $message,
		], $extra ), $status );
	}
}
