<?php
/**
 * PDF Design post type registration.
 *
 * @package PDFDraft
 */

namespace PDFDraft\PostTypes;

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

use PDFDraft\Controllers\V1\PDFDesignRevisionsController;
use PDFDraft\Support\Factory\ControllerFactory;

/**
 * Registers and configures the PDF Design custom post type.
 */
class PDFDesign extends PostType {

	const NAMESPACE = 'pdfdraft/v1';
	const REST_BASE = 'pdf-designs';

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->addAction( "pdfdraft:{$this->getPostType()}:post-type:registered", array( $this, 'onRegistration' ) );
		$this->addAction( 'rest_api_init', array( $this, 'replaceController' ), 5 );
	}

	/**
	 * Get the post type slug.
	 *
	 * @return string
	 */
	public function getPostType(): string {
		return 'pdfdraft-design';
	}

	/**
	 * Get the post type registration arguments.
	 *
	 * @return array
	 */
	public function getPostTypeArgs(): array {
		return $this->applyFilters(
			'pdfdraft:post-type:design:args',
			array(
				'labels'                          => $this->labels(),
				'public'                          => false,
				'exclude_from_search'             => true,
				'publicly_queryable'              => false,
				'show_ui'                         => false,
				'show_in_menu'                    => false,
				'query_var'                       => false,
				'rewrite'                         => false,
				'capability_type'                 => 'post',
				'has_archive'                     => false,
				'hierarchical'                    => false,
				'show_in_rest'                    => true,
				'rest_namespace'                  => self::NAMESPACE,
				'rest_base'                       => self::REST_BASE,
				'rest_controller_class'           => \PDFDraft\Controllers\V1\PDFDesignsController::class,
				'supports'                        => $this->supports(),
				'revisions_rest_controller_class' => PDFDesignRevisionsController::class,
				'autosaves_rest_controller_class' => \PDFDraft\Controllers\V1\PDFDesignAutosavesController::class,
				'taxonomies'                      => array( 'design_orientation' ),
			)
		);
	}

	/**
	 * Get the post type labels.
	 *
	 * @return array
	 */
	protected function labels() {
		return $this->applyFilters(
			'pdfdraft:post-type:design:labels',
			array(
				'name'               => __( 'PDF design', 'pdfdraft' ),
				'singular_name'      => __( 'PDF design', 'pdfdraft' ),
				'add_new'            => __( 'Add new pdf design', 'pdfdraft' ),
				'add_new_item'       => __( 'Add new pdf design', 'pdfdraft' ),
				'edit_item'          => __( 'Edit pdf design', 'pdfdraft' ),
				'new_item'           => __( 'New pdf design', 'pdfdraft' ),
				'view_item'          => __( 'View pdf design', 'pdfdraft' ),
				'search_items'       => __( 'Search pdf design', 'pdfdraft' ),
				'not_found'          => __( 'No font pdf design', 'pdfdraft' ),
				'not_found_in_trash' => __( 'No pdf design found in Trash', 'pdfdraft' ),
				'parent_item_colon'  => '',
			)
		);
	}

	/**
	 * Get the supported post type features.
	 *
	 * @return array
	 */
	protected function supports() {
		return $this->applyFilters(
			'pdfdraft:post-type:design:supports',
			array( 'title', 'editor', 'thumbnail', 'revisions', 'author', 'custom-fields' )
		);
	}

	/**
	 * Register post meta on post type registration.
	 *
	 * @return void
	 */
	public function onRegistration() {
		register_meta(
			'post',
			'orientation',
			array(
				'object_subtype' => $this->getPostType(),
				'show_in_rest'   => true,
				'type'           => 'string',
				'default'        => 'landscape',
				'single'         => true,
			)
		);
		register_meta(
			'post',
			'pdfdraft_for',
			array(
				'object_subtype'    => $this->getPostType(),
				'show_in_rest'      => true,
				'type'              => 'string',
				'single'            => true,
				'default'           => '',
				'sanitize_callback' => 'sanitize_text_field',
			)
		);
	}

	/**
	 * Replace the controller instance with one that has dependencies injected.
	 *
	 * @return void
	 */
	public function replaceController(): void {
		$post_type_object = get_post_type_object( $this->getPostType() );
		if ( ! $post_type_object ) {
			return;
		}
		$post_type_object->rest_controller = ControllerFactory::createPDFDesignsController( $this->getPostType() );
	}
}
