<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


class AiContentWizard_PromoPilot {
	const NOTICE_META_KEY = 'ai_cw_promopilot_notice_dismissed';
	const NONCE_ACTION    = 'promopilot-dismiss';

	public static function init() {
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
		add_action( 'admin_notices', array( __CLASS__, 'render_global_notice' ) );
		add_action( 'wp_ajax_promopilot_dismiss_notice', array( __CLASS__, 'handle_dismiss_notice' ) );
	}

	public static function enqueue_assets() {
		wp_enqueue_style( 'promopilot-banner-style', AI_CONTENT_WIZARD_URL . 'css/promopilot-banner.css', array(), AI_CONTENT_WIZARD_VERSION );
		wp_enqueue_script( 'promopilot-banner-script', AI_CONTENT_WIZARD_URL . 'js/promopilot-banner.js', array( 'jquery' ), AI_CONTENT_WIZARD_VERSION, true );
		wp_localize_script(
			'promopilot-banner-script',
			'promopilotBanner',
			array(
				'nonce'   => wp_create_nonce( self::NONCE_ACTION ),
				'ajaxUrl' => admin_url( 'admin-ajax.php' ),
			)
		);
	}

	public static function render_global_notice() {
		if ( ! is_admin() || self::is_notice_dismissed() ) {
			return;
		}

		if ( function_exists( 'get_current_screen' ) ) {
			$current_screen = get_current_screen();
			if ( $current_screen && in_array( $current_screen->id, array( 'toplevel_page_ai_content_wizard', 'ai-content-wizard_page_ai_content_wizard_settings' ), true ) ) {
				return;
			}
		}
		?>
		<div class="promopilot-admin-notice notice" data-notice-id="<?php echo esc_attr( self::get_global_notice_id() ); ?>">
			<div class="pp-bg-animation" style="opacity:0.5;">
				<div class="pp-orb pp-orb-1" style="width:100px;height:100px;top:-20px;right:20%;"></div>
				<div class="pp-star pp-star-1"></div>
				<div class="pp-star pp-star-2"></div>
			</div>

			<div class="promopilot-notice-content" style="position:relative;z-index:2;">
				<div class="promopilot-logo-area">
					<span class="dashicons dashicons-rocket" style="color:var(--pp-secondary);font-size:24px;width:24px;height:24px;"></span>
					<span class="promopilot-logo-text">PromoPilot</span>
					<span class="promopilot-badge">BETA $30</span>
				</div>
				<div class="promopilot-message">
					<strong><?php esc_html_e( 'New!', 'ai-content-wizard' ); ?></strong> <?php esc_html_e( 'Automated 3-Level Link Cascade (5 → 50 → 5000) for explosive DR & UR growth. Safe, natural, and effective link building automation.', 'ai-content-wizard' ); ?>
				</div>
				<div class="promopilot-cta">
					<a href="https://promopilot.link?utm_source=aiwizard&utm_medium=banner&utm_campaign=global_notice" target="_blank" class="promopilot-btn">
						<?php esc_html_e( 'Get Started', 'ai-content-wizard' ); ?> <span class="dashicons dashicons-arrow-right-alt2" style="margin-top:2px;"></span>
					</a>
					<button type="button" class="promopilot-dismiss">
						<span class="dashicons dashicons-dismiss"></span>
					</button>
				</div>
			</div>
		</div>
		<?php
	}

