<?php
/**
 * Bootstrap class. init() is called on plugins_loaded and boots every other
 * class in dependency order.
 *
 * @package search-social-appearance-audit
 */

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

class SSAA_Plugin {

	public static function init() {
		SSAA_License::init();
		SSAA_Settings::init();
		SSAA_Ignores::init();
		SSAA_Results::init();
		SSAA_Analyzer::init();
		SSAA_Site_Checks::init();

		// Engine adoption phase 2 (docs/SPEC-engine-adoption.md): the shared
		// render engine now owns rendering, the scan queue, content discovery,
		// and the exclusion store + its core-list-table bulk actions.
		// SSAA_Engine::boot() registers the render door, the queue cron, and the
		// bulk actions ATOMICALLY, on the same ssaa_* hooks/keys, so the old
		// SSAA_Renderer (door), SSAA_Scan_Queue (cron) and SSAA_List_Actions
		// (bulk actions) are NO LONGER initialised here - booting them too would
		// double-register and break the scan. SSAA_Content_Discovery::init() was
		// a no-op; its exclude/reinclude store is superseded by
		// SSAA_Engine_Exclusions (same ssaa_excluded_ids option, now post: refs).
		// Logging routes through SSAA_Logger (the shared-logging component, writes the same
		// ssaa.log); the old SSAA_Logger is now unused. All superseded classes
		// remain on disk until phase 4.
		// Deferred to init: engine_config() builds translated bulk-action
		// labels, and __() before init trips WP 6.7's just-in-time textdomain
		// notice. Everything the boot registers fires after init.
		add_action( 'init', array( 'SSAA_Scan', 'boot_engine' ), 1 );

		// NOTE: the update "homepage" URL repoint lives in
		// includes/class-updater-url__premium_only.php, NOT here. The
		// update-transient hook it uses is one Plugin Check bans in WP.org
		// builds, and it is only needed for the premium (Freemius-delivered)
		// build anyway (free updates come from WordPress.org). Keeping it in a
		// __premium_only file strips it from the free build so Plugin Check
		// passes. See _standards CONVENTIONS "Update-notice homepage URL".

		if ( is_admin() ) {
			SSAA_Admin::init();
		}
	}

	/**
	 * Plugin activation.
	 */
	public static function activate() {
		SSAA_Results::install();
	}

	/**
	 * Plugin deactivation.
	 */
	public static function deactivate() {
		SSAA_Scan::cancel();
		wp_clear_scheduled_hook( 'ssaa_recovery_scan' );
	}

	/**
	 * Canonical docs URL builder. Cross-plugin standard UTM scheme (see
	 * _standards CONVENTIONS.md "Admin UI standards"):
	 * ?utm_source=plugin&utm_medium={surface}&utm_campaign=docs_link
	 *
	 * @param string $path   Path under the plugin's docs section, no leading slash.
	 * @param string $medium Linking surface, e.g. 'settings_tab', 'logs_tab', 'dashboard'.
	 * @return string
	 */
	public static function docs_url( $path = '', $medium = '' ) {
		$url = 'https://docs.plugupp.com/' . SSAA_PLUGIN_SLUG . '/' . ltrim( $path, '/' );
		if ( '' !== $medium ) {
			$url = add_query_arg(
				array(
					'utm_source'   => 'plugin',
					'utm_medium'   => sanitize_key( $medium ),
					'utm_campaign' => 'docs_link',
				),
				$url
			);
		}
		return $url;
	}

	/**
	 * Standard admin footer: plugin name + version + brand byline
	 * (cross-plugin standard). Echoes escaped markup.
	 *
	 * @param string $medium utm_medium surface identifier.
	 */
	public static function render_admin_footer( $medium = 'admin_footer' ) {
		echo '<p class="ssaa-admin-footer">'
			. esc_html( sprintf( 'PlugUpp Search & Social Appearance Audit v%s', SSAA_VERSION ) )
			. ' ' . esc_html__( 'by', 'search-social-appearance-audit' ) . ' '
			. '<a href="' . esc_url( self::brand_url( $medium ) ) . '" target="_blank" rel="noopener">PlugUpp</a>'
			. '</p>';
	}

	/**
	 * Standard page header: H1 + right-aligned PlugUpp logo linking to
	 * plugupp.com (cross-plugin standard). Echoes escaped markup.
	 *
	 * @param string $title  Page title.
	 * @param string $medium utm_medium surface identifier.
	 */
	public static function render_page_header( $title, $medium ) {
		echo '<div class="ssaa-page-head">';
		echo '<h1>' . esc_html( $title ) . '</h1>';
		echo '<a class="ssaa-brand-logo" href="' . esc_url( self::brand_url( $medium ) ) . '" target="_blank" rel="noopener" aria-label="PlugUpp">';
		echo '<img src="' . esc_url( SSAA_PLUGIN_URL . 'assets/img/logo-plugupp.svg' ) . '" alt="PlugUpp" />';
		echo '</a>';
		echo '</div>';

		// Tell WordPress where admin notices belong. Without this marker,
		// wp-admin/js/common.js falls back to inserting notices as the next
		// sibling of the first <h1> inside .wrap - which, because our H1
		// lives inside the .ssaa-page-head flex container, places the
		// notice INSIDE that flex container as a third flex child between
		// the H1 and the PlugUpp logo. The result is a broken header row.
		// With this marker, WordPress inserts notices immediately after it
		// (outside the flex container), matching the standard placement
		// used on built-in admin pages like Plugins and Posts.
		echo '<hr class="wp-header-end">';
	}

	/**
	 * plugupp.com with the standard brand UTMs.
	 *
	 * @param string $medium utm_medium surface identifier.
	 * @return string
	 */
	public static function brand_url( $medium ) {
		return add_query_arg(
			array(
				'utm_source'   => 'plugin',
				'utm_medium'   => sanitize_key( $medium ),
				'utm_campaign' => 'brand_link',
			),
			'https://plugupp.com/'
		);
	}

	/**
	 * Standard top-right Documentation link (cross-plugin standard).
	 * Echoes escaped markup.
	 *
	 * @param string $medium utm_medium surface identifier.
	 */
	public static function render_docs_link( $medium ) {
		echo '<a class="ssaa-docs-link" href="' . esc_url( self::docs_url( '', $medium ) ) . '" target="_blank" rel="noopener">'
			. '<span class="dashicons dashicons-book" aria-hidden="true"></span>'
			. esc_html__( 'Documentation', 'search-social-appearance-audit' )
			. '<span class="dashicons dashicons-external" aria-hidden="true"></span></a>';
	}
}
