<?php
/**
 * PDF Design autosaves REST API controller.
 *
 * @package PDFDraft
 */

namespace PDFDraft\Controllers\V1;

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

/**
 * Handles REST API requests for PDF design autosaves.
 */
class PDFDesignAutosavesController extends \WP_REST_Autosaves_Controller {

	/**
	 * Constructor.
	 *
	 * @param string $parent_post_type Parent post type slug.
	 */
	public function __construct( $parent_post_type ) {
		parent::__construct( $parent_post_type );
		$post_type_object           = get_post_type_object( $parent_post_type );
		$parent_controller          = $post_type_object->get_rest_controller();
		$this->parent_controller    = $parent_controller;
		$this->parent_post_type     = $parent_post_type;
		$this->revisions_controller = $post_type_object->get_revisions_rest_controller();
		$this->rest_base            = 'autosaves';
		$this->parent_base          = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
	}

	/**
	 * Register REST API routes for autosaves.
	 *
	 * @return void
	 */
	public function register_routes() {
		register_rest_route(
			$this->namespace,
			'/' . $this->parent_base . '/(?P<id>[\w-]+)/' . $this->rest_base,
			array(
				'args'   => array(
					'parent' => array(
						'description' => __( 'The ID for the parent of the autosave.', 'pdfdraft' ),
						'type'        => 'integer',
					),
				),
				array(
					'methods'             => \WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_items' ),
					'permission_callback' => array( $this, 'get_items_permissions_check' ),
					'args'                => $this->get_collection_params(),
				),
				array(
					'methods'             => \WP_REST_Server::CREATABLE,
					'callback'            => array( $this, 'create_item' ),
					'permission_callback' => array( $this, 'create_item_permissions_check' ),
					'args'                => $this->parent_controller->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ),
				),
				'schema' => array( $this, 'get_public_item_schema' ),
			)
		);

		register_rest_route(
			$this->namespace,
			'/' . $this->parent_base . '/(?P<parent>[\w-]+)/' . $this->rest_base . '/(?P<id>[\w-]+)',
			array(
				'args'   => array(
					'parent' => array(
						'description' => __( 'The ID for the parent of the autosave.', 'pdfdraft' ),
						'type'        => 'string',
					),
					'id'     => array(
						'description' => __( 'The ID for the autosave.', 'pdfdraft' ),
						'type'        => 'string',
					),
				),
				array(
					'methods'             => \WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_item' ),
					'permission_callback' => array( $this->revisions_controller, 'get_item_permissions_check' ),
					'args'                => array(
						'context' => $this->get_context_param( array( 'default' => 'view' ) ),
					),
				),
				'schema' => array( $this, 'get_public_item_schema' ),
			)
		);
	}
}
