<?php
/**
 * Global Admin Banner
 *
 * Shows trial-related admin notices on ALL WordPress admin pages.
 * This drives users to the plugin from anywhere in the admin.
 *
 * @package Simple_AI_Auto_Post
 * @since 2.5.0
 */

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

/**
 * Render the global admin banner based on user scenario.
 *
 * This appears on ALL admin pages, not just the plugin page.
 * It's meant to be subtle for trial users and prominent for CTAs.
 */
function saiap_render_global_admin_banner() {
	// Only for users who can manage options.
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	$scenario = saiap_get_user_scenario();

	// Don't show banner for paid users (avoid clutter).
	if ( 'paid' === $scenario ) {
		return;
	}

	// Check if user dismissed the banner.
	$dismissed = get_user_meta( get_current_user_id(), 'saiap_global_banner_dismissed', true );
	if ( $dismissed && 'no_trial_no_keys' !== $scenario && 'expired_trial' !== $scenario ) {
		// For critical states (new user, expired), always show.
		return;
	}

	switch ( $scenario ) {
		case 'no_trial_no_keys':
			saiap_render_banner_no_trial_no_keys();
			break;

		case 'no_trial_has_keys':
			saiap_render_banner_no_trial_has_keys();
			break;

		case 'active_trial':
			saiap_render_banner_active_trial();
			break;

		case 'expired_trial':
			saiap_render_banner_expired_trial();
			break;
	}
}
add_action( 'admin_notices', 'saiap_render_global_admin_banner' );

/**
 * Banner for new users without trial or API keys.
 */
function saiap_render_banner_no_trial_no_keys() {
	// Don't show on plugin page - Dashboard handles trial CTA there.
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	if ( isset( $_GET['page'] ) && 'saiap' === $_GET['page'] ) {
		return;
	}

	$plugin_url = admin_url( 'admin.php?page=saiap&tab=dashboard' );
	?>
	<div class="notice notice-info saiap-global-banner saiap-global-banner--cta is-dismissible" data-scenario="no_trial_no_keys">
		<div class="saiap-global-banner__content">
			<span class="saiap-global-banner__icon dashicons dashicons-edit"></span>
			<div class="saiap-global-banner__text">
				<strong><?php esc_html_e( 'Organic Traffic on Autopilot', 'clearpost-simple-ai-auto-post' ); ?></strong>
				<span><?php esc_html_e( 'ClearPost writes and publishes SEO content for you — every day, without you lifting a finger.', 'clearpost-simple-ai-auto-post' ); ?></span>
			</div>
			<button type="button" class="button button-primary saiap-start-trial-btn">
				<?php esc_html_e( 'Get My Calendar', 'clearpost-simple-ai-auto-post' ); ?>
			</button>
		</div>
	</div>
	<?php
}

/**
 * Banner for users with API keys but no trial.
 */
function saiap_render_banner_no_trial_has_keys() {
	// Don't show on plugin page - Dashboard handles upgrade CTA there.
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	if ( isset( $_GET['page'] ) && 'saiap' === $_GET['page'] ) {
		return;
	}

	// Check if the "trial available after update" notice should show.
	$show_update_notice = saiap_should_show_trial_update_notice();

	if ( $show_update_notice ) {
		?>
		<div class="notice notice-info saiap-global-banner saiap-global-banner--upgrade is-dismissible" data-scenario="no_trial_has_keys">
			<div class="saiap-global-banner__content">
				<span class="saiap-global-banner__icon dashicons dashicons-awards"></span>
				<div class="saiap-global-banner__text">
					<strong><?php esc_html_e( 'New: Free Trial Available!', 'clearpost-simple-ai-auto-post' ); ?></strong>
					<span><?php esc_html_e( 'You\'re generating manually. Upgrade to automated publishing - free for 7 days.', 'clearpost-simple-ai-auto-post' ); ?></span>
				</div>
				<button type="button" class="button button-primary saiap-start-trial-btn">
					<?php esc_html_e( 'Start Free Trial', 'clearpost-simple-ai-auto-post' ); ?>
				</button>
			</div>
		</div>
		<?php
	} else {
		?>
		<div class="notice notice-info saiap-global-banner saiap-global-banner--upgrade is-dismissible" data-scenario="no_trial_has_keys">
			<div class="saiap-global-banner__content">
				<span class="saiap-global-banner__icon dashicons dashicons-admin-generic"></span>
				<div class="saiap-global-banner__text">
					<strong><?php esc_html_e( 'ClearPost', 'clearpost-simple-ai-auto-post' ); ?>:</strong>
					<span><?php esc_html_e( 'You\'re generating manually. Try automated publishing - free for 7 days.', 'clearpost-simple-ai-auto-post' ); ?></span>
				</div>
				<button type="button" class="button button-primary saiap-start-trial-btn">
					<?php esc_html_e( 'Start Free Trial', 'clearpost-simple-ai-auto-post' ); ?>
				</button>
			</div>
		</div>
		<?php
	}
}

