<?php //phpcs:ignore

/**
 * File: Ecre_MyAccount_Menu.php
 *
 * The Ecre_MyAccount_Menu class handles customizing the My Account menu in WooCommerce.
 *
 * @package EchoRewardsPro\Frontend
 * @since   1.0.0
 */

namespace ECRE\Frontend;

if ( ! class_exists( 'Ecre_MyAccount_Menu' ) ) {
	/**
	 * The Ecre_MyAccount_Menu class handles customizing the My Account menu in WooCommerce.
	 *
	 * @since 1.0.0
	 */
	class Ecre_MyAccount_Menu {

		/**
		 * The name of the menu.
		 *
		 * @var string
		 */
		private $menu_name;

		/**
		 * The slug (unique identifier) for the menu.
		 *
		 * @var string
		 */
		private $menu_slug;

		/**
		 * Initializes the Ecre_MyAccount_Menu class.
		 *
		 * Sets up class properties and registers WordPress/WooCommerce hooks
		 * based on plugin configuration settings and pro status.
		 *
		 * @param bool  $is_pro            Indicates whether the user has a professional (pro) account.
		 * @param array $erce_settings     The current settings array for configuring Ecre_MyAccount_Menu.
		 * @param array $default_settings  The default settings used for fallback or initialization.
		 *
		 * @since 1.0.0
		 */
		public function __construct( $is_pro, $erce_settings, $default_settings ) {
			$this->menu_name = $erce_settings['referralMenuName'];

			if ( ! empty( $this->menu_name ) ) {
				$this->menu_slug = strtolower( str_replace( ' ', '-', $this->menu_name ) );
			}

			$woocommerce_account_hook = 'woocommerce_account_' . $this->menu_slug . '_endpoint';

			$this->ecre_pro_add_rewards_endpoint();
			add_filter( 'query_vars', array( $this, 'ecre_pro_query_vars' ) );
			add_filter( 'woocommerce_account_menu_items', array( $this, 'ecre_pro_add_rewards_link_my_account' ) );
			add_action( $woocommerce_account_hook, array( $this, 'ecre_pro_rewards_content' ) );
		}

		/**
		 * Add the 'woo-echo-rewards-coupon' endpoint to WordPress.
		 *
		 * @since 1.0.0
		 */
		public function ecre_pro_add_rewards_endpoint() {
			if ( ! empty( $this->menu_name ) ) {
				$this->menu_slug = strtolower( str_replace( ' ', '-', $this->menu_name ) );
			}

			flush_rewrite_rules();
			add_rewrite_endpoint( $this->menu_slug, EP_ROOT | EP_PAGES );
		}

		/**
		 * Add 'woo-echo-rewards-coupon' to the list of query vars.
		 *
		 * @param array $vars List of query vars.
		 *
		 * @return array
		 *
		 * @since 1.0.0
		 */
		public function ecre_pro_query_vars( $vars ) {
			if ( ! empty( $this->menu_name ) ) {
				$this->menu_slug = strtolower( str_replace( ' ', '-', $this->menu_name ) );
			}

			$vars[] = $this->menu_slug;
			return $vars;
		}

		/**
		 * Add 'Refer & Get Discounts' link to the My Account menu.
		 *
		 * @param array $items My Account menu items.
		 *
		 * @return array
		 *
		 * @since 1.0.0
		 */
		public function ecre_pro_add_rewards_link_my_account( $items ) {
			unset( $items['customer-logout'] );

			if ( ! empty( $this->menu_name ) ) {
				$this->menu_slug = strtolower( str_replace( ' ', '-', $this->menu_name ) );
			}

			$items[ $this->menu_slug ] = esc_html( $this->menu_name );
			$items['customer-logout']  = esc_html__( 'Logout', 'echo-rewards' );
			return $items;
		}

		/**
		 * Display content for the 'woo-echo-rewards-coupon' endpoint in My Account.
		 *
		 * @since 1.0.0
		 */
		public function ecre_pro_rewards_content() {
			?>
				<div id="ecre_my_account_dashboard">
				</div>
			<?php
		}
	}
}
