<?php
/**
 * Changelog REST controller — GET /blocksify/v1/changelog.
 *
 * Delegates to Blocksify_Dashboard_Changelog::parse() which owns the
 * regex contract. Single-source-of-truth parser; JS receives structured
 * releases (SPEC §8.4.2).
 *
 * @package Blocksify
 */

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

/**
 * REST controller for the changelog endpoint.
 */
class Blocksify_Dashboard_Changelog_Controller extends WP_REST_Controller {

	/**
	 * Wire namespace + base for register_routes().
	 */
	public function __construct() {
		$this->namespace = 'blocksify/v1';
		$this->rest_base = 'changelog';
	}

	/**
	 * Register GET on /changelog.
	 */
	public function register_routes() {
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base,
			array(
				array(
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_item' ),
					'permission_callback' => array( $this, 'permission_check' ),
				),
			)
		);
	}

	/**
	 * Capability gate.
	 *
	 * @return bool
	 */
	public function permission_check() {
		return current_user_can( Blocksify_Dashboard::get_capability() );
	}

	/**
	 * GET /changelog handler.
	 *
	 * @param WP_REST_Request $request Unused.
	 * @return WP_REST_Response
	 */
	public function get_item( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
		return rest_ensure_response( Blocksify_Dashboard_Changelog::parse() );
	}
}
