<?php
/**
 * Shared page hero partial.
 *
 * @package WP_PowerSuite\Admin\Views
 * @var array<string, mixed> $args
 */

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

$extra_class = trim( (string) ( $args['extra_class'] ?? '' ) );
$has_aside   = ! empty( $args['actions'] ) || ! empty( $args['stats'] ) || ! empty( $args['meta_html'] );
?>
<header class="wpps-settings-hero wpps-page-hero <?php echo esc_attr( $extra_class ); ?>">
	<div class="wpps-page-hero__layout">
		<div class="wpps-page-hero__brand">
			<?php if ( ! empty( $args['logo_url'] ) ) : ?>
				<div class="wpps-page-hero__logo-wrap" aria-hidden="true">
					<img
						src="<?php echo esc_url( (string) $args['logo_url'] ); ?>"
						alt=""
						class="wpps-page-hero__logo"
						width="56"
						height="56"
						decoding="async"
						loading="eager"
					/>
				</div>
			<?php elseif ( ! empty( $args['icon'] ) ) : ?>
				<div class="wpps-page-hero__icon-wrap" aria-hidden="true">
					<span class="dashicons <?php echo esc_attr( (string) $args['icon'] ); ?>"></span>
				</div>
			<?php endif; ?>
			<div class="wpps-page-hero__copy">
				<?php if ( ! empty( $args['eyebrow'] ) ) : ?>
					<span class="wpps-page-hero__eyebrow"><?php echo esc_html( (string) $args['eyebrow'] ); ?></span>
				<?php endif; ?>
				<div class="wpps-page-hero__title-row">
					<h1><?php echo esc_html( (string) $args['title'] ); ?></h1>
					<?php if ( ! empty( $args['status'] ) ) : ?>
						<span class="wpps-page-hero__status<?php echo ! empty( $args['status_inactive'] ) ? ' wpps-page-hero__status--inactive' : ''; ?>">
							<?php echo esc_html( (string) $args['status'] ); ?>
						</span>
					<?php endif; ?>
				</div>
				<?php if ( ! empty( $args['tagline'] ) ) : ?>
					<p class="wpps-page-hero__tagline"><?php echo esc_html( (string) $args['tagline'] ); ?></p>
				<?php endif; ?>
			</div>
		</div>
		<?php if ( $has_aside ) : ?>
			<aside class="wpps-page-hero__aside">
				<?php if ( ! empty( $args['actions'] ) && is_array( $args['actions'] ) ) : ?>
					<nav class="wpps-page-hero__actions" aria-label="<?php echo esc_attr( sprintf( __( '%s shortcuts', 'wp-powersuite' ), \WP_PowerSuite\Core\WhiteLabel::pluginName() ) ); ?>">
						<?php foreach ( $args['actions'] as $action ) : ?>
							<?php
							if ( empty( $action['url'] ) || empty( $action['label'] ) ) {
								continue;
							}
							$icon = ! empty( $action['icon'] ) ? (string) $action['icon'] : '';
							?>
							<a href="<?php echo esc_url( (string) $action['url'] ); ?>" class="wpps-page-hero__action">
								<?php if ( $icon !== '' ) : ?>
									<span class="dashicons <?php echo esc_attr( $icon ); ?>" aria-hidden="true"></span>
								<?php endif; ?>
								<?php echo esc_html( (string) $action['label'] ); ?>
							</a>
						<?php endforeach; ?>
					</nav>
				<?php endif; ?>
				<?php if ( ! empty( $args['meta_html'] ) ) : ?>
					<div class="wpps-page-hero__meta">
						<?php
						// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Caller supplies trusted admin markup (buttons).
						echo $args['meta_html'];
						?>
					</div>
				<?php endif; ?>
				<?php if ( ! empty( $args['stats'] ) && is_array( $args['stats'] ) ) : ?>
					<div class="wpps-page-hero__stats" role="group" aria-label="<?php esc_attr_e( 'Statistics', 'wp-powersuite' ); ?>">
						<?php foreach ( $args['stats'] as $stat ) : ?>
							<?php if ( empty( $stat['label'] ) ) { continue; } ?>
							<div class="wpps-page-hero__stat">
								<strong><?php echo esc_html( (string) ( $stat['value'] ?? '' ) ); ?></strong>
								<span><?php echo esc_html( (string) $stat['label'] ); ?></span>
							</div>
						<?php endforeach; ?>
					</div>
				<?php endif; ?>
			</aside>
		<?php endif; ?>
	</div>
</header>
