<?php
/**
 * Admin menu.
 *
 * @package WPDudeProducts
 */

declare(strict_types=1);

namespace WPDude\Products\Admin;

use WPDude\Products\Products\Post_Type;
use WPDude\Products\Products\Taxonomy;

/**
 * Top-level Dude Commerce menu + submenus.
 */
final class Menu {

	/**
	 * Top-level admin menu slug.
	 */
	public const MENU_SLUG = 'dude-commerce';

	/**
	 * Register hooks.
	 */
	public function register(): void {
		add_action( 'admin_menu', array( $this, 'add_top_level_menu' ), 9 );
		add_action( 'admin_menu', array( $this, 'add_submenus' ), 20 );
		// Freemius registers Account / Contact / Upgrade at WP_FS__LOWEST_PRIORITY (999999999).
		add_action( 'admin_menu', array( $this, 'add_more_plugins_submenu' ), PHP_INT_MAX );
		add_action( 'admin_menu', array( $this, 'move_more_plugins_submenu_last' ), PHP_INT_MAX );
		add_filter( 'parent_file', array( $this, 'fix_parent_file' ) );
		add_filter( 'submenu_file', array( $this, 'fix_submenu_file' ), 10, 2 );
	}

	/**
	 * Parent menu.
	 */
	public function add_top_level_menu(): void {
		add_menu_page(
			__( 'Dude Commerce', 'dude-commerce' ),
			__( 'Dude Commerce', 'dude-commerce' ),
			'edit_posts',
			self::MENU_SLUG,
			array( $this, 'render_home' ),
			'dashicons-cart',
			58
		);
	}

	/**
	 * Settings page URL.
	 */
	public static function settings_url(): string {
		return admin_url( 'admin.php?page=dude-commerce-settings' );
	}

	/**
	 * Orders page URL.
	 */
	public static function orders_url(): string {
		return Orders_Page::list_url();
	}

	/**
	 * Picking page URL.
	 */
	public static function picking_url(): string {
		return Picking_Page::url();
	}

	/**
	 * Product categories admin URL.
	 */
	public static function categories_url(): string {
		return admin_url( 'edit-tags.php?taxonomy=' . Taxonomy::TAXONOMY . '&post_type=' . Post_Type::POST_TYPE );
	}

	/**
	 * Keep Dude Commerce selected when managing product categories.
	 *
	 * @param string $parent_file Parent file.
	 */
	public function fix_parent_file( string $parent_file ): string {
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( $screen && Taxonomy::TAXONOMY === $screen->taxonomy ) {
			return self::MENU_SLUG;
		}
		return $parent_file;
	}

	/**
	 * Highlight the Categories submenu item.
	 *
	 * @param string|null $submenu_file Submenu file.
	 * @param string      $parent_file  Parent file.
	 * @return string|null
	 */
	public function fix_submenu_file( $submenu_file, string $parent_file ) {
		unset( $parent_file );
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( $screen && Taxonomy::TAXONOMY === $screen->taxonomy ) {
			return 'edit-tags.php?taxonomy=' . Taxonomy::TAXONOMY . '&post_type=' . Post_Type::POST_TYPE;
		}
		return $submenu_file;
	}

	/**
	 * Landing page.
	 */
	public function render_home(): void {
		if ( ! current_user_can( 'edit_posts' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'dude-commerce' ) );
		}

		$products_url   = admin_url( 'edit.php?post_type=' . Post_Type::POST_TYPE );
		$categories_url = self::categories_url();
		$orders_url     = self::orders_url();
		$picking_url    = self::picking_url();
		$settings_url   = self::settings_url();
		?>
		<div class="wrap">
			<h1><?php echo esc_html__( 'Dude Commerce', 'dude-commerce' ); ?></h1>
			<p><?php echo esc_html__( 'Sell physical products with Stripe Checkout.', 'dude-commerce' ); ?></p>
			<p>
				<a class="button button-primary" href="<?php echo esc_url( $products_url ); ?>">
					<?php echo esc_html__( 'Manage products', 'dude-commerce' ); ?>
				</a>
				<a class="button" href="<?php echo esc_url( $categories_url ); ?>">
					<?php echo esc_html__( 'Categories', 'dude-commerce' ); ?>
				</a>
				<?php if ( current_user_can( 'manage_options' ) ) : ?>
					<a class="button" href="<?php echo esc_url( $picking_url ); ?>">
						<?php echo esc_html__( 'Picking', 'dude-commerce' ); ?>
					</a>
					<a class="button" href="<?php echo esc_url( $orders_url ); ?>">
						<?php echo esc_html__( 'Manage orders', 'dude-commerce' ); ?>
					</a>
					<a class="button" href="<?php echo esc_url( $settings_url ); ?>">
						<?php echo esc_html__( 'Settings', 'dude-commerce' ); ?>
					</a>
				<?php endif; ?>
			</p>
		</div>
		<?php
	}

	/**
	 * Submenus.
	 */
	public function add_submenus(): void {
		add_submenu_page(
			self::MENU_SLUG,
			__( 'Categories', 'dude-commerce' ),
			__( 'Categories', 'dude-commerce' ),
			'manage_categories',
			'edit-tags.php?taxonomy=' . Taxonomy::TAXONOMY . '&post_type=' . Post_Type::POST_TYPE
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Picking', 'dude-commerce' ),
			__( 'Picking', 'dude-commerce' ),
			'manage_options',
			Picking_Page::PAGE_SLUG,
			array( Picking_Page::class, 'render' )
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Orders', 'dude-commerce' ),
			__( 'Orders', 'dude-commerce' ),
			'manage_options',
			Orders_Page::PAGE_SLUG,
			array( Orders_Page::class, 'render' )
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Settings', 'dude-commerce' ),
			__( 'Settings', 'dude-commerce' ),
			'manage_options',
			'dude-commerce-settings',
			array( Settings_Page::class, 'render' )
		);
	}

	/**
	 * Cross-promotion page — registered late so Freemius links appear above it.
	 */
	public function add_more_plugins_submenu(): void {
		$hook = add_submenu_page(
			self::MENU_SLUG,
			__( 'More WP Dude Plugins', 'dude-commerce' ),
			__( 'More WP Dude Plugins', 'dude-commerce' ),
			'manage_options',
			More_Plugins_Page::PAGE_SLUG,
			array( More_Plugins_Page::class, 'render' )
		);

		if ( is_string( $hook ) && '' !== $hook ) {
			More_Plugins_Page::bind_screen_hook( $hook );
		}
	}

	/**
	 * Ensure the More WP Dude Plugins item is the last submenu entry.
	 */
	public function move_more_plugins_submenu_last(): void {
		global $submenu;

		$parent = self::MENU_SLUG;
		if ( empty( $submenu[ $parent ] ) || ! is_array( $submenu[ $parent ] ) ) {
			return;
		}

		$target = null;
		$rest   = array();
		foreach ( $submenu[ $parent ] as $item ) {
			$slug = isset( $item[2] ) ? (string) $item[2] : '';
			if ( More_Plugins_Page::PAGE_SLUG === $slug ) {
				$target = $item;
				continue;
			}
			$rest[] = $item;
		}

		if ( null === $target ) {
			return;
		}

		$rest[]             = $target;
		$submenu[ $parent ] = $rest;
	}
}
