<?php
/**
 * Abilities Catalog Page Template
 *
 * Showcases all available MCP abilities for AI assistants.
 *
 * @package Specflux_Marketing_Analytics
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Build the abilities list.
 *
 * Uses wp_get_registered_abilities() if available (WordPress 7.0+),
 * otherwise falls back to a static list of known abilities.
 */
$mac_abilities = array();

if ( function_exists( 'wp_get_registered_abilities' ) ) {
	$registered = wp_get_registered_abilities();
	if ( is_array( $registered ) ) {
		foreach ( $registered as $ability ) {
			$mac_abilities[] = array(
				'name'        => isset( $ability['name'] ) ? $ability['name'] : '',
				'slug'        => isset( $ability['slug'] ) ? $ability['slug'] : '',
				'description' => isset( $ability['description'] ) ? $ability['description'] : '',
				'category'    => isset( $ability['category'] ) ? $ability['category'] : 'other',
				'tier'        => isset( $ability['tier'] ) ? $ability['tier'] : 'free',
			);
		}
	}
}

// Fallback: static list if Abilities API not available or returned nothing.
if ( empty( $mac_abilities ) ) {
	$mac_abilities = array(
		// GA4 abilities.
		array(
			'name'        => __( 'Get GA4 Metrics', 'specflux-marketing-analytics-chat' ),
			'slug'        => 'marketing-analytics/get-ga4-metrics',
			'description' => __( 'Retrieve traffic, user, and conversion metrics from Google Analytics 4.', 'specflux-marketing-analytics-chat' ),
			'category'    => 'ga4',
			'tier'        => 'free',
			'icon'        => 'dashicons-chart-line',
		),
		array(
			'name'        => __( 'Get GA4 Realtime', 'specflux-marketing-analytics-chat' ),
			'slug'        => 'marketing-analytics/get-ga4-realtime',
			'description' => __( 'Get real-time active users and pageviews from Google Analytics 4.', 'specflux-marketing-analytics-chat' ),
			'category'    => 'ga4',
			'tier'        => 'free',
			'icon'        => 'dashicons-chart-line',
		),
		array(
			'name'        => __( 'Get GA4 Audience', 'specflux-marketing-analytics-chat' ),
			'slug'        => 'marketing-analytics/get-ga4-audience',
			'description' => __( 'Analyze audience demographics, devices, and geographic distribution.', 'specflux-marketing-analytics-chat' ),
			'category'    => 'ga4',
			'tier'        => 'free',
			'icon'        => 'dashicons-chart-line',
		),

		// GSC abilities.
		array(
			'name'        => __( 'Get Search Performance', 'specflux-marketing-analytics-chat' ),
			'slug'        => 'marketing-analytics/get-search-performance',
			'description' => __( 'Query search analytics for clicks, impressions, CTR, and average position.', 'specflux-marketing-analytics-chat' ),
			'category'    => 'gsc',
			'tier'        => 'free',
			'icon'        => 'dashicons-search',
		),
		array(
			'name'        => __( 'Get Top Queries', 'specflux-marketing-analytics-chat' ),
			'slug'        => 'marketing-analytics/get-top-queries',
			'description' => __( 'Retrieve top-performing search queries by clicks or impressions.', 'specflux-marketing-analytics-chat' ),
			'category'    => 'gsc',
			'tier'        => 'free',
			'icon'        => 'dashicons-search',
		),
		array(
			'name'        => __( 'Get Page Performance', 'specflux-marketing-analytics-chat' ),
			'slug'        => 'marketing-analytics/get-page-performance',
			'description' => __( 'Analyze individual page search performance and keyword rankings.', 'specflux-marketing-analytics-chat' ),
			'category'    => 'gsc',
			'tier'        => 'free',
			'icon'        => 'dashicons-search',
		),

		// Clarity abilities.
		array(
			'name'        => __( 'Get Clarity Insights', 'specflux-marketing-analytics-chat' ),
			'slug'        => 'marketing-analytics/get-clarity-insights',
			'description' => __( 'Retrieve session recordings, heatmap data, and user behavior insights.', 'specflux-marketing-analytics-chat' ),
			'category'    => 'clarity',
			'tier'        => 'free',
			'icon'        => 'dashicons-chart-area',
		),
		array(
			'name'        => __( 'Get Clarity Metrics', 'specflux-marketing-analytics-chat' ),
			'slug'        => 'marketing-analytics/get-clarity-metrics',
			'description' => __( 'Get dead clicks, rage clicks, scroll depth, and engagement metrics.', 'specflux-marketing-analytics-chat' ),
			'category'    => 'clarity',
			'tier'        => 'free',
			'icon'        => 'dashicons-chart-area',
		),

		// Cross-platform abilities.
		array(
			'name'        => __( 'Cross-Platform Summary', 'specflux-marketing-analytics-chat' ),
			'slug'        => 'marketing-analytics/cross-platform-summary',
			'description' => __( 'Get a unified summary combining data from all connected platforms.', 'specflux-marketing-analytics-chat' ),
			'category'    => 'cross-platform',
			'tier'        => 'free',
			'icon'        => 'dashicons-networking',
		),
		array(
			'name'        => __( 'Compare Platforms', 'specflux-marketing-analytics-chat' ),
			'slug'        => 'marketing-analytics/compare-platforms',
			'description' => __( 'Compare metrics across platforms to identify correlations and discrepancies.', 'specflux-marketing-analytics-chat' ),
			'category'    => 'cross-platform',
			'tier'        => 'free',
			'icon'        => 'dashicons-networking',
		),

	);
}

