<?php
/**
 * Wave Access – Backend-Banner (nur Free-Version).
 * Holt zentral gepflegte Hinweise vom Wave-Tools-Server (12-h-Cache) und zeigt
 * sie Administratoren als wegklickbare Notice im WP-Backend. Pro-Lizenzen
 * sehen nie Banner. Jeder Banner ist einzeln und dauerhaft ausblendbar.
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

class Wave_Banner {

	const ENDPOINT  = 'https://wavetools.io/api/wave-banner/v1/current';
	const CACHE_KEY = 'wave_banner_cache';
	const CACHE_TTL = 12 * HOUR_IN_SECONDS;
	const DISMISSED = 'wave_banners_dismissed';

	public static function init() {
		if ( ! is_admin() ) {
			return;
		}
		add_action( 'admin_init', array( __CLASS__, 'handle_dismiss' ) );
		add_action( 'admin_notices', array( __CLASS__, 'render' ) );
	}

	/** Banner nur in der Free-Version und nur für Admins. */
	private static function should_show() {
		if ( class_exists( 'Wave_License' ) && Wave_License::is_pro() ) {
			return false;
		}
		return current_user_can( 'manage_options' );
	}

	/** Banner vom Server holen (Transient-Cache, auch negativ). */
	private static function get_banners() {
		$cached = get_transient( self::CACHE_KEY );
		if ( false !== $cached ) {
			return is_array( $cached ) ? $cached : array();
		}
		$lang = substr( (string) get_bloginfo( 'language' ), 0, 2 );
		$resp = wp_remote_get(
			add_query_arg(
				array( 'lang' => $lang, 'product' => 'wave-access', 'version' => WAVE_VERSION ),
				self::ENDPOINT
			),
			array( 'timeout' => 8 )
		);
		$banners = array();
		if ( ! is_wp_error( $resp ) && 200 === (int) wp_remote_retrieve_response_code( $resp ) ) {
			$json = json_decode( wp_remote_retrieve_body( $resp ), true );
			if ( ! empty( $json['ok'] ) && ! empty( $json['banners'] ) && is_array( $json['banners'] ) ) {
				$banners = $json['banners'];
			}
			set_transient( self::CACHE_KEY, $banners, self::CACHE_TTL );
		} else {
			// Negativ-Cache: Server nicht erreichbar → 1 h Ruhe.
			set_transient( self::CACHE_KEY, array(), HOUR_IN_SECONDS );
		}
		return $banners;
	}

	public static function handle_dismiss() {
		if ( ! isset( $_GET['wave_dismiss_banner'] ) || ! current_user_can( 'manage_options' ) ) {
			return;
		}
		if ( ! wp_verify_nonce( isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '', 'wave_dismiss_banner' ) ) {
			return;
		}
		$id        = (int) $_GET['wave_dismiss_banner'];
		$dismissed = get_option( self::DISMISSED, array() );
		$dismissed = is_array( $dismissed ) ? $dismissed : array();
		if ( ! in_array( $id, $dismissed, true ) ) {
			$dismissed[] = $id;
			update_option( self::DISMISSED, $dismissed, false );
		}
		wp_safe_redirect( remove_query_arg( array( 'wave_dismiss_banner', '_wpnonce' ) ) );
		exit;
	}

	public static function render() {
		if ( ! self::should_show() ) {
			return;
		}
		$banners = self::get_banners();
		if ( ! $banners ) {
			return;
		}
		$dismissed = get_option( self::DISMISSED, array() );
		$dismissed = is_array( $dismissed ) ? $dismissed : array();

		foreach ( $banners as $b ) {
			$id = isset( $b['id'] ) ? (int) $b['id'] : 0;
			if ( ! $id || in_array( $id, $dismissed, true ) || empty( $b['title'] ) ) {
				continue;
			}
			$dismiss_url = wp_nonce_url( add_query_arg( 'wave_dismiss_banner', $id ), 'wave_dismiss_banner' );
			?>
			<div class="notice" style="border-left-color:#0891b2;padding:14px 44px 14px 16px;position:relative">
				<p style="margin:0 0 2px;font-size:14px"><strong><?php echo esc_html( $b['title'] ); ?></strong></p>
				<p style="margin:0;color:#50575e"><?php echo esc_html( isset( $b['text'] ) ? $b['text'] : '' ); ?>
					<?php if ( ! empty( $b['cta_url'] ) ) : ?>
						&nbsp;<a class="button button-primary" style="margin-left:8px;vertical-align:baseline" href="<?php echo esc_url( $b['cta_url'] ); ?>" target="_blank" rel="noopener">
							<?php echo esc_html( ! empty( $b['cta_label'] ) ? $b['cta_label'] : __( 'Mehr erfahren', 'wave-access' ) ); ?>
						</a>
					<?php endif; ?>
				</p>
				<a href="<?php echo esc_url( $dismiss_url ); ?>" style="position:absolute;top:10px;right:12px;text-decoration:none;font-size:16px;color:#787c82" aria-label="<?php esc_attr_e( 'Hinweis dauerhaft ausblenden', 'wave-access' ); ?>">&#10005;</a>
			</div>
			<?php
			break; // höchstens ein Banner gleichzeitig
		}
	}
}

Wave_Banner::init();
