<?php
/**
 * File Path: includes/admin/class-mbs-quote-widget.php
 *
 * My Book Showroom — Daily Quote Widget
 *
 * Renders the daily inspirational quote in two admin surfaces:
 *   1. Dashboard — hooked to mbs_dashboard_after_grid action.
 *   2. Settings tabs — hooked to mbs_tab_footer action (Option C).
 *
 * No PRO gate: admin decoration is available on all tiers.
 *
 * @package MyBookShowroom
 * @since   1.1.0
 */

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

class MBS_Quote_Widget {

	// ── Bootstrap ─────────────────────────────────────────────────────────────

	/**
	 * Register all admin hooks. Called once from MBS_Plugin::init().
	 */
	public static function init(): void {
		add_action( 'admin_enqueue_scripts',  array( __CLASS__, 'enqueue_assets' ) );
		add_action( 'mbs_dashboard_after_grid', array( __CLASS__, 'render_dashboard_card' ) );
		add_action( 'mbs_tab_footer',          array( __CLASS__, 'render_tab_panel' ) );
	}

	// ── Asset Enqueueing ──────────────────────────────────────────────────────

	/**
	 * Enqueue mbs-quote.css on the MBS dashboard and all MBS settings pages.
	 *
	 * @param string $hook_suffix Current admin page hook suffix.
	 */
	public static function enqueue_assets( string $hook_suffix ): void {
		$is_dashboard = ( 'toplevel_page_my-book-showroom' === $hook_suffix );
		$is_settings  = ( strpos( $hook_suffix, 'my-book-showroom' ) !== false );

		if ( ! $is_dashboard && ! $is_settings ) {
			return;
		}

		wp_enqueue_style(
			'mbs-quote',
			MBS_ASSETS_URL . 'css/mbs-quote.css',
			array(),
			MBS_VERSION
		);
	}

	// ── Render: Dashboard ─────────────────────────────────────────────────────

	/**
	 * Render the quote card below the dashboard grid.
	 *
	 * Hooked to: mbs_dashboard_after_grid (no arguments).
	 */
	public static function render_dashboard_card(): void {
		if ( ! class_exists( 'MBS_Quote_Library' ) ) {
			return;
		}

		$quote = MBS_Quote_Library::get_today();
		?>
		<div class="mbs-quote-strip mbs-quote-strip--dashboard">
			<blockquote class="mbs-quote-body">
				<p class="mbs-quote-text"><?php echo esc_html( $quote['text'] ); ?></p>
				<?php if ( ! empty( $quote['author'] ) ) : ?>
					<cite class="mbs-quote-cite">&mdash; <?php echo esc_html( $quote['author'] ); ?></cite>
				<?php endif; ?>
			</blockquote>
			<p class="mbs-quote-label"><?php esc_html_e( 'Daily Inspiration', 'my-book-showroom' ); ?></p>
		</div>
		<?php
	}

	// ── Render: Settings Tab Footer ───────────────────────────────────────────

	/**
	 * Render the quote panel below the active settings tab content.
	 *
	 * Hooked to: mbs_tab_footer( string $tab_slug ).
	 *
	 * @param string $tab_slug The active tab slug (passed by do_action).
	 */
	public static function render_tab_panel( string $tab_slug = '' ): void {
		if ( ! class_exists( 'MBS_Quote_Library' ) ) {
			return;
		}

		$quote = MBS_Quote_Library::get_today();
		?>
		<div class="mbs-quote-strip mbs-quote-strip--tab">
			<blockquote class="mbs-quote-body">
				<p class="mbs-quote-text"><?php echo esc_html( $quote['text'] ); ?></p>
				<?php if ( ! empty( $quote['author'] ) ) : ?>
					<cite class="mbs-quote-cite">&mdash; <?php echo esc_html( $quote['author'] ); ?></cite>
				<?php endif; ?>
			</blockquote>
			<p class="mbs-quote-label"><?php esc_html_e( 'Daily Inspiration', 'my-book-showroom' ); ?></p>
		</div>
		<?php
	}
}
