<?php
/**
 * Product custom post type.
 *
 * @package WPDudeProducts
 */

declare(strict_types=1);

namespace WPDude\Products\Products;

use WPDude\Products\Admin\Menu;

/**
 * Registers wpdude_dc_product CPT.
 */
final class Post_Type {

	public const POST_TYPE = 'wpdude_dc_product';

	/**
	 * Register CPT.
	 */
	public static function register(): void {
		register_post_type(
			self::POST_TYPE,
			array(
				'labels'              => array(
					'name'               => __( 'Products', 'dude-commerce' ),
					'singular_name'      => __( 'Product', 'dude-commerce' ),
					'add_new'            => __( 'Add New', 'dude-commerce' ),
					'add_new_item'       => __( 'Add New Product', 'dude-commerce' ),
					'edit_item'          => __( 'Edit Product', 'dude-commerce' ),
					'new_item'           => __( 'New Product', 'dude-commerce' ),
					'view_item'          => __( 'View Product', 'dude-commerce' ),
					'search_items'       => __( 'Search Products', 'dude-commerce' ),
					'not_found'          => __( 'No products found.', 'dude-commerce' ),
					'not_found_in_trash' => __( 'No products found in Trash.', 'dude-commerce' ),
					'menu_name'          => __( 'Products', 'dude-commerce' ),
					'all_items'          => __( 'Products', 'dude-commerce' ),
				),
				'public'              => true,
				'show_ui'             => true,
				'show_in_menu'        => Menu::MENU_SLUG,
				'menu_position'       => null,
				'menu_icon'           => null,
				'supports'            => array( 'title', 'thumbnail' ),
				'capability_type'     => 'post',
				'map_meta_cap'        => true,
				'has_archive'         => true,
				'rewrite'             => array(
					'slug'       => 'products',
					'with_front' => false,
				),
				'show_in_rest'        => true,
			)
		);
	}

	/**
	 * CPT-related hooks.
	 */
	public static function hooks(): void {
		add_action( 'after_setup_theme', array( self::class, 'ensure_thumbnail_support' ), 20 );
		add_filter( 'enter_title_here', array( self::class, 'title_placeholder' ), 10, 2 );
		add_filter( 'use_block_editor_for_post_type', array( self::class, 'disable_block_editor' ), 10, 2 );
	}

	/**
	 * Featured images must be available for products.
	 */
	public static function ensure_thumbnail_support(): void {
		add_theme_support( 'post-thumbnails' );
		add_post_type_support( self::POST_TYPE, 'thumbnail' );
	}

	/**
	 * Keep the product screen on classic UI so meta boxes are the editor.
	 *
	 * @param bool   $use       Whether to use the block editor.
	 * @param string $post_type Post type.
	 */
	public static function disable_block_editor( bool $use, string $post_type ): bool {
		if ( self::POST_TYPE === $post_type ) {
			return false;
		}
		return $use;
	}

	/**
	 * Title placeholder.
	 *
	 * @param string   $title Placeholder.
	 * @param \WP_Post $post  Post.
	 */
	public static function title_placeholder( string $title, $post ): string {
		if ( isset( $post->post_type ) && self::POST_TYPE === $post->post_type ) {
			return __( 'Product name', 'dude-commerce' );
		}
		return $title;
	}
}
