<?php
namespace Pay4All\Pro;

if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * Re-registers the taxonomies and post types that used to ship in the free
 * Pay4All Shop plugin before 2.0.0 and were moved into Pay4All Pro.
 *
 *  - Taxonomy p4all_product_cat   : Categories (with quantity rules)
 *  - Taxonomy p4all_product_brand : Brands (with quantity rules)
 *  - CPT p4all_ticket             : one row per seat (QR-code product type)
 *
 * The slugs are identical to what the free plugin used pre-2.0.0 so a site
 * that upgrades from 1.4.x to (Free 2.0.0 + Pro 2.0.0) keeps its historical
 * data intact — the Free plugin's Activator::migrate_from_pre_2_0_0() only
 * drops terms + posts when Pro isn't around to reclaim them, but on a
 * standard install the Pro plugin activates BEFORE that migration runs.
 */
final class PostTypes {

	public const TAXONOMY       = 'p4all_product_cat';
	public const TAXONOMY_BRAND = 'p4all_product_brand';
	public const TICKET         = 'p4all_ticket';
	public const NEWSLETTER     = 'p4all_newsletter';

	public static function register() : void {
		add_action( 'init', [ self::class, 'do_register' ], 6 );
	}

	public static function do_register() : void {
		$product_pt = defined( '\\Pay4All\\Form\\PostTypes::PRODUCT' )
			? \Pay4All\Form\PostTypes::PRODUCT
			: 'p4all_product';

		register_taxonomy( self::TAXONOMY, [ $product_pt ], [
			'labels'            => [
				'name'          => __( 'Catégories',         'pay4all-pro' ),
				'singular_name' => __( 'Catégorie',          'pay4all-pro' ),
				'add_new_item'  => __( 'Ajouter une catégorie', 'pay4all-pro' ),
				'edit_item'     => __( 'Modifier la catégorie', 'pay4all-pro' ),
				'search_items'  => __( 'Rechercher une catégorie', 'pay4all-pro' ),
			],
			'public'            => false,
			'show_ui'           => true,
			'show_admin_column' => true,
			'show_in_menu'      => false,
			'hierarchical'      => true,
			'rewrite'           => false,
			'show_in_rest'      => false,
			'capabilities'      => [
				'manage_terms' => 'manage_options',
				'edit_terms'   => 'manage_options',
				'delete_terms' => 'manage_options',
				'assign_terms' => 'manage_options',
			],
		] );

		register_taxonomy( self::TAXONOMY_BRAND, [ $product_pt ], [
			'labels'            => [
				'name'          => __( 'Marques',            'pay4all-pro' ),
				'singular_name' => __( 'Marque',             'pay4all-pro' ),
				'add_new_item'  => __( 'Ajouter une marque', 'pay4all-pro' ),
				'edit_item'     => __( 'Modifier la marque', 'pay4all-pro' ),
				'search_items'  => __( 'Rechercher une marque', 'pay4all-pro' ),
			],
			'public'            => false,
			'show_ui'           => true,
			'show_admin_column' => true,
			'show_in_menu'      => false,
			'hierarchical'      => true,
			'rewrite'           => false,
			'show_in_rest'      => false,
			'capabilities'      => [
				'manage_terms' => 'manage_options',
				'edit_terms'   => 'manage_options',
				'delete_terms' => 'manage_options',
				'assign_terms' => 'manage_options',
			],
		] );

		// Ticket CPT — one post per issued seat. The list screen is our own
		// (Admin\Tickets\Page), we hide the WordPress default sidebar entry.
		register_post_type( self::TICKET, [
			'labels'          => [
				'name'          => __( 'Billets', 'pay4all-pro' ),
				'singular_name' => __( 'Billet',  'pay4all-pro' ),
			],
			'public'          => false,
			'show_ui'         => false,
			'show_in_menu'    => false,
			'show_in_rest'    => false,
			'supports'        => [ 'title' ],
			'capability_type' => 'page',
			'map_meta_cap'    => true,
		] );

		// Newsletter CPT — one post per campaign. The list screen + editor
		// live under our own submenu (Admin\Newsletter\Page). Kept show_ui
		// true so the wp-admin edit screen works out of the box for the
		// per-language body editor + save flow ; hidden from the sidebar
		// via show_in_menu = false (Menu.php owns the sidebar).
		register_post_type( self::NEWSLETTER, [
			'labels'          => [
				'name'               => __( 'Newsletters',               'pay4all-pro' ),
				'singular_name'      => __( 'Newsletter',                'pay4all-pro' ),
				'add_new'            => __( 'Add newsletter',            'pay4all-pro' ),
				'add_new_item'       => __( 'Add a new newsletter',      'pay4all-pro' ),
				'edit_item'          => __( 'Edit the newsletter',       'pay4all-pro' ),
				'search_items'       => __( 'Search newsletters',        'pay4all-pro' ),
				'not_found'          => __( 'No newsletter yet.',        'pay4all-pro' ),
				'not_found_in_trash' => __( 'No newsletter in the bin.', 'pay4all-pro' ),
			],
			'public'          => false,
			'show_ui'         => true,
			'show_in_menu'    => false,
			'show_in_rest'    => false,
			'supports'        => [ 'title' ],
			'capability_type' => 'page',
			'map_meta_cap'    => true,
			'menu_icon'       => 'dashicons-email-alt',
		] );
	}
}
