<?php
/**
 * Branded RSMSpark wp-admin notice chrome (peach panel, orange accent, Spark logo).
 *
 * @package Rsmspark_Suite
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Suite_Admin_Notice
 */
final class Rsmspark_Suite_Admin_Notice {

	const STYLE_HANDLE = 'rsmspark-suite-admin-notice';

	/**
	 * @var bool
	 */
	private static $inline_attached = false;

	/**
	 * Register hooks.
	 */
	public static function init() {
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_styles' ), 5 );
	}

	/**
	 * Register (and enqueue) a handle so notice CSS can be attached without raw echoes.
	 *
	 * @param string $hook_suffix Admin hook.
	 * @return void
	 */
	public static function register_styles( $hook_suffix ) {
		unset( $hook_suffix );

		if ( wp_style_is( self::STYLE_HANDLE, 'registered' ) ) {
			return;
		}

		wp_register_style( self::STYLE_HANDLE, false, array(), defined( 'RSMSPARK_SUITE_VERSION' ) ? RSMSPARK_SUITE_VERSION : '1.0.0' );
		wp_enqueue_style( self::STYLE_HANDLE );
		self::attach_inline_css();
	}

	/**
	 * @return void
	 */
	private static function attach_inline_css() {
		if ( self::$inline_attached ) {
			return;
		}
		self::$inline_attached = true;
		wp_add_inline_style( self::STYLE_HANDLE, self::css_rules() );
	}

	/**
	 * Render a branded admin notice.
	 *
	 * @param array<string, mixed> $args {
	 *     @type string $message   Required notice body (plain text unless html is true).
	 *     @type bool   $html      When true, message is passed through wp_kses_post().
	 *     @type array  $actions   Optional list of [ label, url, variant?, target?, rel? ].
	 *     @type string $logo_url  Optional logo; defaults to Spark news logo when found.
	 *     @type bool   $logo      Show logo (default true).
	 * }
	 */
	public static function render( array $args ) {
		$message = (string) ( $args['message'] ?? '' );
		if ( '' === $message ) {
			return;
		}

		if ( ! wp_style_is( self::STYLE_HANDLE, 'enqueued' ) && ! wp_style_is( self::STYLE_HANDLE, 'done' ) ) {
			wp_register_style( self::STYLE_HANDLE, false, array(), defined( 'RSMSPARK_SUITE_VERSION' ) ? RSMSPARK_SUITE_VERSION : '1.0.0' );
			wp_enqueue_style( self::STYLE_HANDLE );
			self::attach_inline_css();
			if ( function_exists( 'wp_print_styles' ) ) {
				wp_print_styles( self::STYLE_HANDLE );
			}
		}

		$actions   = is_array( $args['actions'] ?? null ) ? $args['actions'] : array();
		$html      = ! empty( $args['html'] );
		$show_logo = ! isset( $args['logo'] ) || (bool) $args['logo'];
		$logo_url  = (string) ( $args['logo_url'] ?? '' );
		if ( $show_logo && '' === $logo_url ) {
			$logo_url = self::default_logo_url();
		}

		echo '<div class="notice rsmspark-suite-admin-notice rsm-getting-started-notice" style="' . esc_attr( self::inline_style_attr() ) . '">';
		echo '<div class="rsmspark-suite-admin-notice__inner rsm-getting-started-notice__inner">';

		echo '<div class="rsmspark-suite-admin-notice__content rsm-getting-started-notice__content">';
		$message_tag = ! empty( $args['block'] ) ? 'div' : 'p';
		echo '<' . tag_escape( $message_tag ) . ' class="rsmspark-suite-admin-notice__message rsm-getting-started-notice__message">';
		if ( $html ) {
			echo wp_kses_post( $message );
		} else {
			echo esc_html( $message );
		}
		echo '</' . tag_escape( $message_tag ) . '>';

		if ( ! empty( $actions ) ) {
			echo '<p class="rsmspark-suite-admin-notice__actions rsm-getting-started-notice__actions">';
			foreach ( $actions as $action ) {
				if ( ! is_array( $action ) ) {
					continue;
				}
				$label   = (string) ( $action['label'] ?? '' );
				$url     = (string) ( $action['url'] ?? '' );
				if ( '' === $label || '' === $url ) {
					continue;
				}
				$variant = sanitize_key( (string) ( $action['variant'] ?? 'remind' ) );
				$target  = (string) ( $action['target'] ?? '' );
				$rel     = (string) ( $action['rel'] ?? '' );
				$class   = 'rsmspark-suite-admin-notice__action rsm-getting-started-notice__action rsmspark-suite-admin-notice__action--' . $variant;
				$class  .= ' rsm-getting-started-notice__action--' . $variant;

				echo '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '"';
				if ( '' !== $target ) {
					echo ' target="' . esc_attr( $target ) . '"';
				}
				if ( '' !== $rel ) {
					echo ' rel="' . esc_attr( $rel ) . '"';
				}
				echo '>' . esc_html( $label ) . '</a>';
			}
			echo '</p>';
		}

		echo '</div>';

		if ( $show_logo && '' !== $logo_url ) {
			echo '<div class="rsmspark-suite-admin-notice__brand rsm-getting-started-notice__brand">';
			echo '<img class="rsmspark-suite-admin-notice__logo rsm-getting-started-notice__logo" src="' . esc_url( $logo_url ) . '" alt="' . esc_attr__( 'RSM Spark', 'rsm-content-widgets' ) . '" loading="lazy" decoding="async" />';
			echo '</div>';
		}

		echo '</div></div>';
	}

