<?php
/**
 * Account Router
 *
 * Handles navigation and content routing for the account area.
 *
 * @package AmbassadorHub\Accounts\Blocks\Account
 */

namespace AmbassadorHub\Accounts\Blocks\Account;

use function apply_filters;
use function sanitize_key;
use function add_query_arg;
use function remove_query_arg;

/**
 * Router for account block navigation and content.
 */
final class Router {
	/**
	 * Get the current active tab.
	 *
	 * @return string Tab ID.
	 */
	public static function get_current_tab(): string {
		$tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'dashboard'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for tab navigation only, no data modification.

		$tabs = self::get_tabs();

		// Fallback to dashboard if invalid tab.
		if ( ! isset( $tabs[ $tab ] ) ) {
			$tab = 'dashboard';
		}

		return $tab;
	}

	/**
	 * Get registered tabs/sections.
	 *
	 * @return array Tabs configuration.
	 */
	public static function get_tabs(): array {
		$tabs = array(
			'dashboard' => array(
				'label'    => __( 'Dashboard', 'ambassadorhub' ),
				'priority' => 10,
			),
			'profile'   => array(
				'label'    => __( 'Profile', 'ambassadorhub' ),
				'priority' => 20,
			),
		);

		/**
		 * Filter account tabs.
		 *
		 * Allows modules to add custom sections to the account area.
		 *
		 * @since 0.1.0
		 *
		 * @param array $tabs {
		 *     Tabs configuration.
		 *
		 *     @type array {
		 *         @type string $label    Tab label.
		 *         @type int    $priority Tab order (lower = first).
		 *     }
		 * }
		 */
		$tabs = apply_filters( 'ambh_account_tabs', $tabs );

		// Sort by priority.
		uasort(
			$tabs,
			static function ( $a, $b ) {
				return ( $a['priority'] ?? 10 ) <=> ( $b['priority'] ?? 10 );
			}
		);

		return $tabs;
	}

	/**
	 * Render tab navigation.
	 *
	 * @param string $current_tab Current active tab ID.
	 *
	 * @return void
	 */
	public static function render_navigation( string $current_tab ): void {
		$tabs = self::get_tabs();

		/**
		 * Fires before account navigation.
		 *
		 * @since 0.1.0
		 *
		 * @param string $current_tab Current tab ID.
		 */
		do_action( 'ambh_before_account_navigation', $current_tab );

		require __DIR__ . '/Templates/navigation.php';

		/**
		 * Fires after account navigation.
		 *
		 * @since 0.1.0
		 *
		 * @param string $current_tab Current tab ID.
		 */
		do_action( 'ambh_after_account_navigation', $current_tab );
	}

	/**
	 * Render tab content.
	 *
	 * @param string $current_tab Current active tab ID.
	 *
	 * @return void
	 */
	public static function render_content( string $current_tab ): void {
		/**
		 * Fires before account content.
		 *
		 * @since 0.1.0
		 *
		 * @param string $current_tab Current tab ID.
		 */
		do_action( 'ambh_before_account_content', $current_tab );

		/**
		 * Fires to render account tab content.
		 *
		 * Modules should hook here to render their tab content.
		 *
		 * @since 0.1.0
		 *
		 * @param string $current_tab Current tab ID.
		 */
		do_action( "ambh_account_content_{$current_tab}", $current_tab );

		/**
		 * Fires after account content.
		 *
		 * @since 0.1.0
		 *
		 * @param string $current_tab Current tab ID.
		 */
		do_action( 'ambh_after_account_content', $current_tab );
	}

	/**
	 * Get URL for a specific tab.
	 *
	 * @param string $tab Tab ID.
	 *
	 * @return string Tab URL.
	 */
	public static function get_tab_url( string $tab ): string {
		global $wp;

		if ( 'dashboard' === $tab ) {
			return remove_query_arg( 'tab' );
		}

		return add_query_arg( 'tab', $tab );
	}
}
