<?php
/**
 * Admin view helpers.
 *
 * @package SubmissionInbox
 */

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

/**
 * Small helpers for admin page markup.
 */
class SUBIN_View {
	/**
	 * Open a standard admin page.
	 *
	 * @param string              $title Page title.
	 * @param string              $description Page description.
	 * @param array<string,mixed> $args Header options.
	 * @return void
	 */
	public static function page_header( $title, $description, array $args = array() ) {
		$primary_action    = isset( $args['primary_action'] ) && is_array( $args['primary_action'] ) ? $args['primary_action'] : array();
		$secondary_actions = isset( $args['secondary_actions'] ) && is_array( $args['secondary_actions'] ) ? $args['secondary_actions'] : array();
		$badges            = isset( $args['badges'] ) && is_array( $args['badges'] ) ? $args['badges'] : array();
		$wrapper_class     = isset( $args['wrapper_class'] ) ? self::sanitize_class_list( (string) $args['wrapper_class'] ) : '';
		$icon_url          = defined( 'SUBIN_PLUGIN_URL' ) ? SUBIN_PLUGIN_URL . 'assets/icon.png' : '';
		?>
		<div class="wrap subin-admin-page<?php echo $wrapper_class ? ' ' . esc_attr( $wrapper_class ) : ''; ?>">
			<div class="subin-admin-hero subin-admin-hero--compact">
				<div class="subin-admin-hero__main">
					<?php if ( '' !== $icon_url ) : ?>
						<div class="subin-admin-hero__icon" aria-hidden="true">
							<img class="subin-admin-hero__icon-img" src="<?php echo esc_url( $icon_url ); ?>" alt="" aria-hidden="true" />
						</div>
					<?php endif; ?>
					<div class="subin-admin-hero__content">
						<h1><?php echo esc_html( $title ); ?></h1>
						<p class="subin-page-description"><?php echo esc_html( $description ); ?></p>
						<?php if ( ! empty( $badges ) ) : ?>
							<div class="subin-admin-hero__badges">
								<?php foreach ( $badges as $badge ) : ?>
									<?php
									$badge_label = is_array( $badge ) ? ( $badge['label'] ?? '' ) : $badge;
									$badge_type  = is_array( $badge ) ? ( $badge['type'] ?? 'muted' ) : 'muted';
									self::badge( (string) $badge_label, (string) $badge_type );
									?>
								<?php endforeach; ?>
							</div>
						<?php endif; ?>
					</div>
				</div>
				<?php if ( ! empty( $primary_action ) || ! empty( $secondary_actions ) ) : ?>
					<div class="subin-admin-hero__actions">
						<?php if ( ! empty( $primary_action['label'] ) && ! empty( $primary_action['url'] ) ) : ?>
							<a class="button button-primary subin-btn subin-btn-primary" href="<?php echo esc_url( $primary_action['url'] ); ?>"><?php echo esc_html( $primary_action['label'] ); ?></a>
						<?php endif; ?>
						<?php foreach ( $secondary_actions as $action ) : ?>
							<?php if ( empty( $action['label'] ) || empty( $action['url'] ) ) : ?>
								<?php continue; ?>
							<?php endif; ?>
							<a class="button subin-btn subin-btn-secondary" href="<?php echo esc_url( $action['url'] ); ?>"><?php echo esc_html( $action['label'] ); ?></a>
						<?php endforeach; ?>
					</div>
				<?php endif; ?>
			</div>
		<?php
	}

	/**
	 * Close a standard admin page.
	 *
	 * @return void
	 */
	public static function page_footer() {
		?>
		</div>
		<?php
	}

	/**
	 * Sanitize a space-separated class list.
	 *
	 * @param string $classes Class list.
	 * @return string
	 */
	private static function sanitize_class_list( $classes ) {
		$classes = preg_split( '/\s+/', trim( $classes ) );
		if ( ! is_array( $classes ) ) {
			return '';
		}

		$clean = array();
		foreach ( $classes as $class ) {
			$class = sanitize_html_class( $class );
			if ( '' !== $class ) {
				$clean[] = $class;
			}
		}

		return implode( ' ', array_unique( $clean ) );
	}

