<?php
/**
 * WordPress.org free build settings page.
 *
 * @package Consortia_Quick_Find_Replace
 */

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

/**
 * Plugin settings and admin page rendering.
 */
class Consortia_Qfr_Settings {

	/**
	 * Welcome notice option name.
	 *
	 * @var string
	 */
	const WELCOME_NOTICE_OPTION = 'consortiaqfr_show_welcome_notice';

	/**
	 * Boot settings hooks.
	 *
	 * @return void
	 */
	public static function init() {
		add_action( 'admin_menu', array( __CLASS__, 'add_settings_page' ) );
		add_action( 'admin_notices', array( __CLASS__, 'render_welcome_notice' ) );
	}

	/**
	 * Add admin page under Tools.
	 *
	 * @return void
	 */
	public static function add_settings_page() {
		add_management_page(
			__( 'Quick Find & Replace', 'consortia-quick-find-replace' ),
			__( 'Quick Find & Replace', 'consortia-quick-find-replace' ),
			'manage_options',
			'consortiaqfr-settings',
			array( __CLASS__, 'render_settings_page' )
		);
	}

	/**
	 * Render settings page.
	 *
	 * @return void
	 */
	public static function render_settings_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		$docs_url = self::get_documentation_url();
		?>
			<div class="wrap">
				<h1><?php esc_html_e( 'Quick Find & Replace', 'consortia-quick-find-replace' ); ?></h1>
				<div class="card">
					<h2><?php esc_html_e( 'Editor workflow', 'consortia-quick-find-replace' ); ?></h2>
					<p><?php esc_html_e( 'Quick Find & Replace runs in supported editor screens and changes only the open document.', 'consortia-quick-find-replace' ); ?></p>
					<h3><?php esc_html_e( 'How it works', 'consortia-quick-find-replace' ); ?></h3>
					<ol class="consortia-qfr-usage-list">
						<li><?php esc_html_e( 'Open a post, page, docs entry, or product in the editor.', 'consortia-quick-find-replace' ); ?></li>
						<li><?php esc_html_e( 'Use the Quick Find & Replace box in the editor sidebar to search the current document.', 'consortia-quick-find-replace' ); ?></li>
						<li><?php esc_html_e( 'Move through matches, replace one or all, then save or update the document normally in WordPress.', 'consortia-quick-find-replace' ); ?></li>
					</ol>
					<p><?php esc_html_e( 'The plugin does not run site-wide replacements. It only changes the content that is open in the current editor screen.', 'consortia-quick-find-replace' ); ?></p>
					<p>
						<a class="button button-secondary" href="<?php echo esc_url( $docs_url ); ?>" target="_blank" rel="noopener noreferrer">
							<?php esc_html_e( 'View documentation', 'consortia-quick-find-replace' ); ?>
						</a>
					</p>
					<?php self::render_free_upgrade_notice(); ?>
				</div>
			</div>
		<?php
	}

	/**
	 * Render a one-time welcome notice after activation.
	 *
	 * @return void
	 */
	public static function render_welcome_notice() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( ! get_option( self::WELCOME_NOTICE_OPTION ) ) {
			return;
		}

		delete_option( self::WELCOME_NOTICE_OPTION );

		$tools_url = add_query_arg(
			array(
				'page' => 'consortiaqfr-settings',
			),
			admin_url( 'tools.php' )
		);
		$docs_url  = self::get_documentation_url();
		?>
		<div class="notice notice-success is-dismissible consortia-qfr-welcome-notice">
			<p><strong><?php esc_html_e( 'Thanks for installing Quick Find & Replace.', 'consortia-quick-find-replace' ); ?></strong></p>
			<p><?php esc_html_e( 'Open the plugin page under Tools to see how it works and learn about the Pro upgrade.', 'consortia-quick-find-replace' ); ?></p>
			<p>
				<a class="button button-primary" href="<?php echo esc_url( $tools_url ); ?>">
					<?php esc_html_e( 'Open Quick Find & Replace', 'consortia-quick-find-replace' ); ?>
				</a>
				<a class="button button-secondary" href="<?php echo esc_url( $docs_url ); ?>" target="_blank" rel="noopener noreferrer">
					<?php esc_html_e( 'View documentation', 'consortia-quick-find-replace' ); ?>
				</a>
			</p>
		</div>
		<?php
	}

	/**
	 * Get public documentation URL.
	 *
	 * @return string
	 */
	private static function get_documentation_url() {
		return 'https://www.consortia.no/docs/quick-find-replace/';
	}

	/**
	 * Get Freemius upgrade URL when available.
	 *
	 * @return string
	 */
	private static function get_upgrade_url() {
		if ( ! function_exists( 'consortia_qfr_fs' ) ) {
			return '';
		}

		$freemius = consortia_qfr_fs();

		if ( ! $freemius || ! method_exists( $freemius, 'get_upgrade_url' ) ) {
			return '';
		}

		return (string) $freemius->get_upgrade_url();
	}

	/**
	 * Render a compact upgrade notice for the free build.
	 *
	 * @return void
	 */
	private static function render_free_upgrade_notice() {
		$upgrade_url = self::get_upgrade_url();
		$product_url = 'https://www.consortia.no/quick-find-replace/';
		?>
			<h3><?php esc_html_e( 'Available in Pro', 'consortia-quick-find-replace' ); ?></h3>
			<p><?php esc_html_e( 'Quick Find & Replace Pro adds extra workflow tools for editors who make repeated changes and want a faster editing flow.', 'consortia-quick-find-replace' ); ?></p>
			<ul class="consortia-qfr-pro-list">
				<li><?php esc_html_e( 'Saved presets, replacement history, and undo all for the current session.', 'consortia-quick-find-replace' ); ?></li>
				<li><?php esc_html_e( 'Custom highlight colors, remembered values, and a draggable floating panel.', 'consortia-quick-find-replace' ); ?></li>
				<li><?php esc_html_e( 'Extra WooCommerce support, including product short description replacements.', 'consortia-quick-find-replace' ); ?></li>
			</ul>
			<p>
				<a class="button button-secondary" href="<?php echo esc_url( $product_url ); ?>" target="_blank" rel="noopener noreferrer">
					<?php esc_html_e( 'View Pro details', 'consortia-quick-find-replace' ); ?>
				</a>
				<?php if ( $upgrade_url ) : ?>
					<a class="button" href="<?php echo esc_url( $upgrade_url ); ?>">
						<?php esc_html_e( 'Learn more about Pro', 'consortia-quick-find-replace' ); ?>
					</a>
				<?php endif; ?>
			</p>
			<?php
	}
}
