<?php
/**
 * Reusable admin page hero (Modules, Settings, License, module screens).
 *
 * @package WP_PowerSuite\Admin
 * @since 1.0.0
 */

namespace WP_PowerSuite\Admin;

use WP_PowerSuite\Core\NetworkScope;
use WP_PowerSuite\Core\WhiteLabel;

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

/**
 * Page hero renderer.
 */
class PageHero {

	/**
	 * Render the shared hero header.
	 *
	 * @param array<string, mixed> $args {
	 *     @type string               $eyebrow         Optional eyebrow label.
	 *     @type string               $title           Page title (required).
	 *     @type string               $tagline         Subtitle text.
	 *     @type string               $status          Optional status pill text.
	 *     @type bool                 $status_inactive Inactive status styling.
	 *     @type string               $logo_url        Optional logo image URL.
	 *     @type string               $icon            Dashicons class (e.g. dashicons-admin-generic).
	 *     @type array<int, array>     $stats           [ ['value' => '', 'label' => ''], ... ].
	 *     @type array<int, array>     $actions         [ ['label' => '', 'url' => '', 'icon' => ''], ... ].
	 *     @type string               $meta_html       Optional HTML in the aside (buttons, etc.).
	 *     @type string               $extra_class     Extra classes on <header>.
	 * }
	 * @return void
	 */
	public static function render( array $args ): void {
		$defaults = [
			'eyebrow'          => __( 'Control Center', 'wp-powersuite' ),
			'title'            => '',
			'tagline'          => '',
			'status'           => '',
			'status_inactive'  => false,
			'logo_url'         => '',
			'icon'             => '',
			'stats'            => [],
			'actions'          => [],
			'meta_html'        => '',
			'extra_class'      => '',
		];

		$args = wp_parse_args( $args, $defaults );

		if ( $args['title'] === '' ) {
			return;
		}

		$view = defined( 'WP_POWERSUITE_PLUGIN_DIR' )
			? WP_POWERSUITE_PLUGIN_DIR . 'src/Admin/Views/partials/page-hero.php'
			: dirname( __DIR__ ) . '/Admin/Views/partials/page-hero.php';

		if ( is_readable( $view ) ) {
			include $view;
		}
	}

	/**
	 * Hero for individual module admin screens (no Control Center eyebrow or hub nav).
	 *
	 * @param array<string, mixed> $args Same as render(); actions default to empty.
	 * @return void
	 */
	public static function render_for_module( array $args ): void {
		$args = wp_parse_args(
			$args,
			[
				'eyebrow' => '',
				'actions' => [],
			]
		);

		self::render( $args );
	}

	/**
	 * Default modules hub icon URL.
	 *
	 * @return string
	 */
	public static function default_icon_url(): string {
		return (string) apply_filters(
			'wp_powersuite_modules_hero_icon_url',
			BrandAssets::icon_url()
		);
	}

	/**
	 * Standard cross-links for sub-pages (Modules, Settings, License).
	 *
	 * @param string $omit Slug to omit: modules|settings|license.
	 * @return array<int, array<string, string>>
	 */
	public static function nav_actions( string $omit = '' ): array {
		$all = [];

		if ( $omit !== 'modules' ) {
			$all[] = [
				'label' => __( 'Modules', 'wp-powersuite' ),
				'url'   => NetworkScope::adminUrl( 'admin.php', [ 'page' => 'powersuite' ] ),
				'icon'  => 'dashicons-grid-view',
			];
		}

		if ( $omit !== 'settings' ) {
			$all[] = [
				'label' => __( 'Settings', 'wp-powersuite' ),
				'url'   => NetworkScope::adminUrl( 'admin.php', [ 'page' => 'powersuite-settings' ] ),
				'icon'  => 'dashicons-admin-generic',
			];
		}

		if ( $omit !== 'license' && class_exists( 'WPPS_License_Manager', false ) && \WP_PowerSuite\Core\NetworkScope::canManagePlugin() && ! WhiteLabel::shouldHideLicenseMenu() ) {
			$all[] = [
				'label' => __( 'License', 'wp-powersuite' ),
				'url'   => WhiteLabel::licenseDirectUrl(),
				'icon'  => 'dashicons-admin-network',
			];
		}

		return $all;
	}
}