/**
 * Banner for users with active trial (subtle status indicator).
 */
function saiap_render_banner_active_trial() {
	$days_remaining = saiap_get_trial_days_remaining();
	$total_days     = saiap_get_trial_total_days();
	$dashboard_url  = admin_url( 'admin.php?page=saiap&tab=dashboard' );
	$upgrade_url    = saiap_get_checkout_url();
	$current_day    = max( 1, $total_days - $days_remaining + 1 );

	// Only show on non-plugin pages (subtle reminder).
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	if ( isset( $_GET['page'] ) && 'saiap' === $_GET['page'] ) {
		return;
	}

	?>
	<div class="notice saiap-global-banner saiap-global-banner--status saiap-global-banner--trial-active">
		<div class="saiap-global-banner__content">
			<span class="saiap-global-banner__status-dot saiap-global-banner__status-dot--active"></span>
			<a href="<?php echo esc_url( $dashboard_url ); ?>" class="saiap-global-banner__status-link">
				<strong><?php esc_html_e( 'ClearPost Trial', 'clearpost-simple-ai-auto-post' ); ?>:</strong>
				<?php
				printf(
					/* translators: %1$d: current day number, %2$d: total trial days */
					esc_html__( 'Day %1$d of %2$d', 'clearpost-simple-ai-auto-post' ),
					absint( $current_day ),
					absint( $total_days )
				);
				?>
			</a>
			<span class="saiap-global-banner__separator">|</span>
			<a href="<?php echo esc_url( $upgrade_url ); ?>" class="saiap-global-banner__upgrade" target="_blank">
				<?php esc_html_e( 'Upgrade Now', 'clearpost-simple-ai-auto-post' ); ?>
			</a>
		</div>
	</div>
	<?php
}

/**
 * Banner for users with expired trial.
 */
function saiap_render_banner_expired_trial() {
	$pricing_url = saiap_get_checkout_url();
	?>
	<div class="notice notice-warning saiap-global-banner saiap-global-banner--expired">
		<div class="saiap-global-banner__content">
			<span class="saiap-global-banner__icon dashicons dashicons-warning"></span>
			<div class="saiap-global-banner__text">
				<strong><?php esc_html_e( 'Your ClearPost trial has ended', 'clearpost-simple-ai-auto-post' ); ?></strong>
				<span><?php esc_html_e( 'Resume automated publishing to keep your content engine running.', 'clearpost-simple-ai-auto-post' ); ?></span>
			</div>
			<a href="<?php echo esc_url( $pricing_url ); ?>" class="button button-primary" target="_blank">
				<?php esc_html_e( 'Subscribe Now', 'clearpost-simple-ai-auto-post' ); ?>
			</a>
		</div>
	</div>
	<?php
}

/**
 * AJAX handler for dismissing the global banner.
 */
function saiap_ajax_dismiss_global_banner() {
	check_ajax_referer( 'saiap_trial_nonce', 'nonce' );

	if ( ! current_user_can( 'manage_options' ) ) {
		wp_send_json_error( array( 'message' => __( 'Permission denied.', 'clearpost-simple-ai-auto-post' ) ) );
	}

	update_user_meta( get_current_user_id(), 'saiap_global_banner_dismissed', true );

	// Also dismiss the trial update notice if it was shown.
	saiap_dismiss_trial_update_notice();

	wp_send_json_success();
}
add_action( 'wp_ajax_saiap_dismiss_global_banner', 'saiap_ajax_dismiss_global_banner' );

/**
 * Enqueue global banner styles on all admin pages.
 */
function saiap_enqueue_global_banner_styles() {
	// Only for users who can manage options.
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	$scenario = saiap_get_user_scenario();

	// Don't load styles for paid users (no banner shown).
	if ( 'paid' === $scenario ) {
		return;
	}

	wp_enqueue_style(
		'saiap-global-banner',
		plugin_dir_url( dirname( __FILE__ ) ) . 'assets/css/global-banner.css',
		array(),
		defined( 'SAIAP_VERSION' ) ? SAIAP_VERSION : '1.0.0'
	);

	// Also enqueue the JS for trial start button.
	$js_file = plugin_dir_path( dirname( __FILE__ ) ) . 'assets/js/global-banner.js';
	wp_enqueue_script(
		'saiap-global-banner',
		plugin_dir_url( dirname( __FILE__ ) ) . 'assets/js/global-banner.js',
		array( 'jquery' ),
		file_exists( $js_file ) ? filemtime( $js_file ) : '1.0.0',
		true
	);

	wp_localize_script(
		'saiap-global-banner',
		'saiapGlobalBanner',
		array(
			'ajaxurl'    => admin_url( 'admin-ajax.php' ),
			'trialNonce' => wp_create_nonce( 'saiap_trial_nonce' ),
			'adminEmail' => get_option( 'admin_email', '' ),
		)
	);
}
add_action( 'admin_enqueue_scripts', 'saiap_enqueue_global_banner_styles' );