	/**
	 * Resolve Spark news logo from suite/shared assets (no hardcoded sibling plugin folder).
	 *
	 * @return string
	 */
	public static function default_logo_url() {
		$files = array(
			'assets/images/rsm-spark-news-logo.webp',
			'assets/images/rsm-spark-news-logo.png',
			'assets/images/rsm-spark-news-logo.jpg',
			'assets/images/rsm-spark-news-logo.svg',
		);

		$pairs = array();

		if ( defined( 'RSMSPARK_SUITE_DIR' ) && defined( 'RSMSPARK_SUITE_URL' ) && '' !== RSMSPARK_SUITE_DIR && '' !== RSMSPARK_SUITE_URL ) {
			$pairs[] = array(
				'path' => trailingslashit( RSMSPARK_SUITE_DIR ),
				'url'  => trailingslashit( RSMSPARK_SUITE_URL ),
			);
		}

		if ( defined( 'RSMSPARK_SPARK_PATH' ) && defined( 'RSMSPARK_SPARK_URL' ) && '' !== RSMSPARK_SPARK_PATH && '' !== RSMSPARK_SPARK_URL ) {
			$pairs[] = array(
				'path' => trailingslashit( RSMSPARK_SPARK_PATH ),
				'url'  => trailingslashit( RSMSPARK_SPARK_URL ),
			);
		}

		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		if ( function_exists( 'get_plugins' ) ) {
			foreach ( get_plugins() as $basename => $data ) {
				unset( $data );
				$leaf = basename( (string) $basename );
				if ( 'rsm-spark.php' !== $leaf && 'rsm-spark-pro.php' !== $leaf ) {
					continue;
				}
				$main = WP_PLUGIN_DIR . '/' . $basename;
				if ( ! is_readable( $main ) ) {
					continue;
				}
				$pairs[] = array(
					'path' => trailingslashit( dirname( $main ) ),
					'url'  => trailingslashit( plugins_url( '/', $main ) ),
				);
				break;
			}
		}

		foreach ( $pairs as $pair ) {
			foreach ( $files as $file ) {
				$path = wp_normalize_path( $pair['path'] . $file );
				if ( is_readable( $path ) ) {
					return $pair['url'] . $file;
				}
			}
		}

		return '';
	}

	/**
	 * @return string
	 */
	public static function inline_style_attr() {
		return 'margin:12px 0 16px;padding:0;background:#fff6f1;border:1px solid #f0d4c4;border-left:4px solid #ff6b35;box-shadow:0 1px 2px rgba(0,0,0,.04)';
	}

