<?php
/**
 * Admin Notices for Lite Version
 *
 * Displays upgrade prompts and notices for Lite users
 *
 * @package Vareon_Calculator_Suite_Lite
 */

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

/**
 * Get the user meta key for upgrade notice dismissal.
 *
 * @return string
 */
function vareoncalc_get_upgrade_notice_meta_key() {
	return 'vareoncalc_upgrade_notice_dismissed';
}

/**
 * Determine whether the current user dismissed the upgrade notice.
 *
 * @param int $user_id Optional user ID.
 * @return bool
 */
function vareoncalc_is_upgrade_notice_dismissed( $user_id = 0 ) {
	$user_id = absint( $user_id );
	if ( $user_id <= 0 ) {
		$user_id = get_current_user_id();
	}

	if ( $user_id <= 0 ) {
		return false;
	}

	return ! empty( get_user_meta( $user_id, vareoncalc_get_upgrade_notice_meta_key(), true ) );
}

/**
 * Decide whether to show the Lite upgrade notice.
 *
 * @return bool
 */
function vareoncalc_should_show_upgrade_notice() {
	$upgrade_notice_enabled = (bool) apply_filters( 'vareoncalc_enable_upgrade_notice', false );
	if ( ! $upgrade_notice_enabled || ! current_user_can( 'manage_options' ) ) {
		return false;
	}

	return ! vareoncalc_is_upgrade_notice_dismissed();
}

/**
 * Enqueue the small dismissal helper for the admin notice.
 */
function vareoncalc_enqueue_admin_notice_scripts() {
	if ( ! vareoncalc_should_show_upgrade_notice() ) {
		return;
	}

	$script_ver = function_exists( 'vareoncalc_get_asset_version' ) ? vareoncalc_get_asset_version() : VAREONCALC_VER;
	wp_register_script( 'vareoncalc-admin-notice', false, array( 'jquery' ), $script_ver, true );
	wp_enqueue_script( 'vareoncalc-admin-notice' );

	wp_localize_script(
		'vareoncalc-admin-notice',
		'vareoncalcNotice',
		array(
			'ajaxUrl' => admin_url( 'admin-ajax.php' ),
			'nonce'   => wp_create_nonce( 'vareoncalc_dismiss_notice' ),
		)
	);

	wp_add_inline_script(
		'vareoncalc-admin-notice',
		'
    jQuery(document).ready(function($) {
      $(".vareoncalc-upgrade-notice .notice-dismiss").on("click", function() {
        $.post(vareoncalcNotice.ajaxUrl, {
          action: "vareoncalc_dismiss_upgrade_notice",
          nonce: vareoncalcNotice.nonce
        });
      });
    });
  '
	);
}
add_action( 'admin_enqueue_scripts', 'vareoncalc_enqueue_admin_notice_scripts' );

/**
 * Output notices tied to Lite/Pro state.
 */