$mac_mcp_endpoint = rest_url( 'mcp/mcp-adapter-default-server' );
?>

<div class="wrap smac-abilities-catalog">
	<h1><?php esc_html_e( 'MCP Abilities Catalog', 'specflux-marketing-analytics-chat' ); ?></h1>
	<p class="description">
		<?php esc_html_e( 'All the analytics abilities available to your AI assistants via the Model Context Protocol. No competitor has MCP-native architecture.', 'specflux-marketing-analytics-chat' ); ?>
	</p>

	<!-- Category filter -->
	<div class="smac-abilities-filter">
		<button type="button" class="smac-abilities-filter-btn active" data-filter="all">
			<?php esc_html_e( 'All', 'specflux-marketing-analytics-chat' ); ?>
		</button>
		<button type="button" class="smac-abilities-filter-btn" data-filter="ga4">
			<?php esc_html_e( 'Google Analytics', 'specflux-marketing-analytics-chat' ); ?>
		</button>
		<button type="button" class="smac-abilities-filter-btn" data-filter="gsc">
			<?php esc_html_e( 'Search Console', 'specflux-marketing-analytics-chat' ); ?>
		</button>
		<button type="button" class="smac-abilities-filter-btn" data-filter="clarity">
			<?php esc_html_e( 'Clarity', 'specflux-marketing-analytics-chat' ); ?>
		</button>
		<button type="button" class="smac-abilities-filter-btn" data-filter="cross-platform">
			<?php esc_html_e( 'Cross-Platform', 'specflux-marketing-analytics-chat' ); ?>
		</button>
	</div>

	<!-- Abilities grid -->
	<div class="smac-abilities-grid">
		<?php foreach ( $mac_abilities as $mac_ability ) : ?>
			<?php
			$mac_category = isset( $mac_ability['category'] ) ? $mac_ability['category'] : 'other';
			$mac_tier     = isset( $mac_ability['tier'] ) ? $mac_ability['tier'] : 'free';
			$mac_icon     = isset( $mac_ability['icon'] ) ? $mac_ability['icon'] : 'dashicons-admin-generic';
			?>
			<div class="smac-ability-card" data-category="<?php echo esc_attr( $mac_category ); ?>">
				<div class="smac-ability-icon">
					<span class="dashicons <?php echo esc_attr( $mac_icon ); ?>"></span>
				</div>
				<h3><?php echo esc_html( $mac_ability['name'] ); ?></h3>
				<p><?php echo esc_html( $mac_ability['description'] ); ?></p>
				<div class="smac-ability-meta">
					<span class="smac-ability-status free">
						<?php esc_html_e( 'Free', 'specflux-marketing-analytics-chat' ); ?>
					</span>
					<code><?php echo esc_html( $mac_ability['slug'] ); ?></code>
				</div>
			</div>
		<?php endforeach; ?>
	</div>

	<!-- MCP endpoint info -->
	<div class="smac-abilities-mcp-info">
		<h3><?php esc_html_e( 'Connect Your AI Assistant', 'specflux-marketing-analytics-chat' ); ?></h3>
		<p><?php esc_html_e( 'Any MCP-compatible AI assistant can use these abilities. Add this endpoint to your assistant configuration:', 'specflux-marketing-analytics-chat' ); ?></p>
		<div class="mcp-endpoint-box">
			<code class="mcp-endpoint"><?php echo esc_url( $mac_mcp_endpoint ); ?></code>
			<button type="button" class="button button-secondary copy-endpoint">
				<?php esc_html_e( 'Copy URL', 'specflux-marketing-analytics-chat' ); ?>
			</button>
		</div>
		<p class="description">
			<?php
			printf(
				/* translators: %s: link to documentation */
				esc_html__( 'Works with Claude Desktop, ChatGPT, Cursor, and any MCP-compatible client. See our %s for setup guides.', 'specflux-marketing-analytics-chat' ),
				'<a href="https://github.com/specflux/marketing-analytics-chat#configuration" target="_blank" rel="noopener noreferrer">' . esc_html__( 'documentation', 'specflux-marketing-analytics-chat' ) . '</a>'
			);
			?>
		</p>
	</div>
</div>
