<?php

namespace WarrantyCart\Admin;

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

/**
 * Admin Menu Class
 */
class Menu {

	/**
	 * Constructor
	 */
	public function __construct() {
		add_action( 'admin_menu', array( $this, 'register' ) );
	}

	/**
	 * Register admin menu
	 */
	public function register(): void {
		global $submenu;

		$slug          = 'warranty-cart';
		$capability    = apply_filters( 'warranty_cart_menu_capability', 'manage_warranty_cart' );
		$menu_position = apply_filters( 'warranty_cart_menu_position', 56 );

		// Menu icon
		$menu_icon = 'dashicons-shield';

		/**
		 * Filter the admin menu icon
		 *
		 * @param string $menu_icon Menu icon.
		 */
		$menu_icon = apply_filters( 'warranty_cart_admin_menu_icon', $menu_icon );

		// Main menu page
		$this->add_menu(
			__( 'Warranty Cart', 'warranty-cart' ),
			__( 'Warranty Cart', 'warranty-cart' ),
			$capability,
			$slug,
			array( $this, 'render_dashboard_page' ),
			$menu_icon,
			$menu_position
		);

		if ( current_user_can( $capability ) ) {
			// phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited
			$submenu[ $slug ][] = array( __( 'Dashboard', 'warranty-cart' ), $capability, 'admin.php?page=' . $slug . '#/' );
			$submenu[ $slug ][] = array( __( 'Warranties', 'warranty-cart' ), $capability, 'admin.php?page=' . $slug . '#/warranties' );
			$submenu[ $slug ][] = array( __( 'Orders', 'warranty-cart' ), $capability, 'admin.php?page=' . $slug . '#/orders' );
			$submenu[ $slug ][] = array( __( 'Reports', 'warranty-cart' ), $capability, 'admin.php?page=' . $slug . '#/reports' );
			$submenu[ $slug ][] = array( __( 'Tools', 'warranty-cart' ), $capability, 'admin.php?page=' . $slug . '#/tools' );
			$submenu[ $slug ][] = array( __( 'Settings', 'warranty-cart' ), $capability, 'admin.php?page=' . $slug . '#/settings' );

			/**
			 * Filter admin menu submenu items
			 *
			 * @param array  $submenu_items Submenu items.
			 * @param string $capability    Menu capability.
			 * @param string $slug          Menu slug.
			 */
			$submenu[ $slug ] = apply_filters( 'warranty_cart_admin_menu_submenu', $submenu[ $slug ], $capability, $slug );
			// phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited
		}

		/**
		 * Action for adding custom menu items
		 *
		 * @param string $capability    Menu capability.
		 * @param int    $menu_position Menu position.
		 */
		do_action( 'warranty_cart_admin_menu', $capability, $menu_position );
	}

	/**
	 * Add menu page helper
	 *
	 * @param string   $page_title Page title.
	 * @param string   $menu_title Menu title.
	 * @param string   $capability Capability required to access the menu.
	 * @param string   $menu_slug  Menu slug.
	 * @param callable $callback   Callback function to render the page.
	 * @param string   $icon       Menu icon.
	 * @param int      $position   Menu position.
	 */
	private function add_menu( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $callback, string $icon, int $position ): void {
		add_menu_page(
			$page_title,
			$menu_title,
			$capability,
			$menu_slug,
			$callback,
			$icon,
			$position
		);
	}

	/**
	 * Render dashboard page
	 */
	public function render_dashboard_page(): void {
		echo '<div id="warranty-cart-dashboard"></div>';
	}
}