function vareoncalc_admin_notices() {
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	if ( get_transient( 'vareoncalc_pro_activated' ) ) {
		?>
	<div class="notice notice-success is-dismissible">
		<p><strong><?php esc_html_e( 'Vareon Calculator Suite Pro activated!', 'vareon-calculator-suite-lite' ); ?></strong> <?php esc_html_e( 'You now have access to all premium features including an expanding calculator library, custom themes, and advanced customization.', 'vareon-calculator-suite-lite' ); ?></p>
	</div>
		<?php
		delete_transient( 'vareoncalc_pro_activated' );
		return;
	}

	if ( get_transient( 'vareoncalc_pro_deactivated' ) ) {
		?>
	<div class="notice notice-warning is-dismissible">
		<p><strong><?php esc_html_e( 'Vareon Calculator Suite reverted to Lite version.', 'vareon-calculator-suite-lite' ); ?></strong> <?php esc_html_e( 'Pro features have been disabled.', 'vareon-calculator-suite-lite' ); ?> <a href="<?php echo esc_url( admin_url( 'options-general.php?page=vareoncalc' ) ); ?>"><?php esc_html_e( 'Explore Pro options', 'vareon-calculator-suite-lite' ); ?></a> <?php esc_html_e( 'to regain access.', 'vareon-calculator-suite-lite' ); ?></p>
	</div>
		<?php
		delete_transient( 'vareoncalc_pro_deactivated' );
		return;
	}

	if ( ! vareoncalc_should_show_upgrade_notice() ) {
		return;
	}

	?>
	<div class="notice notice-info is-dismissible vareoncalc-upgrade-notice" data-notice="upgrade">
	<div style="display: flex; align-items: center; gap: 20px; padding: 10px 0;">
		<div style="flex-shrink: 0;">
		<svg width="48" height="48" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
			<rect width="24" height="24" rx="4" fill="#3b82f6"/>
			<path d="M7 12h10M12 7v10" stroke="white" stroke-width="2" stroke-linecap="round"/>
		</svg>
		</div>
		<div style="flex: 1;">
		<h3 style="margin: 0 0 5px 0; font-size: 16px;"><?php esc_html_e( "You're using Vareon Calculator Suite Lite", 'vareon-calculator-suite-lite' ); ?></h3>
		<p style="margin: 0;">
			<?php
			printf(
				/* translators: %s: "Pro" wrapped in strong tags */
				esc_html__( 'Explore %s to unlock an expanding calculator library, advanced themes, custom colors & fonts, and more!', 'vareon-calculator-suite-lite' ),
				'<strong>' . esc_html__( 'Pro', 'vareon-calculator-suite-lite' ) . '</strong>'
			);
			?>
		</p>
		</div>
		<div style="flex-shrink: 0;">
		<a href="<?php echo esc_url( 'https://vareon.dev' ); ?>" class="button button-primary" style="background: #3b82f6; border-color: #2563eb; text-shadow: none; box-shadow: none;">
			<?php esc_html_e( 'Explore Pro', 'vareon-calculator-suite-lite' ); ?>
		</a>
		</div>
	</div>
	</div>
	<?php
}
add_action( 'admin_notices', 'vareoncalc_admin_notices' );

/**
 * Ajax dismissal handler for the upgrade notice.
 */
function vareoncalc_dismiss_upgrade_notice() {
	check_ajax_referer( 'vareoncalc_dismiss_notice', 'nonce' );

	if ( ! current_user_can( 'manage_options' ) ) {
		wp_send_json_error( __( 'Unauthorized', 'vareon-calculator-suite-lite' ), 403 );
	}

	update_user_meta( get_current_user_id(), vareoncalc_get_upgrade_notice_meta_key(), 1 );

	wp_send_json_success();
}
add_action( 'wp_ajax_vareoncalc_dismiss_upgrade_notice', 'vareoncalc_dismiss_upgrade_notice' );

/**
 * Add custom row meta on the plugins page.
 *
 * NOTE: Pro upgrade link temporarily disabled.
 *
 * @param array  $links Plugin links.
 * @param string $file  Plugin file.
 * @return array
 */
function vareoncalc_plugin_row_meta( $links, $file ) {
	if ( 'vareon-calculator-suite-lite/vareon-calculator-suite-lite.php' !== $file ) {
		return $links;
	}

	if ( function_exists( 'vareoncalc_get_review_cta_action_url' ) ) {
		$links[] = '<a href="' . esc_url( vareoncalc_get_review_cta_action_url( 'review', 'plugins' ) ) . '">' . esc_html__( 'Leave a Review', 'vareon-calculator-suite-lite' ) . '</a>';
		$links[] = '<a href="' . esc_url( vareoncalc_get_review_cta_action_url( 'help', 'plugins' ) ) . '">' . esc_html__( 'Support', 'vareon-calculator-suite-lite' ) . '</a>';
	}

	return $links;
}
add_filter( 'plugin_row_meta', 'vareoncalc_plugin_row_meta', 10, 2 );

/**
 * Add action links on the plugins page.
 *
 * @param array $links Plugin action links.
 * @return array
 */
function vareoncalc_plugin_action_links( $links ) {
	$docs_link     = '<a href="' . esc_url( admin_url( 'options-general.php?page=vareoncalc&tab=docs' ) ) . '">' . esc_html__( 'Docs', 'vareon-calculator-suite-lite' ) . '</a>';
	$settings_link = '<a href="' . esc_url( admin_url( 'options-general.php?page=vareoncalc' ) ) . '">' . esc_html__( 'Settings', 'vareon-calculator-suite-lite' ) . '</a>';
	array_unshift( $links, $docs_link );
	array_unshift( $links, $settings_link );

	return $links;
}
add_filter( 'plugin_action_links_vareon-calculator-suite-lite/vareon-calculator-suite-lite.php', 'vareoncalc_plugin_action_links' );
