<?php

namespace CelerSearch\Admin;

defined( 'ABSPATH' ) || exit;

use CelerSearch\Interfaces\IRegistrable;

/**
 * Overrides the wp-admin footer text on CelerSearch admin pages only.
 *
 * Shows a short "Thank you for using CelerSearch — ★★★★★ Leave a review"
 * line in the native footer slot. This is the WordPress.org convention for
 * self-promoted plugin feedback: persistent, non-intrusive, no dismiss.
 * Invisible on any other wp-admin screen.
 */
class FooterRating implements IRegistrable {

	const PAGE_SLUG = 'celersearch';

	public function register(): void {
		add_filter( 'admin_footer_text', [ $this, 'render' ], 99 );
	}

	public function render( $default_text ) {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Just checking admin page context
		$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
		if ( self::PAGE_SLUG !== $page ) {
			return $default_text;
		}

		$review_url = defined( 'CELERSEARCH_REVIEW_URL' )
			? CELERSEARCH_REVIEW_URL
			: 'https://wordpress.org/support/plugin/celersearch/reviews/#new-post';

		$stars = '<span class="cs-footer-rating__stars" aria-hidden="true">★★★★★</span>';

		$link = sprintf(
			'<a href="%1$s" target="_blank" rel="noopener noreferrer" class="cs-footer-rating__link">%2$s %3$s</a>',
			esc_url( $review_url ),
			$stars,
			esc_html__( 'Leave a review on WordPress.org', 'celersearch' )
		);

		$text = sprintf(
			/* translators: 1: Plugin name (bold), 2: Review link with stars and label */
			esc_html__( 'Thank you for using %1$s — %2$s', 'celersearch' ),
			'<strong>CelerSearch</strong>',
			$link
		);

		// Inline <style> scoped via unique classnames — avoids touching the
		// built CSS bundle just for five stars in the footer.
		$style = '<style>'
			. '.cs-footer-rating__stars{color:#ffb900;letter-spacing:2px;margin-right:4px;vertical-align:middle;}'
			. '.cs-footer-rating__link{text-decoration:none;}'
			. '.cs-footer-rating__link:hover .cs-footer-rating__stars{filter:brightness(1.1);}'
			. '</style>';

		return $style . $text;
	}
}
