<?php
/**
 * Dealer custom post type registration.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\PostTypes;

class DealerPostType {

	public const POST_TYPE = 'trx_ad_dealer';

	/**
	 * Register the post type.
	 *
	 * Hooked to `init`.
	 *
	 * @return void
	 */
	public function register(): void {
		$labels = array(
			'name'                  => _x( 'Dealers', 'Post type general name', 'trx-auto-dealer' ),
			'singular_name'         => _x( 'Dealer', 'Post type singular name', 'trx-auto-dealer' ),
			'menu_name'             => _x( 'Dealers', 'Admin menu', 'trx-auto-dealer' ),
			'name_admin_bar'        => _x( 'Dealer', 'Add new on admin bar', 'trx-auto-dealer' ),
			'add_new'               => __( 'Add New', 'trx-auto-dealer' ),
			'add_new_item'          => __( 'Add New Dealer', 'trx-auto-dealer' ),
			'new_item'              => __( 'New Dealer', 'trx-auto-dealer' ),
			'edit_item'             => __( 'Edit Dealer', 'trx-auto-dealer' ),
			'view_item'             => __( 'View Dealer', 'trx-auto-dealer' ),
			'all_items'             => __( 'All Dealers', 'trx-auto-dealer' ),
			'search_items'          => __( 'Search Dealers', 'trx-auto-dealer' ),
			'parent_item_colon'     => __( 'Parent Dealer:', 'trx-auto-dealer' ),
			'not_found'             => __( 'No dealers found.', 'trx-auto-dealer' ),
			'not_found_in_trash'    => __( 'No dealers found in Trash.', 'trx-auto-dealer' ),
			'featured_image'        => __( 'Dealer Logo', 'trx-auto-dealer' ),
			'set_featured_image'    => __( 'Set dealer logo', 'trx-auto-dealer' ),
			'remove_featured_image' => __( 'Remove dealer logo', 'trx-auto-dealer' ),
			'use_featured_image'    => __( 'Use as dealer logo', 'trx-auto-dealer' ),
			'archives'              => __( 'Dealer Archives', 'trx-auto-dealer' ),
			'filter_items_list'     => __( 'Filter dealers list', 'trx-auto-dealer' ),
			'items_list_navigation' => __( 'Dealers list navigation', 'trx-auto-dealer' ),
			'items_list'            => __( 'Dealers list', 'trx-auto-dealer' ),
		);

		$args = array(
			'labels'             => $labels,
			'public'             => true,
			'publicly_queryable' => true,
			'show_ui'            => true,
			'show_in_menu'       => true,
			'show_in_rest'       => true,
			'query_var'          => true,
			'rewrite'            => array(
				'slug'       => 'dealers',
				'with_front' => false,
			),
			'capability_type'    => 'trx_ad_dealer',
			'map_meta_cap'       => true,
			// Dealer-role users get `edit_trx_ad_dealers` (default
			// `create_posts` cap) so the admin menu builds correctly:
			// without "Add New" the WP menu collapse logic in
			// wp-admin/includes/menu.php strips the only remaining
			// submenu (because its slug equals the parent slug),
			// which breaks `get_admin_page_parent()` and 403s the
			// dealer out of their own list. Save-time gate is in
			// `DataIsolation::denyOthersCaps` — a dealer trying to
			// create a NEW dealer post gets `do_not_allow` at save
			// because `$post_id !== $dealer_id`.
			'has_archive'        => true,
			'hierarchical'       => false,
			'menu_position'      => 26,
			'menu_icon'          => 'dashicons-store',
			'supports'           => array( 'title', 'editor', 'thumbnail' ),
		);

		register_post_type( self::POST_TYPE, $args );
	}
}
