<?php
/**
 * Soft-dependency probe — admin notice when the Abilities API or MCP Adapter
 * isn't available so the plugin can degrade visibly instead of silently.
 *
 * @package Ppok\AIToolkit
 * @license GPL-2.0-or-later
 */

namespace Ppok\AIToolkit\Admin;

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

final class Dependency_Notice {

	private const ABILITIES_API_FN     = 'wp_register_ability';
	private const MCP_ADAPTER_BASENAME = 'mcp-adapter/mcp-adapter.php';

	public function maybe_render_notice(): void {
		if ( ! current_user_can( 'activate_plugins' ) ) {
			return;
		}

		$missing = $this->missing_dependencies();
		if ( empty( $missing ) ) {
			return;
		}

		printf(
			'<div class="notice notice-warning is-dismissible"><p><strong>%s</strong></p><ul style="list-style:disc;margin-left:1.5em;">',
			esc_html__( 'Ppok AI Toolkit needs the following to function:', 'ppok-ai-toolkit' )
		);

		foreach ( $missing as $dep ) {
			printf(
				'<li><a href="%s" target="_blank" rel="noopener noreferrer"><strong>%s</strong></a> — %s</li>',
				esc_url( $dep['url'] ),
				esc_html( $dep['name'] ),
				esc_html( $dep['reason'] )
			);
		}

		echo '</ul></div>';
	}

	private function missing_dependencies(): array {
		$missing = array();

		if ( ! function_exists( self::ABILITIES_API_FN ) ) {
			$missing[] = array(
				'name'   => __( 'WordPress Abilities API', 'ppok-ai-toolkit' ),
				'url'    => 'https://github.com/WordPress/abilities-api',
				'reason' => __( 'Ppok abilities can\'t register without it.', 'ppok-ai-toolkit' ),
			);
		}

		if ( ! function_exists( 'is_plugin_active' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		if ( ! is_plugin_active( self::MCP_ADAPTER_BASENAME ) ) {
			$missing[] = array(
				'name'   => __( 'WordPress MCP Adapter', 'ppok-ai-toolkit' ),
				'url'    => 'https://github.com/WordPress/mcp-adapter',
				'reason' => __( 'AI clients won\'t reach the abilities without it.', 'ppok-ai-toolkit' ),
			);
		}

		return $missing;
	}
}
