<?php
/**
 * HJCyberX Checkout for Paddle Lite Marketing & Admin Notices.
 *
 * Shows an "Upgrade to Pro" admin notice for free users.
 *
 * @package PaddleKonnect_Lite
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class PaddleKonnect_Lite_Marketing.
 */
class PaddleKonnect_Lite_Marketing {

	/**
	 * Initialize marketing hooks.
	 */
	public static function init(): void {
		add_action( 'admin_notices', array( __CLASS__, 'display_admin_notice' ) );
		add_action( 'admin_init', array( __CLASS__, 'dismiss_admin_notice' ) );
	}

	/**
	 * Display the "Upgrade to Pro" admin notice.
	 */
	public static function display_admin_notice(): void {
		// Only show to admins.
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$user_id = get_current_user_id();
		if ( get_user_meta( $user_id, '_paddlekonnect_lite_marketing_notice_dismissed', true ) ) {
			return;
		}

		$dismiss_url  = wp_nonce_url( add_query_arg( 'paddlekonnect_lite_dismiss_marketing', '1' ), 'paddlekonnect_lite_dismiss_notice' );
		$upgrade_url  = 'https://plugins.hjcyberx.com/plugins/hjcyberx-checkout-paddle#features';
		$features_url = admin_url( 'admin.php?page=paddlekonnect-lite-pro' );

		?>
		<div class="notice notice-info is-dismissible" style="border-left-color: #46b450; padding: 15px; display: flex; align-items: center; justify-content: space-between; background: linear-gradient(135deg, #f0fff4 0%, #fff 100%); box-shadow: 0 1px 3px rgba(0,0,0,.08);">
			<div>
				<h3 style="margin: 0 0 5px 0; color: #2e7d32;">🚀 Upgrade to HJCyberX Checkout for Paddle Pro!</h3>
				<p style="margin: 0; font-size: 14px; color: #555;">
					Unlock <strong>Auto-Fetch Products</strong>, <strong>Vault / Content Restriction</strong>, and <strong>Subscription Management</strong>.
					Get the full power of Paddle Billing in WooCommerce!
				</p>
			</div>
			<div style="text-align: right; min-width: 280px; flex-shrink: 0;">
				<a href="<?php echo esc_url( $upgrade_url ); ?>" class="button button-primary button-large" target="_blank" style="background: #46b450; border-color: #3a9a42; font-weight: bold;">Upgrade Now</a>
				<a href="<?php echo esc_url( $features_url ); ?>" class="button button-large" style="margin-left: 8px;">See Features</a>
				<a href="<?php echo esc_url( $dismiss_url ); ?>" class="button button-large" style="margin-left: 8px;">Dismiss</a>
			</div>
		</div>
		<?php
	}

	/**
	 * Handle the dismissal of the admin notice.
	 */
	public static function dismiss_admin_notice(): void {
		if ( isset( $_GET['paddlekonnect_lite_dismiss_marketing'] ) && isset( $_GET['_wpnonce'] ) ) {
			if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'paddlekonnect_lite_dismiss_notice' ) ) {
				$user_id = get_current_user_id();
				update_user_meta( $user_id, '_paddlekonnect_lite_marketing_notice_dismissed', '1' );
				wp_safe_redirect( remove_query_arg( array( 'paddlekonnect_lite_dismiss_marketing', '_wpnonce' ) ) );
				exit;
			}
		}
	}
}