	public static function render_dashboard_banner() {
		?>
		<div class="promopilot-dashboard-banner">
			<div class="pp-bg-animation">
				<div class="pp-orb pp-orb-1"></div>
				<div class="pp-orb pp-orb-2"></div>
				<div class="pp-star pp-star-1"></div>
				<div class="pp-star pp-star-2"></div>
				<div class="pp-star pp-star-3"></div>
				<div class="pp-atom-path">
					<div class="pp-atom"></div>
				</div>
			</div>

			<div class="promopilot-dashboard-content" style="position:relative;z-index:2;">
				<div class="promopilot-info">
					<h2><?php esc_html_e( 'Boost Your SEO with PromoPilot', 'ai-content-wizard' ); ?> <span class="promopilot-badge" style="vertical-align:middle;font-size:0.4em;"><?php esc_html_e( 'NEW', 'ai-content-wizard' ); ?></span></h2>
					<p>
						<?php esc_html_e( 'Discover the power of', 'ai-content-wizard' ); ?> <strong><?php esc_html_e( '3-Level Link Cascades', 'ai-content-wizard' ); ?></strong>.
						<?php esc_html_e( 'Turn 5 direct links into 5,000 signals for massive authority growth.', 'ai-content-wizard' ); ?>
						<br>
						<span style="color:var(--pp-secondary);font-weight:bold;"><?php esc_html_e( 'Beta Offer: Only $30 (Save $470+)', 'ai-content-wizard' ); ?></span>
					</p>
					<div class="promopilot-features">
						<div class="promopilot-feature">
							<span class="dashicons dashicons-chart-line"></span> <?php esc_html_e( 'High DR/UR', 'ai-content-wizard' ); ?>
						</div>
						<div class="promopilot-feature">
							<span class="dashicons dashicons-shield"></span> <?php esc_html_e( 'Spam Protection', 'ai-content-wizard' ); ?>
						</div>
						<div class="promopilot-feature">
							<span class="dashicons dashicons-admin-site-alt3"></span> <?php esc_html_e( 'Automated', 'ai-content-wizard' ); ?>
						</div>
					</div>
					<a href="https://promopilot.link?utm_source=aiwizard&utm_medium=dashboard&utm_campaign=main_banner" target="_blank" class="promopilot-btn" style="display:inline-block;">
						<?php esc_html_e( 'Launch Campaign Now', 'ai-content-wizard' ); ?>
					</a>
				</div>
				<div class="promopilot-visual">
					<div class="promopilot-visual-circle"></div>
					<div class="promopilot-visual-circle small"></div>
					<div class="promopilot-visual-circle tiny"></div>
				</div>
			</div>
		</div>
		<?php
	}

	public static function render_settings_banner() {
		?>
		<div class="promopilot-dashboard-banner" style="padding:25px;margin:10px 0 20px;background:linear-gradient(135deg,#1e1b4b 0%,#312e81 100%);position:relative;overflow:hidden;">
			<div class="pp-bg-animation">
				<div class="pp-orb pp-orb-1"></div>
				<div class="pp-orb pp-orb-2"></div>
				<div class="pp-star pp-star-1"></div>
				<div class="pp-star pp-star-2"></div>
				<div class="pp-star pp-star-3"></div>
				<div class="pp-atom-path">
					<div class="pp-atom"></div>
				</div>
			</div>

			<div class="promopilot-dashboard-content" style="gap:25px;position:relative;z-index:2;">
				<div class="promopilot-info" style="flex:1;">
					<h3 style="margin:0 0 8px;font-size:1.5em;color:#fff;font-weight:700;">
						PromoPilot <?php esc_html_e( 'Automated Link Building', 'ai-content-wizard' ); ?>
						<span class="promopilot-badge" style="font-size:0.5em;vertical-align:middle;">BETA $30</span>
					</h3>
					<p style="margin-bottom:12px;font-size:1em;color:rgba(255,255,255,0.9);line-height:1.6;">
						<?php esc_html_e( 'Automate your link building with 3-Level Cascades.', 'ai-content-wizard' ); ?><br>
						<strong style="color:#ec4899;">5 Links → 50 Articles → 5000 Signals.</strong> <?php esc_html_e( 'Safe & Effective.', 'ai-content-wizard' ); ?>
					</p>
					<div style="display:flex;gap:15px;margin-bottom:15px;flex-wrap:wrap;">
						<div style="display:flex;align-items:center;gap:6px;font-size:0.9em;color:rgba(255,255,255,0.85);">
							<span style="color:#10b981;font-size:1.2em;">✓</span> <?php esc_html_e( 'High DR/UR Links', 'ai-content-wizard' ); ?>
						</div>
						<div style="display:flex;align-items:center;gap:6px;font-size:0.9em;color:rgba(255,255,255,0.85);">
							<span style="color:#10b981;font-size:1.2em;">✓</span> <?php esc_html_e( 'Spam Protection', 'ai-content-wizard' ); ?>
						</div>
						<div style="display:flex;align-items:center;gap:6px;font-size:0.9em;color:rgba(255,255,255,0.85);">
							<span style="color:#10b981;font-size:1.2em;">✓</span> <?php esc_html_e( 'Fully Automated', 'ai-content-wizard' ); ?>
						</div>
					</div>
					<a href="https://promopilot.link?utm_source=aiwizard&utm_medium=settings&utm_campaign=settings_banner" target="_blank" class="promopilot-btn" style="padding:8px 20px;font-size:0.95em;display:inline-block;">
						<?php esc_html_e( 'Launch Campaign Now', 'ai-content-wizard' ); ?>
					</a>
				</div>
				<div class="promopilot-visual" style="width:140px;height:120px;flex-shrink:0;">
					<div class="promopilot-visual-circle" style="width:70px;height:70px;font-size:1.1em;"></div>
					<div class="promopilot-visual-circle small" style="width:35px;height:35px;font-size:0.7em;top:10px;right:15px;"></div>
					<div class="promopilot-visual-circle tiny" style="width:22px;height:22px;font-size:0.55em;bottom:15px;left:20px;"></div>
				</div>
			</div>
		</div>
		<?php
	}

