<?php
/**
 * Plugin admin notices — welcome notice, review prompt.
 *
 * @link       https://zqe.io
 * @since      1.0.0
 *
 * @package    Variation_Swatches_And_Gallery
 * @subpackage Variation_Swatches_And_Gallery/includes
 */

namespace Zqe;

/**
 * Handles admin notices: activation welcome and review request.
 *
 * @since      1.0.7
 * @package    Variation_Swatches_And_Gallery
 * @subpackage Variation_Swatches_And_Gallery/includes
 * @author     ZQE <dev@zqe.io>
 */
class Variation_Swatches_And_Gallery_Notice {
	/**
	 * The plugin instance.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      \Zqe\Variation_Swatches_And_Gallery $plugin
	 */
	private $plugin;

	/**
	 * Initialize the class.
	 *
	 * @since    1.0.0
	 * @param    \Zqe\Variation_Swatches_And_Gallery $plugin The plugin instance.
	 */
	public function __construct( $plugin ) {
		$this->plugin = $plugin;
	}

	/**
	 * Redirect to settings page after first activation.
	 */
	public function activation_redirect() {
		if ( ! get_transient( 'vsg_activation_redirect' ) ) {
			return;
		}

		delete_transient( 'vsg_activation_redirect' );

		// Don't redirect on multisite bulk activation or if already on the page.
		if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
			return;
		}

		wp_safe_redirect( admin_url( 'admin.php?page=variation-swatches-and-gallery&vsg-welcome=1' ) );
		exit;
	}

	/**
	 * Display welcome notice after activation.
	 */
	public function welcome_notice() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		// Only show on the plugin's own settings page with the welcome flag.
		if ( ! isset( $_GET['vsg-welcome'] ) ) {
			return;
		}

		$docs_url = 'https://zqe.io/docs/variation-swatches-and-gallery-documentation/';
		$demo_url = 'https://demo.zqe.io/variation-swatches-and-gallery/';
		?>
		<div class="notice notice-success is-dismissible">
			<h3><?php esc_html_e( 'Variation Swatches and Gallery is active!', 'variation-swatches-and-gallery' ); ?></h3>
			<p>
				<?php esc_html_e( 'All product variation dropdowns have been automatically converted to button swatches.', 'variation-swatches-and-gallery' ); ?>
				<?php esc_html_e( 'Visit any variable product page to see the swatches in action.', 'variation-swatches-and-gallery' ); ?>
			</p>
			<p>
				<strong><?php esc_html_e( 'Quick Start:', 'variation-swatches-and-gallery' ); ?></strong>
				<?php esc_html_e( 'To use Color or Image swatches, go to Products > Attributes, edit an attribute, and change its Type.', 'variation-swatches-and-gallery' ); ?>
			</p>
			<p>
				<a href="<?php echo esc_url( $docs_url ); ?>" class="button button-primary" target="_blank">
					<?php esc_html_e( 'View Documentation', 'variation-swatches-and-gallery' ); ?>
				</a>
				<a href="<?php echo esc_url( $demo_url ); ?>" class="button" target="_blank" style="margin-left: 8px;">
					<?php esc_html_e( 'See Live Demo', 'variation-swatches-and-gallery' ); ?>
				</a>
			</p>
		</div>
		<?php
	}

	/**
	 * Display review request notice after 7+ days of active use.
	 * Shows only once, dismissible, respects user choice.
	 */
	public function review_notice() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		// Don't show if user already dismissed.
		if ( get_user_meta( get_current_user_id(), 'vsg_review_notice_dismissed', true ) ) {
			return;
		}

		// Don't show if user already left a review.
		if ( get_option( 'vsg_review_done' ) ) {
			return;
		}

		// Only show after 7 days of active use.
		$activation_time = get_option( 'vsg_activation_time' );
		if ( ! $activation_time || ( time() - $activation_time ) < 7 * DAY_IN_SECONDS ) {
			return;
		}

		// Only show if WooCommerce has variable products (plugin is actually in use).
		if ( class_exists( 'WC_Product_Query' ) ) {
			$query = new \WC_Product_Query( array(
				'type'   => 'variable',
				'status' => 'publish',
				'limit'  => 1,
				'return' => 'ids',
			) );

			$products = $query->get_products();
			if ( empty( $products ) ) {
				return;
			}
		}

		// Only show on WooCommerce or plugin settings pages.
		$screen = get_current_screen();
		if ( $screen && ! in_array( $screen->id, array(
			'toplevel_page_variation-swatches-and-gallery',
			'edit-product',
			'product',
			'woocommerce_page_wc-settings',
		), true ) ) {
			return;
		}

		$review_url  = 'https://wordpress.org/support/plugin/variation-swatches-and-gallery/reviews/#new-post';
		$dismiss_url = wp_nonce_url( admin_url( 'admin-ajax.php?action=vsg_dismiss_review_notice' ), 'vsg_dismiss_review' );
		$done_url    = wp_nonce_url( admin_url( 'admin-ajax.php?action=vsg_review_done' ), 'vsg_review_done' );
		?>
		<div class="notice notice-info is-dismissible vsg-review-notice" style="padding: 12px 16px;">
			<p style="font-size: 14px;">
				<?php esc_html_e( "You've been using Variation Swatches and Gallery for a while now. If it's working well for your store, would you mind leaving a quick review? It helps other WooCommerce store owners find us.", 'variation-swatches-and-gallery' ); ?>
			</p>
			<p>
				<a href="<?php echo esc_url( $review_url ); ?>" class="button button-primary" target="_blank">
					<?php esc_html_e( 'Leave a Review', 'variation-swatches-and-gallery' ); ?>
				</a>
				<a href="<?php echo esc_url( $dismiss_url ); ?>" class="button" style="margin-left: 8px;">
					<?php esc_html_e( 'Maybe Later', 'variation-swatches-and-gallery' ); ?>
				</a>
				<a href="<?php echo esc_url( $done_url ); ?>" class="button" style="margin-left: 8px;">
					<?php esc_html_e( 'Already Did', 'variation-swatches-and-gallery' ); ?>
				</a>
			</p>
		</div>
		<script>
			(function($) {
				$(document).on('click', '.vsg-review-notice .notice-dismiss', function() {
					$.post(ajaxurl, { action: 'vsg_dismiss_review_notice', _wpnonce: '<?php echo esc_js( wp_create_nonce( 'vsg_dismiss_review' ) ); ?>' });
				});
			})(jQuery);
		</script>
		<?php
	}

	/**
	 * AJAX handler: dismiss review notice.
	 */
	public function dismiss_review_notice() {
		check_ajax_referer( 'vsg_dismiss_review', '_wpnonce' );
		update_user_meta( get_current_user_id(), 'vsg_review_notice_dismissed', true );
		wp_send_json_success();
	}

	/**
	 * AJAX handler: user says they already reviewed.
	 */
	public function review_done() {
		check_ajax_referer( 'vsg_review_done', '_wpnonce' );
		update_option( 'vsg_review_done', true );
		wp_safe_redirect( wp_get_referer() ?: admin_url() );
		exit;
	}
}
