<?php
/**
 * Admin Page Layout Helpers
 *
 * Shared header and footer renderer used across all AmbassadorHub admin pages
 * to eliminate the repeated `.ambh-admin` wrapper and header markup.
 *
 * All HTML parameters (title_html, subtitle_html, action_html) must be
 * pre-sanitised by the caller using esc_html(), esc_url(), wp_kses(), etc.
 *
 * @package AmbassadorHub\Admin
 */

namespace AmbassadorHub\Admin;

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

/**
 * Renders the shared page chrome for AmbassadorHub admin pages.
 */
final class AdminPage {

	/**
	 * Render the page header and enqueue shared assets.
	 *
	 * @param string $title_html    Page title — caller-escaped HTML.
	 * @param string $subtitle_html Page subtitle — caller-escaped HTML. '' omits the element.
	 * @param string $action_html   Optional action element (button/link) — caller-sanitised HTML.
	 * @return void
	 */
	public static function header(
		string $title_html,
		string $subtitle_html = '',
		string $action_html = ''
	): void {
		wp_enqueue_style( 'ambh-admin' );
		?>
		<div class="ambh-admin-header">
			<div class="ambh-admin-header__inner">
				<img src="<?php echo esc_url( AMBASSADOR_HUB_URL . 'assets/images/logo-icon.png' ); ?>" alt="" class="ambh-admin-header__logo-icon">
				<div class="ambh-admin-header__text">
					<h1 class="ambh-admin-header__title"><?php echo $title_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></h1>
					<?php if ( '' !== $subtitle_html ) : ?>
					<p class="ambh-admin-header__subtitle"><?php echo $subtitle_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
					<?php endif; ?>
				</div>
				<?php if ( '' !== $action_html ) : ?>
					<div class="ambh-admin-header__action">
						<?php echo $action_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					</div>
				<?php endif; ?>
			</div>
		</div>
		<?php
	}

	/**
	 * Check whether the current admin request is for the given page slug.
	 *
	 * Reads $_GET['page'] and compares it to $slug. Safe to call anywhere;
	 * callers do not need their own sanitise/nonce guard for this check.
	 *
	 * @param string $slug The page slug, e.g. 'ambassadorhub-donors'.
	 * @return bool
	 */
	public static function is_page( string $slug ): bool {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : '';
		return $slug === $page;
	}
}