	/**
	 * Render admin page tabs.
	 *
	 * @param array<int,array<string,string>> $tabs Tabs.
	 * @param string                          $active Active tab key.
	 * @return void
	 */
	public static function admin_tabs( array $tabs, $active ) {
		if ( empty( $tabs ) ) {
			return;
		}
		?>
		<nav class="subin-admin-tabs" aria-label="<?php echo esc_attr__( 'Section navigation', 'submission-inbox' ); ?>">
			<?php foreach ( $tabs as $tab ) : ?>
				<?php
				$key   = isset( $tab['key'] ) ? sanitize_key( $tab['key'] ) : '';
				$label = isset( $tab['label'] ) ? (string) $tab['label'] : '';
				$url   = isset( $tab['url'] ) ? (string) $tab['url'] : '';

				if ( '' === $key || '' === $label || '' === $url ) {
					continue;
				}
				?>
				<a class="subin-admin-tab<?php echo $key === $active ? ' subin-admin-tab--active' : ''; ?>" href="<?php echo esc_url( $url ); ?>"<?php echo $key === $active ? ' aria-current="page"' : ''; ?>>
					<?php echo esc_html( $label ); ?>
				</a>
			<?php endforeach; ?>
		</nav>
		<?php
	}

	/**
	 * Render a status badge.
	 *
	 * @param string $label Badge label.
	 * @param string $type Badge type.
	 * @return void
	 */
	public static function badge( $label, $type = 'muted' ) {
		$allowed = array( 'success', 'warning', 'danger', 'muted', 'info' );
		$type    = in_array( $type, $allowed, true ) ? $type : 'muted';

		printf(
			'<span class="subin-badge subin-badge--%1$s">%2$s</span>',
			esc_attr( $type ),
			esc_html( $label )
		);
	}

	/**
	 * Render a compact contextual help tooltip.
	 *
	 * @param string $text Tooltip text.
	 * @param string $label Accessible label.
	 * @return void
	 */
	public static function help_tooltip( $text, $label = '' ) {
		$text  = trim( (string) $text );
		$label = '' !== trim( (string) $label ) ? (string) $label : __( 'Help', 'submission-inbox' );

		if ( '' === $text ) {
			return;
		}
		?>
		<span class="subin-help" tabindex="0" role="note" aria-label="<?php echo esc_attr( $label . ': ' . $text ); ?>" data-subin-help-text="<?php echo esc_attr( $text ); ?>">
			<span class="subin-help__icon" aria-hidden="true">?</span>
			<span class="subin-help__tooltip"><?php echo esc_html( $text ); ?></span>
		</span>
		<?php
	}

	/**
	 * Render an empty state.
	 *
	 * @param string $title Empty state title.
	 * @param string $text Empty state text.
	 * @return void
	 */
	public static function empty_state( $title, $text ) {
		?>
		<div class="subin-empty-state">
			<h2><?php echo esc_html( $title ); ?></h2>
			<p><?php echo esc_html( $text ); ?></p>
		</div>
		<?php
	}

	/**
	 * Render a simple coming-next panel.
	 *
	 * @param string $heading Panel heading.
	 * @param array<int,string> $items Notes.
	 * @return void
	 */
	public static function coming_next( $heading, array $items ) {
		?>
		<div class="subin-panel">
			<h2><?php echo esc_html( $heading ); ?></h2>
			<ul class="subin-coming-next">
				<?php foreach ( $items as $item ) : ?>
					<li><?php echo esc_html( $item ); ?></li>
				<?php endforeach; ?>
			</ul>
		</div>
		<?php
	}

	/**
	 * Render a checkbox field.
	 *
	 * @param string $name Field name.
	 * @param string $label Field label.
	 * @param bool   $checked Checked state.
	 * @param string $description Optional description.
	 * @param bool   $disabled Disabled state.
	 * @return void
	 */
	public static function checkbox_field( $name, $label, $checked, $description = '', $disabled = false ) {
		?>
		<label class="subin-settings-toggle<?php echo $disabled ? ' subin-settings-toggle--disabled' : ''; ?>">
			<input type="checkbox" name="<?php echo esc_attr( $name ); ?>" value="1" <?php checked( $checked ); ?> <?php disabled( $disabled ); ?> />
			<span></span>
			<strong><?php echo esc_html( $label ); ?></strong>
		</label>
		<?php if ( '' !== $description ) : ?>
			<p class="description"><?php echo esc_html( $description ); ?></p>
		<?php endif; ?>
		<?php
	}
}
