<?php
/**
 * Pro launch banner for plugin admin pages.
 *
 * @package WebEquipe\PDFSearch
 * @since 1.2.0
 */

namespace WebEquipe\PDFSearch;

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

/**
 * Renders a dismissible Pro upsell banner on plugin-owned admin screens only.
 */
class Pro_Launch_Banner {

	/**
	 * User meta key for permanent dismissal.
	 */
	const DISMISSED_META = 'webequipe_pro_banner_dismissed';

	/**
	 * Plugin marketing site URL.
	 */
	const PLUGIN_WEBSITE_URL = 'https://webequipe.com/pdf-search/';

	/**
	 * Pro pricing page URL.
	 */
	const PRICING_URL = 'https://webequipe.com/pdf-search/#pricing';

	/**
	 * Register AJAX handler for dismiss.
	 */
	public function __construct() {
		add_action( 'wp_ajax_webequipe_dismiss_pro_banner', array( $this, 'ajax_dismiss' ) );
	}

	/**
	 * Plugin marketing site URL.
	 *
	 * @since 2.0.0
	 * @return string
	 */
	public static function get_plugin_website_url() {
		/**
		 * Filter the plugin marketing site URL.
		 *
		 * @since 2.0.0
		 * @param string $url Plugin website URL.
		 */
		return apply_filters( 'webequipe_pdf_search_plugin_website_url', self::PLUGIN_WEBSITE_URL );
	}

	/**
	 * External pricing page URL (Pro launch banner and analytics referrers).
	 *
	 * @since 1.2.2
	 * @return string
	 */
	public static function get_pricing_url() {
		/**
		 * Filter the external Pro pricing page URL (banner only).
		 *
		 * @since 1.2.2
		 * @param string $url Pricing page URL.
		 */
		return apply_filters( 'webequipe_pdf_search_pro_pricing_url', self::PRICING_URL );
	}

	/**
	 * Freemius in-dashboard pricing / checkout URL.
	 *
	 * @since 2.0.0
	 * @return string
	 */
	public static function get_admin_pricing_url() {
		$url = admin_url( 'admin.php?page=webequipe-pdf-search-pricing' );

		/**
		 * Filter the Freemius admin pricing page URL.
		 *
		 * @since 2.0.0
		 * @param string $url Admin pricing page URL.
		 */
		return apply_filters( 'webequipe_pdf_search_admin_pricing_url', $url );
	}

	/**
	 * Pro Features comparison page URL in wp-admin.
	 *
	 * @since 2.0.0
	 * @return string
	 */
	public static function get_pro_features_url() {
		$url = admin_url( 'admin.php?page=webequipe-pdf-search-upgrade' );

		/**
		 * Filter the Pro Features admin page URL.
		 *
		 * @since 2.0.0
		 * @param string $url Pro Features page URL.
		 */
		return apply_filters( 'webequipe_pdf_search_pro_features_url', $url );
	}

