<?php

namespace WC_Affiliate\Controllers\Admin;

defined( 'ABSPATH' ) || exit;

/**
 * Admin Menu Controller for WC Affiliate Plugin.
 *
 * Handles admin menu registration, submenu management, and navigation
 * for the affiliate management interface in WordPress admin.
 *
 * @since 1.0.0
 */
class Menu {

	/**
	 * Controller constructor
	 *
	 * Initializes the admin menu controller and registers necessary hooks
	 */
	public function __construct() {
		$this->register_hooks();
	}

	/**
	 * Register hooks for menu controller
	 *
	 * @return void
	 */
	protected function register_hooks(): void {
		add_action( 'admin_menu', array( $this, 'register' ) );
	}

	/**
	 * Register admin menu and submenus
	 *
	 * Creates the main WC Affiliate menu in WordPress admin with all necessary submenus
	 * and applies filters for customization.
	 *
	 * @return void
	 */
	public function register(): void {
		/**
		 * Action hook fired before admin menu registration.
		 */
		do_action( 'wc_affiliate_admin_menu_register_before' );

		global $submenu;

		/**
		 * Action before admin menu is added
		 *
		 * @since 3.0.0
		 */
		do_action( 'dokan_kits_before_admin_menu' );

		$slug          = 'wc-affiliate';
		$capability    = apply_filters( 'wc_affiliate_menu_capability', 'manage_options' );
		$menu_position = apply_filters( 'wc_affiliate_menu_position', 25 );

		// Menu icon as SVG data URI.
		$menu_icon = 'dashicons-chart-line';

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

		// Main menu page.
		add_menu_page(
			__( 'WC Affiliate', 'wc-affiliate' ),
			__( 'WC Affiliate', 'wc-affiliate' ),
			$capability,
			$slug,
			array( $this, 'callback_dashboard' ),
			$menu_icon,
			$menu_position
		);

		if ( current_user_can( $capability ) ) {
			// phpcs:disable.
			$submenu[ $slug ][] = array( __( 'Summary', 'wc-affiliate' ), $capability, 'admin.php?page=' . $slug . '#/' );
			$submenu[ $slug ][] = array( __( 'Affiliates', 'wc-affiliate' ), $capability, 'admin.php?page=' . $slug . '#/affiliates' );
			$submenu[ $slug ][] = array( __( 'Visits', 'wc-affiliate' ), $capability, 'admin.php?page=' . $slug . '#/visits' );
			$submenu[ $slug ][] = array( __( 'Referrals', 'wc-affiliate' ), $capability, 'admin.php?page=' . $slug . '#/referrals' );
			$submenu[ $slug ][] = array( __( 'Payables', 'wc-affiliate' ), $capability, 'admin.php?page=' . $slug . '#/payables' );
			$submenu[ $slug ][] = array( __( 'Transactions', 'wc-affiliate' ), $capability, 'admin.php?page=' . $slug . '#/transactions' );
			$submenu[ $slug ][] = array( __( 'Banners', 'wc-affiliate' ), $capability, 'admin.php?page=' . $slug . '#/banners' );
			$submenu[ $slug ][] = array( __( 'Settings', 'wc-affiliate' ), $capability, 'admin.php?page=' . $slug . '#/settings' );

			if ( wca_should_show_setup_wizard() ) {
				$submenu[ $slug ][] = array( __( 'Setup Wizard', 'wc-affiliate' ), $capability, 'admin.php?page=' . $slug . '#/setup-wizard' );
			}

			if ( ! wca_should_show_setup_wizard() && ! wca_is_pro_activated() ) {
				$submenu[ $slug ][] = array( '<span style="background: #8523f5; width: 100%; display: inline-block; color: #fff; text-align: center; padding: 5px 0; border-radius: 4px;">' . __( 'Upgrade to Pro', 'wc-affiliate' ) . '</span>', $capability, 'admin.php?page=' . $slug . '#/upgrade' );
			}
			// phpcs:enable.

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

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

		/**
		 * Action hook fired after admin menu registration.
		 *
		 * @param string $capability    Menu capability.
		 * @param int    $menu_position Menu position.
		 */
		do_action( 'wc_affiliate_admin_menu_register_after', $capability, $menu_position );
	}

	/**
	 * Render the admin dashboard page
	 *
	 * Outputs the main admin dashboard template for the WC Affiliate plugin.
	 *
	 * @return void
	 */
	public function callback_dashboard(): void {
		/**
		 * Action hook fired before admin dashboard rendering.
		 */
		do_action( 'wc_affiliate_admin_dashboard_callback_before' );

		echo wca_get_template( 'dashboard', 'views/admin' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		/**
		 * Action hook fired after admin dashboard rendering.
		 */
		do_action( 'wc_affiliate_admin_dashboard_callback_after' );
	}
}
