<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Register the Menu CPT and taxonomy with dynamic slug
 */
final class Sifency_CPT_Menu {

	public function __construct() {
		if ( Sifency_Addons_Theme_Builder_Master::sifency_addons_is_legal() ) {
		add_action( 'init', [ $this, 'register_menu_cpt' ] );
		}
	}

	public function register_menu_cpt() {
		$options = get_option( 'sifency-options' );
		$slug = ! empty( $options['menu_slug'] ) ? sanitize_title( $options['menu_slug'] ) : 'menu';

		$labels = array(
			'name'               => __( 'Restaurant Menus', 'sifency-addons' ),
			'singular_name'      => __( 'Restaurant Menu', 'sifency-addons' ),
			'add_new'            => __( 'Restaurant Add Restaurant New', 'sifency-addons' ),
			'add_new_item'       => __( 'Add New Restaurant Menu', 'sifency-addons' ),
			'edit_item'          => __( 'Edit Restaurant Menu', 'sifency-addons' ),
			'new_item'           => __( 'New Restaurant Menu', 'sifency-addons' ),
			'view_item'          => __( 'View Restaurant Menu', 'sifency-addons' ),
			'search_items'       => __( 'Search Restaurant Menus', 'sifency-addons' ),
			'not_found'          => __( 'No menu found', 'sifency-addons' ),
			'not_found_in_trash' => __( 'No menu found in trash', 'sifency-addons' ),
			'menu_name'          => __( 'Restaurant Menu', 'sifency-addons' ),
		);

		$args = array(
			'labels'             => $labels,
			'public'             => true,
			'has_archive'        => true,
			'rewrite'            => array( 'slug' => $slug ),
			'menu_icon'          => 'dashicons-welcome-widgets-menus',
			'show_in_rest'       => true,
			'supports'           => array( 'title', 'thumbnail', 'excerpt', 'revisions' ),
		);

		register_post_type( 'menu', $args );
		$this->register_menu_taxonomy();
	}

	private function register_menu_taxonomy() {
		$labels = array(
			'name'          => __( 'Menu Categories', 'sifency-addons' ),
			'singular_name' => __( 'Menu Category', 'sifency-addons' ),
			'search_items'  => __( 'Search Categories', 'sifency-addons' ),
			'all_items'     => __( 'All Categories', 'sifency-addons' ),
			'edit_item'     => __( 'Edit Category', 'sifency-addons' ),
			'update_item'   => __( 'Update Category', 'sifency-addons' ),
			'add_new_item'  => __( 'Add New Category', 'sifency-addons' ),
			'new_item_name' => __( 'New Category Name', 'sifency-addons' ),
			'menu_name'     => __( 'Categories', 'sifency-addons' ),
		);

		$args = array(
			'hierarchical'      => true,
			'labels'            => $labels,
			'show_ui'           => true,
			'show_admin_column' => true,
			'query_var'         => true,
			'rewrite'           => array( 'slug' => 'menu-category' ),
		);

		register_taxonomy( 'menu_category', array( 'menu' ), $args );
	}
}

new Sifency_CPT_Menu();
