<?php
/**
 * Command palette module and navigation payloads for Quick Launcher.
 *
 * @package WP_PowerSuite\Core
 * @since 1.0.0
 */

namespace WP_PowerSuite\Core;

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

final class CommandPaletteData {

	/**
	 * Module rows searchable in the Quick Launcher.
	 *
	 * @return array<int, array<string, mixed>>
	 */
	public static function getModules(): array {
		$registry        = ModuleRegistry::getInstance();
		$registry->init();
		$option_store    = OptionStore::getInstance();
		$all_modules     = $registry->getAllModules();
		$enabled_modules = $option_store->getEnabledModules();
		$license_valid   = wp_powersuite_is_license_valid();

		$modules_for_palette = [];
		foreach ( $all_modules as $id => $meta ) {
			$is_pro = ! empty( $meta['is_pro'] );
			if ( ! $license_valid && $is_pro ) {
				continue;
			}
			$tags = isset( $meta['tags'] ) && is_array( $meta['tags'] ) ? implode( ' ', $meta['tags'] ) : '';
			$modules_for_palette[] = [
				'id'          => $id,
				'name'        => $meta['name'] ?? $id,
				'description' => $meta['description'] ?? '',
				'category'    => $meta['category'] ?? '',
				'enabled'     => in_array( $id, $enabled_modules, true ),
				'is_pro'      => $is_pro,
				'keywords'    => trim( ( $meta['name'] ?? '' ) . ' ' . ( $meta['description'] ?? '' ) . ' ' . ( $meta['category'] ?? '' ) . ' ' . $tags ),
			];
		}

		return $modules_for_palette;
	}

	/**
	 * PowerSuite admin destinations shown in the Quick Launcher.
	 *
	 * @return array<int, array<string, string>>
	 */
	public static function getNavItems(): array {
		$option_store    = OptionStore::getInstance();
		$enabled_modules = $option_store->getEnabledModules();

		$nav_items = [
			[
				'label'    => __( 'Go to Modules', 'wp-powersuite' ),
				'url'      => 'admin.php?page=powersuite',
				'keywords' => 'modules main dashboard',
			],
			[
				'label'    => __( 'Go to Settings', 'wp-powersuite' ),
				'url'      => 'admin.php?page=powersuite-settings',
				'keywords' => 'settings configurations presets import export',
			],
		];

		if ( $option_store->isPerformanceMonitorEnabled() ) {
			$nav_items[] = [
				'label'    => __( 'Performance Monitor', 'wp-powersuite' ),
				'url'      => 'admin.php?page=' . PerformanceBudgetConfig::PERFORMANCE_ADMIN_PAGE,
				'keywords' => 'performance dashboard metrics hooks monitor',
			];
		}

		if ( class_exists( 'WPPS_License_Manager', false ) && ! WhiteLabel::shouldHideLicenseMenu() ) {
			$nav_items[] = [
				'label'    => __( 'Go to License', 'wp-powersuite' ),
				'url'      => 'admin.php?page=' . WhiteLabel::LICENSE_PAGE_SLUG,
				'keywords' => 'license activate',
			];
		}

		if ( ! WhiteLabel::shouldHideWhiteLabelSettings() ) {
			$nav_items[] = [
				'label'    => __( 'White Label', 'wp-powersuite' ),
				'url'      => 'admin.php?page=powersuite-settings&tab=white-label',
				'keywords' => 'white label branding agency logo',
			];
		}

		if ( in_array( 'options-page-builder', $enabled_modules, true ) ) {
			$nav_items[] = [
				'label'    => __( 'Options Page Builder', 'wp-powersuite' ),
				'url'      => 'admin.php?page=powersuite-options-page-builder',
				'keywords' => 'options page builder opb custom admin',
			];
		}

		if ( in_array( 'basic-forms', $enabled_modules, true ) ) {
			$nav_items[] = [
				'label'    => __( 'Basic Forms', 'wp-powersuite' ),
				'url'      => 'admin.php?page=powersuite-basic-forms',
				'keywords' => 'forms entries contact lead',
			];
		}

		if ( in_array( 'coming-soon-maintenance', $enabled_modules, true ) ) {
			$nav_items[] = [
				'label'    => __( 'Coming Soon & Maintenance', 'wp-powersuite' ),
				'url'      => 'admin.php?page=powersuite-coming-soon-maintenance',
				'keywords' => 'coming soon maintenance countdown',
			];
		}

		if ( in_array( 'login-page-customizor', $enabled_modules, true ) ) {
			$nav_items[] = [
				'label'    => __( 'Login Page Customizer', 'wp-powersuite' ),
				'url'      => 'admin.php?page=powersuite-login-page-customizor',
				'keywords' => 'login customizer branding sign in wp-login',
			];
		}

		return $nav_items;
	}

	/**
	 * Full Quick Launcher payload for AJAX responses.
	 *
	 * @return array<string, mixed>
	 */
	public static function getPayload(): array {
		return [
			'modules'  => self::getModules(),
			'navItems' => self::getNavItems(),
		];
	}
}
