<?php
/**
 * Admin Notices
 *
 * Handles admin notices for conflicts, connector detection, activation,
 * and review prompts. Uses styled notice cards with badges and icons.
 *
 * @package SEOvaultSEO
 */

namespace SEOvaultSEO\Admin;

use SEOvaultSEO\Compatibility\Detector;
use SEOvaultSEO\Core\Options;

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

class Notices {

	private static $instance = null;

	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {}

	public static function init() {
		$instance = self::get_instance();

		// Global operational notices — remain in the admin_notices stream.
		add_action( 'admin_notices', array( __CLASS__, 'show_conflict_notice' ), 5 );
		add_action( 'admin_notices', array( __CLASS__, 'show_disabled_notice' ), 6 );
		add_action( 'admin_notices', array( __CLASS__, 'show_activation_notice' ), 35 );

		// Contextual notices (review, schema onboarding, legacy connector) are
		// now owned by the Action Center and removed from the global stream.
		// Their methods remain as public static for direct invocation and
		// existing test compatibility.

		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
		add_action( 'wp_ajax_seovaultseo_notice_response', array( __CLASS__, 'handle_notice_response' ) );
	}

	public static function enqueue_assets() {
		wp_enqueue_style(
			'seovaultseo-notices',
			SEOVAULT_SEO_URL . 'assets/css/notices.css',
			array(),
			SEOVAULT_SEO_VERSION . '-8'
		);
		wp_enqueue_script(
			'seovaultseo-notices',
			SEOVAULT_SEO_URL . 'assets/js/notices.js',
			array(),
			SEOVAULT_SEO_VERSION,
			true
		);
		wp_localize_script(
			'seovaultseo-notices',
			'seovaultseo_notices',
			array(
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'seovaultseo_admin' ),
			)
		);
	}

	public static function show_activation_notice() {
		if ( ! get_transient( 'seovaultseo_activated' ) ) {
			return;
		}
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		delete_transient( 'seovaultseo_activated' );

		$cta        = \SEOvaultSEO\Schema\SchemaOnboardingGuidance::activation_cta();
		$basics     = \SEOvaultSEO\Schema\SchemaOnboardingGuidance::automatic_basics_copy();
		$additional = \SEOvaultSEO\Schema\SchemaOnboardingGuidance::additional_schema_copy();
		$competitor = \SEOvaultSEO\Schema\SchemaOnboardingGuidance::competitor_notice_copy();
		$is_upgrade = \SEOvaultSEO\Schema\SchemaOnboardingGuidance::should_route_to_migration();
		?>
		<div class="notice notice-success is-dismissible seovaultseo-notice seovaultseo-notice-success seovaultseo-notice-activation">
			<div class="seovaultseo-notice-icon">
				<span class="dashicons dashicons-yes-alt"></span>
			</div>
			<div class="seovaultseo-notice-content">
				<p>
					<strong>
						<?php
						echo $is_upgrade
							? esc_html__( 'SEOvault AI SEO plugin ready — schema setup was updated', 'seovault-ai' )
							: esc_html__( 'SEOvault AI SEO plugin activated!', 'seovault-ai' );
						?>
					</strong>
				</p>
				<p><?php echo esc_html( $basics ); ?></p>
				<p><?php echo esc_html( $additional ); ?></p>
				<?php if ( '' !== $competitor ) : ?>
					<p><?php echo esc_html( $competitor ); ?></p>
				<?php endif; ?>
				<p>
					<a href="<?php echo esc_url( $cta['url'] ); ?>" class="button button-primary">
						<?php echo esc_html( $cta['label'] ); ?>
					</a>
					<?php if ( ! empty( $cta['secondary_url'] ) ) : ?>
						<a href="<?php echo esc_url( $cta['secondary_url'] ); ?>" class="button button-secondary">
							<?php echo esc_html( $cta['secondary_label'] ); ?>
						</a>
					<?php endif; ?>
				</p>
			</div>
		</div>
		<?php
	}

	/**
	 * Short schema guidance notice for fresh installs still missing identity confirmation.
	 *
	 * @return void
	 */
	public static function show_schema_onboarding_notice() {
		if ( ! \SEOvaultSEO\Schema\SchemaOnboardingGuidance::should_show_schema_notice() ) {
			return;
		}

		$basics     = \SEOvaultSEO\Schema\SchemaOnboardingGuidance::automatic_basics_copy();
		$additional = \SEOvaultSEO\Schema\SchemaOnboardingGuidance::additional_schema_copy();
		$schema_url = admin_url( 'admin.php?page=seovault-ai-general&tab=vtab-schema' );
		$dismiss    = wp_nonce_url(
			admin_url( 'admin-post.php?action=' . \SEOvaultSEO\Schema\SchemaOnboardingGuidance::ACTION_DISMISS ),
			\SEOvaultSEO\Schema\SchemaOnboardingGuidance::NONCE_DISMISS
		);
		?>
		<div class="notice notice-info seovaultseo-notice seovaultseo-notice-schema-onboarding" role="status">
			<div class="seovaultseo-notice-icon">
				<span class="dashicons dashicons-list-view" aria-hidden="true"></span>
			</div>
			<div class="seovaultseo-notice-content">
				<p>
					<strong><?php esc_html_e( 'Confirm site identity for automatic schema', 'seovault-ai' ); ?></strong>
				</p>
				<p><?php echo esc_html( $basics ); ?></p>
				<p><?php echo esc_html( $additional ); ?></p>
				<p>
					<a href="<?php echo esc_url( $schema_url ); ?>" class="button button-primary">
						<?php esc_html_e( 'Choose Organization or Person', 'seovault-ai' ); ?>
					</a>
					<a href="<?php echo esc_url( $dismiss ); ?>" class="button button-link">
						<?php esc_html_e( 'Dismiss', 'seovault-ai' ); ?>
					</a>
				</p>
			</div>
		</div>
		<?php
	}

	public static function show_conflict_notice() {
		// Keep the migration source active and the user inside the wizard until
		// they reach the dedicated import step.
		if ( Onboarding::is_onboarding_screen() ) {
			return;
		}

		$conflicts = Detector::get_conflicts();
		if ( empty( $conflicts ) ) {
			return;
		}

		if ( ! Detector::should_output_seo() ) {
			return; // Disabled mode — no need to warn about duplicates.
		}

		$conflict_list = implode( ', ', $conflicts );
		?>
		<div class="notice notice-warning is-dismissible seovaultseo-notice seovaultseo-notice-warning">
			<div class="seovaultseo-notice-icon">
				<span class="dashicons dashicons-warning"></span>
			</div>
			<div class="seovaultseo-notice-content">
				<p>
					<strong><?php esc_html_e( 'SEOvault AI — Duplicate SEO Tags Warning', 'seovault-ai' ); ?></strong>
				</p>
				<p>
					<?php
					printf(
						/* translators: %s: list of other SEO plugins */
						esc_html__( 'The following SEO plugin(s) are also active: %s. Both SEOvault AI and these plugins may output duplicate meta tags, which can cause SEO issues.', 'seovault-ai' ),
						'<strong>' . esc_html( $conflict_list ) . '</strong>'
					);
					?>
				</p>
				<p>
					<?php esc_html_e( 'Recommended: Import all needed SEO data while the other plugin is still active. After importing, deactivate it to prevent duplicate output, or set SEOvault AI to Disabled mode if you want to keep using the other plugin.', 'seovault-ai' ); ?>
				</p>
				<p>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=seovault-ai-general&tab=vtab-tools' ) ); ?>" class="button button-primary">
						<?php esc_html_e( 'Import SEO Data', 'seovault-ai' ); ?>
					</a>
					<a href="<?php echo esc_url( admin_url( 'plugins.php' ) ); ?>" class="button button-secondary">
						<?php esc_html_e( 'Manage Plugins', 'seovault-ai' ); ?>
					</a>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=seovault-ai-general' ) ); ?>" class="button button-secondary">
						<?php esc_html_e( 'SEOvault Output Mode', 'seovault-ai' ); ?>
					</a>
				</p>
			</div>
		</div>
		<?php
	}

	public static function show_disabled_notice() {
		if ( Detector::should_output_seo() ) {
			return;
		}
		?>
		<div class="notice notice-warning seovaultseo-notice seovaultseo-notice-disabled">
			<div class="seovaultseo-notice-icon">
				<span class="dashicons dashicons-warning"></span>
			</div>
			<div class="seovaultseo-notice-content">
				<p>
					<strong><?php esc_html_e( 'SEOvault AI is disabled.', 'seovault-ai' ); ?></strong>
					<?php esc_html_e( 'No SEO output is being generated. Go to General > Output Mode to activate SEOvault AI.', 'seovault-ai' ); ?>
				</p>
				<p>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=seovault-ai-general' ) ); ?>" class="button button-primary">
						<?php esc_html_e( 'Activate SEOvault AI', 'seovault-ai' ); ?>
					</a>
				</p>
			</div>
		</div>
		<?php
	}

	public static function show_old_connector_notice() {
		if ( ! Detector::is_old_connector_active() ) {
			return;
		}
		?>
		<div class="notice notice-info is-dismissible seovaultseo-notice seovaultseo-notice-info">
			<div class="seovaultseo-notice-icon">
				<span class="dashicons dashicons-info"></span>
			</div>
			<div class="seovaultseo-notice-content">
				<p>
					<strong><?php esc_html_e( 'SEOvault AI Connector Detected', 'seovault-ai' ); ?></strong>
					<span class="seovaultseo-badge seovaultseo-badge-info seovaultseo-badge-inline">
						<?php esc_html_e( 'Legacy', 'seovault-ai' ); ?>
					</span>
				</p>
				<p>
					<?php
					esc_html_e( 'The SEOvault AI Connector plugin is still active. SEOvault AI now includes all connector functionality, so you can safely deactivate the old connector plugin. Your site key and connection settings have been preserved.', 'seovault-ai' );
					?>
				</p>
				<p>
					<a href="<?php echo esc_url( admin_url( 'plugins.php' ) ); ?>" class="button button-primary">
						<span class="dashicons dashicons-admin-plugins"></span>
						<?php esc_html_e( 'Manage Plugins', 'seovault-ai' ); ?>
					</a>
				</p>
			</div>
		</div>
		<?php
	}

	public static function show_review_notice() {
		if ( ! self::is_review_eligible() ) {
			return;
		}

		$dismissed = get_user_meta( get_current_user_id(), 'seovaultseo_review_dismissed', true );
		if ( 'yes' === $dismissed ) {
			return;
		}

		$snoozed = get_transient( 'seovaultseo_review_snooze_' . get_current_user_id() );
		if ( false !== $snoozed ) {
			return;
		}
		?>
		<div class="notice notice-info is-dismissible seovaultseo-notice seovaultseo-notice-review" data-notice-id="seovaultseo-review">
			<div class="seovaultseo-notice-icon seovaultseo-notice-icon-star">
				<span class="dashicons dashicons-star-filled"></span>
			</div>
			<div class="seovaultseo-notice-content">
				<p>
					<strong><?php esc_html_e( 'Enjoying SEOvault AI?', 'seovault-ai' ); ?></strong>
				</p>
				<p><?php esc_html_e( 'If SEOvault AI has been helpful, would you mind taking a moment to leave a 5-star review on WordPress.org?', 'seovault-ai' ); ?></p>
				<p>
					<a href="https://wordpress.org/support/plugin/seovault-ai-seo/reviews/#new-post" target="_blank" rel="noopener noreferrer" class="button button-primary seovaultseo-notice-action" data-action="rate">
						<span class="dashicons dashicons-star-filled"></span>
						<?php esc_html_e( 'Rate SEOvault AI', 'seovault-ai' ); ?>
					</a>
					<a href="#" class="button button-secondary seovaultseo-notice-action" data-action="snooze">
						<?php esc_html_e( 'Maybe later', 'seovault-ai' ); ?>
					</a>
					<a href="#" class="button button-secondary seovaultseo-notice-action" data-action="dismiss">
						<?php esc_html_e( 'I already did', 'seovault-ai' ); ?>
					</a>
				</p>
			</div>
		</div>
		<?php
	}

	public static function handle_notice_response() {
		check_ajax_referer( 'seovaultseo_admin', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => __( 'Unauthorized.', 'seovault-ai' ) ), 403 );
		}

		$action = isset( $_POST['notice_action'] ) ? sanitize_text_field( wp_unslash( $_POST['notice_action'] ) ) : '';

		if ( 'dismiss' === $action ) {
			update_user_meta( get_current_user_id(), 'seovaultseo_review_dismissed', 'yes' );
		} elseif ( 'snooze' === $action ) {
			set_transient( 'seovaultseo_review_snooze_' . get_current_user_id(), true, WEEK_IN_SECONDS );
		}

		wp_send_json_success();
	}

	public static function is_review_eligible() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return false;
		}

		if ( ! apply_filters( 'seovaultseo_show_review_notice', true ) ) {
			return false;
		}

		$installed_at = get_option( 'seovaultseo_installed_at', 0 );
		if ( empty( $installed_at ) ) {
			return false;
		}

		$days_active = ( time() - (int) $installed_at ) / DAY_IN_SECONDS;
		if ( $days_active < 7 ) {
			return false;
		}

		return true;
	}
}