	/**
	 * Feature comparison rows for the Pro Features page (Free / Starter / Pro / Agency).
	 *
	 * Cell values: 'yes', 'no', or a short localized string.
	 *
	 * @since 2.0.0
	 * @return array<int, array<string, string>>
	 */
	public static function get_feature_comparison_rows() {
		/**
		 * Filter feature comparison table rows on the Pro Features page.
		 *
		 * @since 2.0.0
		 * @param array<int, array<string, string>> $rows Comparison rows.
		 */
		return apply_filters(
			'webequipe_pdf_search_feature_comparison_rows',
			array(
				array(
					'label'   => __( 'OCR for scanned PDFs', 'webequipe-pdf-search' ),
					'free'    => 'no',
					'starter' => __( '1,000 pages/mo', 'webequipe-pdf-search' ),
					'pro'     => __( '3,000 pages/mo', 'webequipe-pdf-search' ),
					'agency'  => __( '10,000 pages/mo', 'webequipe-pdf-search' ),
				),
				array(
					'label'   => __( 'Private PDF search', 'webequipe-pdf-search' ),
					'free'    => 'no',
					'starter' => 'no',
					'pro'     => 'yes',
					'agency'  => 'yes',
				),
				array(
					'label'   => __( 'Analytics dashboard', 'webequipe-pdf-search' ),
					'free'    => 'no',
					'starter' => 'no',
					'pro'     => 'yes',
					'agency'  => 'yes',
				),
				array(
					'label'   => __( 'Advanced search filters', 'webequipe-pdf-search' ),
					'free'    => 'no',
					'starter' => 'yes',
					'pro'     => 'yes',
					'agency'  => 'yes',
				),
				array(
					'label'   => __( 'White-label mode', 'webequipe-pdf-search' ),
					'free'    => 'no',
					'starter' => 'no',
					'pro'     => 'no',
					'agency'  => 'yes',
				),
				array(
					'label'   => __( 'Text-based PDF indexing', 'webequipe-pdf-search' ),
					'free'    => 'yes',
					'starter' => 'yes',
					'pro'     => 'yes',
					'agency'  => 'yes',
				),
				array(
					'label'   => __( 'WordPress search integration', 'webequipe-pdf-search' ),
					'free'    => 'yes',
					'starter' => 'yes',
					'pro'     => 'yes',
					'agency'  => 'yes',
				),
				array(
					'label'   => __( 'PDF search shortcode', 'webequipe-pdf-search' ),
					'free'    => 'yes',
					'starter' => 'yes',
					'pro'     => 'yes',
					'agency'  => 'yes',
				),
				array(
					'label'   => __( 'Manage PDFs & bulk actions', 'webequipe-pdf-search' ),
					'free'    => 'yes',
					'starter' => 'yes',
					'pro'     => 'yes',
					'agency'  => 'yes',
				),
				array(
					'label'   => __( 'Background indexing', 'webequipe-pdf-search' ),
					'free'    => 'yes',
					'starter' => 'yes',
					'pro'     => 'yes',
					'agency'  => 'yes',
				),
				array(
					'label'   => __( 'Multisite support', 'webequipe-pdf-search' ),
					'free'    => 'yes',
					'starter' => 'yes',
					'pro'     => 'yes',
					'agency'  => 'yes',
				),
				array(
					'label'   => __( 'Exclude PDFs from search', 'webequipe-pdf-search' ),
					'free'    => 'yes',
					'starter' => 'yes',
					'pro'     => 'yes',
					'agency'  => 'yes',
				),
				array(
					'label'   => __( 'Site licenses', 'webequipe-pdf-search' ),
					'free'    => '1',
					'starter' => '1',
					'pro'     => '5',
					'agency'  => __( 'Unlimited', 'webequipe-pdf-search' ),
				),
				array(
					'label'   => __( 'Support', 'webequipe-pdf-search' ),
					'free'    => __( 'Community', 'webequipe-pdf-search' ),
					'starter' => __( 'Priority', 'webequipe-pdf-search' ),
					'pro'     => __( 'Priority', 'webequipe-pdf-search' ),
					'agency'  => __( 'Priority', 'webequipe-pdf-search' ),
				),
			)
		);
	}

	/**
	 * Render one feature-comparison table cell.
	 *
	 * @since 2.0.0
	 * @param string $value Cell value: yes, no, or text.
	 * @return void
	 */
	public static function render_comparison_cell( $value ) {
		if ( 'yes' === $value ) {
			echo '<span class="webequipe-pdf-compare-yes" aria-label="' . esc_attr__( 'Included', 'webequipe-pdf-search' ) . '">';
			echo '<span class="dashicons dashicons-yes" aria-hidden="true"></span>';
			echo '</span>';
			return;
		}

		if ( 'no' === $value ) {
			echo '<span class="webequipe-pdf-compare-no" aria-label="' . esc_attr__( 'Not included', 'webequipe-pdf-search' ) . '">—</span>';
			return;
		}

		echo '<span class="webequipe-pdf-compare-text">' . esc_html( $value ) . '</span>';
	}

	/**
	 * Render the banner when allowed.
	 *
	 * @since 1.2.0
	 * @return void
	 */
	public static function render() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( get_user_meta( get_current_user_id(), self::DISMISSED_META, true ) ) {
			return;
		}

		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( $screen && 'pdf-search_page_webequipe-pdf-search-upgrade' === $screen->id ) {
			return;
		}

		$features = array(
			__( 'OCR for scanned PDFs', 'webequipe-pdf-search' ),
			__( 'Private/member-only search', 'webequipe-pdf-search' ),
			__( 'Analytics dashboard', 'webequipe-pdf-search' ),
		);

		$landing_url = apply_filters( 'webequipe_pdf_search_pro_banner_url', self::get_pricing_url() );

		$view = WEBEQUIPE_PDF_SEARCH_DIR . 'admin/views/pro-launch-banner.php';
		if ( file_exists( $view ) ) {
			include $view;
		}
	}

	/**
	 * AJAX handler — saves permanent dismissal to user meta.
	 *
	 * @since 1.2.0
	 * @return void
	 */
	public function ajax_dismiss() {
		check_ajax_referer( 'webequipe_dismiss_pro_banner', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error();
		}

		update_user_meta( get_current_user_id(), self::DISMISSED_META, '1' );
		wp_send_json_success();
	}
}