	public static function render_sidebar_banner() {
		?>
		<div class="promopilot-sidebar-banner">
			<div class="pp-bg-animation">
				<div class="pp-orb pp-orb-1"></div>
				<div class="pp-orb pp-orb-2"></div>
				<div class="pp-star pp-star-1"></div>
				<div class="pp-star pp-star-2"></div>
				<div class="pp-star pp-star-3"></div>
				<div class="pp-atom-path">
					<div class="pp-atom"></div>
				</div>
			</div>

			<div class="promopilot-sidebar-content" style="position:relative;z-index:2;display:flex;flex-direction:column;gap:10px;">
				<div style="text-align:center;">
					<span class="promopilot-sidebar-title" style="display:inline;">PromoPilot</span>
					<br>
					<span class="promopilot-badge" style="font-size:0.7em;margin-top:4px;display:inline-block;">BETA $30</span>
				</div>
				<span class="promopilot-sidebar-text" style="font-size:0.95em;">
					<?php esc_html_e( 'Automate your link building with 3-Level Cascades.', 'ai-content-wizard' ); ?><br><strong>5 Links → 50 Articles → 5000 Signals.</strong>
				</span>
				<div class="promopilot-sidebar-visual" style="width:100%;height:55px;position:relative;margin:5px 0;">
					<div class="promopilot-visual-circle" style="width:45px;height:45px;left:50%;top:50%;transform:translate(-50%,-50%);"></div>
					<div class="promopilot-visual-circle small" style="width:28px;height:28px;top:50%;right:20%;transform:translateY(-50%);"></div>
					<div class="promopilot-visual-circle tiny" style="width:18px;height:18px;top:50%;left:20%;transform:translateY(-50%);"></div>
				</div>
				<a href="https://promopilot.link?utm_source=aiwizard&utm_medium=sidebar&utm_campaign=settings_sidebar" target="_blank" class="promopilot-sidebar-btn">
					<?php esc_html_e( 'Boost SEO Now', 'ai-content-wizard' ); ?>
				</a>
			</div>
		</div>
		<?php
	}

	public static function handle_dismiss_notice() {
		check_ajax_referer( self::NONCE_ACTION, 'nonce' );

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

		$notice_id = sanitize_text_field( wp_unslash( $_POST['notice_id'] ?? '' ) );
		if ( $notice_id !== self::get_global_notice_id() ) {
			wp_send_json_error( array( 'message' => __( 'Unknown notice.', 'ai-content-wizard' ) ), 400 );
		}

		$user_id = get_current_user_id();
		if ( $user_id ) {
			update_user_meta( $user_id, self::NOTICE_META_KEY, 1 );
		}

		wp_send_json_success();
	}

	private static function is_notice_dismissed() {
		$user_id = get_current_user_id();
		return $user_id ? (bool) get_user_meta( $user_id, self::NOTICE_META_KEY, true ) : false;
	}

	private static function get_global_notice_id() {
		return 'promopilot_global_notice';
	}
}

AiContentWizard_PromoPilot::init();
