<?php

namespace SearchEngineVisibilityWarning;

use WP_PowerSuite\Core\AbstractModule;
use WP_PowerSuite\Core\Contracts\DataLocationsInterface;
use WP_PowerSuite\Core\NetworkScope;

class Module extends AbstractModule implements DataLocationsInterface {

	private const MODULE_ID = 'search-engine-visibility-warning';
	private const MODULE_VERSION = '1.0.0';

	public function getId(): string {
		return self::MODULE_ID;
	}

	public function getName(): string {
		return __( 'Search Engine Visibility Warning', 'wp-powersuite' );
	}

	public function getVersion(): string {
		return self::MODULE_VERSION;
	}

	public function getDescription(): string {
		return __( 'Toolbar reminder when “discourage search engines” is on, with a quick path to change it.', 'wp-powersuite' );
	}

	public function getContext(): string|array {
		return 'both';
	}

	protected function registerHooks(): void {
		if ( ! is_user_logged_in() || ! current_user_can( NetworkScope::adminCapability() ) ) {
			return;
		}
		add_action( 'admin_bar_menu', [ $this, 'addAdminBarNotice' ], 999 );
		add_action( 'wp_head', [ $this, 'addAdminBarStyles' ], 999 );
		add_action( 'admin_head', [ $this, 'addAdminBarStyles' ], 999 );
	}

	public function getDataLocations(): array {
		return [
			'options' => [],
			'tables'  => [],
		];
	}

	/**
	 * Whether the visibility warning should display for the current environment.
	 */
	private function shouldShowWarning(): bool {
		if ( (int) get_option( 'blog_public', 1 ) !== 0 ) {
			return false;
		}
		return ! self::isNonProductionEnvironment();
	}

	/**
	 * Whether the site runs in staging, local, or development.
	 */
	public static function isNonProductionEnvironment(): bool {
		if ( defined( 'WP_ENV' ) ) {
			$wp_env = strtolower( (string) WP_ENV );
			if ( in_array( $wp_env, [ 'staging', 'local', 'dev', 'development' ], true ) ) {
				return true;
			}
		}

		$env_type = function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production';
		if ( in_array( $env_type, [ 'staging', 'local', 'development' ], true ) ) {
			return true;
		}

		if ( $env_type === 'production' ) {
			$host        = isset( $_SERVER['HTTP_HOST'] ) ? strtolower( (string) $_SERVER['HTTP_HOST'] ) : '';
			$server_addr = isset( $_SERVER['SERVER_ADDR'] ) ? (string) $_SERVER['SERVER_ADDR'] : '';
			if ( in_array( $host, [ 'localhost', '127.0.0.1' ], true ) ||
				str_contains( $host, '.local' ) ||
				str_contains( $host, '.test' ) ||
				in_array( $server_addr, [ '127.0.0.1', '::1' ], true ) ||
				str_starts_with( $server_addr, '192.168.' ) ) {
				return true;
			}
		}

		return false;
	}

	public function addAdminBarNotice( \WP_Admin_Bar $wp_admin_bar ): void {
		if ( ! $this->shouldShowWarning() ) {
			return;
		}
		$icon = '<span class="wpps-sev-warning-icon"><svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M10 2L2 18h16L10 2zm0 3.5l5.5 11h-11L10 5.5zM9 8v4h2V8H9zm0 5v2h2v-2H9z"/></svg></span>';
		/* Mobile text must NOT live inside .ab-label: core applies clip-path + visually hidden to .ab-label at max-width 782px. */
		$label_desktop = '<span class="ab-label"><span class="wpps-sev-label-desktop">' . esc_html__( 'Search engines discouraged', 'wp-powersuite' ) . '</span></span>';
		$label_mobile  = '<span class="wpps-sev-label-mobile">' . esc_html__( 'SE: OFF', 'wp-powersuite' ) . '</span>';
		$wp_admin_bar->add_node( [
			'id'     => 'wpps-search-engine-visibility-warning',
			/* Root-level so the warning stays available on narrow viewports (top-secondary is often hidden on mobile). */
			'parent' => false,
			'title'  => $icon . $label_desktop . $label_mobile,
			'href'   => NetworkScope::adminUrl( 'options-reading.php' ),
			'meta'   => [ 'title' => __( 'Open Reading settings to fix.', 'wp-powersuite' ) ],
		] );
	}

	public function addAdminBarStyles(): void {
		if ( ! $this->shouldShowWarning() ) {
			return;
		}
		static $printed = false;
		if ( $printed ) {
			return;
		}
		$printed = true;
		?>
		<style id="wpps-search-engine-visibility-warning-styles">
		#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning > .ab-item {
			background: #d63638 !important;
			color: #fff !important;
			animation: wpps-sev-pulse 2s ease-in-out 3;
		}
		#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning:hover > .ab-item {
			background: #b32d2e !important;
			color: #fff !important;
		}
		#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning .ab-item .wpps-sev-warning-icon {
			display: inline-flex;
			align-items: center;
			margin-right: 6px;
		}
		#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning .ab-item .wpps-sev-warning-icon svg {
			fill: currentColor;
		}
		#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning .wpps-sev-label-mobile {
			display: none;
		}
		/* WordPress hides many top-secondary bar items on narrow viewports; keep the warning visible. */
		@media screen and (max-width: 782px) {
			#wpadminbar #wp-admin-bar-root-default > #wp-admin-bar-wpps-search-engine-visibility-warning,
			#wpadminbar #wp-admin-bar-top-secondary > #wp-admin-bar-wpps-search-engine-visibility-warning,
			#wpadminbar #wp-toolbar > ul > li#wp-admin-bar-wpps-search-engine-visibility-warning {
				display: list-item !important;
				position: relative !important;
			}
			/* Core mobile admin bar sets dark text on links; re-assert contrast on red background. */
			#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning > a.ab-item,
			#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning > .ab-item {
				display: flex !important;
				align-items: center !important;
				min-width: auto !important;
				height: 46px !important;
				padding-left: 10px !important;
				padding-right: 10px !important;
				background: #d63638 !important;
				color: #fff !important;
			}
			#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning:hover > a.ab-item,
			#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning:hover > .ab-item {
				background: #b32d2e !important;
				color: #fff !important;
			}
			/* Core applies clip-path visually-hidden rules to .ab-label on narrow screens; desktop label stays in .ab-label only. */
			#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning .ab-label {
				display: none !important;
			}
			#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning > a.ab-item *,
			#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning > .ab-item * {
				color: #fff !important;
				-webkit-text-fill-color: #fff !important;
			}
			#wpadminbar #wp-admin-bar-wpps-search-engine-visibility-warning .wpps-sev-label-mobile {
				display: inline !important;
				font-size: 12px !important;
				line-height: 1.2 !important;
				white-space: nowrap !important;
			}
		}
		@keyframes wpps-sev-pulse {
			0%, 100% { opacity: 1; }
			50% { opacity: 0.7; }
		}
		</style>
		<?php
	}
}