	/**
	 * @return string
	 */
	public static function css_rules() {
		return '#wpbody-content .notice.rsmspark-suite-admin-notice,#wpbody-content .notice.rsm-getting-started-notice,.wrap .notice.rsmspark-suite-admin-notice,.wrap .notice.rsm-getting-started-notice{margin:12px 0 16px;padding:0;background:#fff6f1!important;border:1px solid #f0d4c4;border-left:4px solid #ff6b35!important;box-shadow:0 1px 2px rgba(0,0,0,.04)}.rsmspark-suite-admin-notice__inner,.rsm-getting-started-notice__inner{display:flex;align-items:stretch;gap:20px;padding:14px 18px}.rsmspark-suite-admin-notice__content,.rsm-getting-started-notice__content{flex:1 1 auto;min-width:0}.rsmspark-suite-admin-notice__brand,.rsm-getting-started-notice__brand{flex:0 0 auto;align-self:stretch;display:flex;align-items:center;justify-content:flex-end;margin-left:auto;max-width:40%;min-width:80px;padding-left:12px}.rsmspark-suite-admin-notice__logo,.rsm-getting-started-notice__logo{display:block;height:100%;max-height:100%;width:auto;max-width:160px;object-fit:contain;object-position:center right}.rsmspark-suite-admin-notice__message,.rsm-getting-started-notice__message{margin:0 0 12px;font-size:14px;line-height:1.5}.rsmspark-suite-admin-notice__actions,.rsm-getting-started-notice__actions{margin:0;display:flex;flex-wrap:wrap;gap:8px 18px}.rsmspark-suite-admin-notice__action,.rsm-getting-started-notice__action{font-weight:600;text-decoration:none}.rsmspark-suite-admin-notice__action--pro,.rsm-getting-started-notice__action--pro{color:#c44f00}.rsmspark-suite-admin-notice__action--pro:hover,.rsmspark-suite-admin-notice__action--pro:focus,.rsm-getting-started-notice__action--pro:hover,.rsm-getting-started-notice__action--pro:focus{color:#ff6b35;text-decoration:underline}.rsmspark-suite-admin-notice__action--remind,.rsmspark-suite-admin-notice__action--hide,.rsmspark-suite-admin-notice__action--primary,.rsm-getting-started-notice__action--remind,.rsm-getting-started-notice__action--hide{color:#1d2327}.rsmspark-suite-admin-notice__action--remind:hover,.rsmspark-suite-admin-notice__action--remind:focus,.rsmspark-suite-admin-notice__action--hide:hover,.rsmspark-suite-admin-notice__action--hide:focus,.rsmspark-suite-admin-notice__action--primary:hover,.rsmspark-suite-admin-notice__action--primary:focus,.rsm-getting-started-notice__action--remind:hover,.rsm-getting-started-notice__action--remind:focus,.rsm-getting-started-notice__action--hide:hover,.rsm-getting-started-notice__action--hide:focus{color:#ff6b35;text-decoration:underline}';
	}
}

if ( ! function_exists( 'rsmspark_suite_render_admin_notice' ) ) {
	/**
	 * Render a branded suite admin notice (peach panel + Spark logo).
	 *
	 * @param string               $message Notice body.
	 * @param array<string, mixed> $args    Optional args for Rsmspark_Suite_Admin_Notice::render().
	 * @return void
	 */
	function rsmspark_suite_render_admin_notice( $message, array $args = array() ) {
		$message = (string) $message;
		if ( '' === $message ) {
			return;
		}

		if ( class_exists( 'Rsmspark_Suite_Admin_Notice', false ) ) {
			$args['message'] = $message;
			Rsmspark_Suite_Admin_Notice::render( $args );
			return;
		}

		echo '<div class="notice notice-info"><p>' . esc_html( $message ) . '</p></div>';
	}
}

if ( ! function_exists( 'rsmspark_suite_render_license_hub_notice' ) ) {
	/**
	 * Standard suite licensing callout on per-plugin Licensing tabs.
	 *
	 * @param string               $text_domain Plugin text domain.
	 * @param array<string, mixed> $args        Optional: title (string), logo (bool).
	 * @return void
	 */
	function rsmspark_suite_render_license_hub_notice( $text_domain, array $args = array() ) {
		unset( $text_domain ); // Host plugin text domain; strings use suite domain (rewritten at package time).
		$title = isset( $args['title'] ) ? (string) $args['title'] : __( 'Suite license', 'rsm-content-widgets' );

		ob_start();
		echo '<strong>' . esc_html( $title ) . '</strong> — ';
		if ( class_exists( 'RSM_Spark_Admin', false ) ) {
			printf(
				wp_kses(
					/* translators: %s: Spark PRO settings URL */
					__( 'Activate one license for the whole suite under %s. Per-plugin activation below is optional.', 'rsm-content-widgets' ),
					array( 'a' => array( 'href' => array() ) )
				),
				'<a href="' . esc_url( admin_url( 'admin.php?page=rsm-spark&tab=pro' ) ) . '">' . esc_html__( 'RSM → Spark → PRO', 'rsm-content-widgets' ) . '</a>'
			);
		} else {
			echo esc_html__( 'Install RSM Spark to activate one license for the whole suite from Spark → PRO.', 'rsm-content-widgets' );
		}

		$notice_args = array(
			'html' => true,
			'logo' => false,
		);
		if ( isset( $args['logo'] ) ) {
			$notice_args['logo'] = (bool) $args['logo'];
		}

		rsmspark_suite_render_admin_notice( (string) ob_get_clean(), $notice_args );
	}
}
