<?php
/**
 * File Path: includes/helpers/class-mbs-cover-renderer.php
 *
 * Cover Renderer
 *
 * Central class for rendering every book cover using the authoritative 4-level hierarchy.
 * Must be used for EVERY cover display (R5 rule). Implements spinning video (PRO),
 * format-specific covers with CSS metaphors (PRO), book default cover, and placeholder fallback.
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

class MBS_Cover_Renderer { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Render the correct cover for a book / format using the 4-level hierarchy.
	 *
	 * Level 1 (highest): Spinning book video (PRO)
	 * Level 2: Format-specific cover image (PRO)
	 * Level 3: Book default cover (FREE)
	 * Level 4: Plugin placeholder (FREE fallback)
	 *
	 * @param ?object  $format_row  Format row object from MBS_Format_Row_DAO (optional).
	 * @return string               Complete cover HTML.
	 */
	public static function render( int $post_id, ?object $format_row = null ): string {
		if ( ! mbs_is_registered() ) {
			return '';
		}

		// Level 1: Spinning book video (PRO only) — hover-play overlay.
		// The cover image is always rendered alongside the video so the book cover
		// remains visible at rest. On hover, JS plays the video (opacity 1) and
		// hides the image (opacity 0); on mouseleave JS pauses + resets.
		// autoplay is intentionally omitted — this is always a hover-play experience.
		if ( mbs_is_pro() ) {
			$video_id = null;
			if ( $format_row && ! empty( $format_row->spinning_video_id ) ) {
				$video_id = (int) $format_row->spinning_video_id;
			} else {
				$video_id = (int) get_post_meta( $post_id, 'mbs_spinning_video_id', true );
			}

			if ( $video_id ) {
				$video_url = wp_get_attachment_url( $video_id );
				if ( $video_url ) {
					// Resolve the fallback cover image URL (same Level 2/3/4 logic)
					// so the static cover is visible before/after hover.
					$cover_img_url = '';
					if ( $format_row && ! empty( $format_row->cover_image_id ) ) {
						$cover_img_url = (string) wp_get_attachment_url( (int) $format_row->cover_image_id );
					}
					if ( ! $cover_img_url ) {
						$thumb_id = get_post_thumbnail_id( $post_id );
						if ( $thumb_id ) {
							$cover_img_url = (string) wp_get_attachment_url( $thumb_id );
						}
					}
					if ( ! $cover_img_url ) {
						$cover_img_url = MBS_ASSETS_URL . 'images/placeholder.svg';
					}

					$title = $post_id ? get_the_title( $post_id ) : '';
					$alt   = $title ? esc_attr( $title ) . ' book cover' : 'Book cover image';

					$inner = sprintf(
						'<div class="mbs-cover mbs-cover-video-hover">' .
							'<img src="%s" alt="%s" loading="lazy" class="mbs-cover-img">' .
							'<video src="%s" loop muted playsinline preload="auto" class="mbs-cover-video"></video>' .
						'</div>',
						esc_url( $cover_img_url ),
						$alt,
						esc_url( $video_url )
					);
					return '<div class="mbs-wrapper">' . $inner . '</div>';
				}
			}
		}

		// Level 2: Format-specific cover (PRO only).
		// Only used when a $format_row is explicitly passed (e.g. single-book page
		// format tab). Shelf/grid contexts pass no $format_row and fall through to
		// Level 3 (WP featured image) so each book shows its own canonical cover.
		if ( mbs_is_pro() && $format_row && ! empty( $format_row->cover_image_id ) ) {
			$cover_url = wp_get_attachment_url( (int) $format_row->cover_image_id );
			if ( $cover_url ) {
				return self::render_cover_image( $cover_url, $format_row->format ?? 'other', $post_id );
			}
		}

		// Level 3: Book default cover (FREE)
		// Cover is stored as WordPress featured image (set_post_thumbnail) by MBS_Book_Header_Cover.
		$default_cover_id = get_post_thumbnail_id( $post_id );
		if ( $default_cover_id ) {
			$cover_url = wp_get_attachment_url( $default_cover_id );
			if ( $cover_url ) {
				return self::render_cover_image( $cover_url, 'default', $post_id );
			}
		}

		// Level 4: Plugin placeholder (FREE fallback)
		$placeholder_url = MBS_ASSETS_URL . 'images/placeholder.svg';
		return self::render_cover_image( $placeholder_url, 'default', $post_id );
	}

	/**
	 * Render a static cover image with the appropriate CSS metaphor.
	 *
	 * @param string $type     Format type for CSS metaphor.
	 * @param int    $post_id  Book post ID (used for descriptive alt text).
	 * @return string          HTML.
	 */
	private static function render_cover_image( string $url, string $type, int $post_id = 0 ): string {
		$classes = 'mbs-cover';
		switch ( $type ) {
			case 'ebook':
				$classes .= ' mbs-cover-ebook';
				break;
			case 'paperback':
				$classes .= ' mbs-cover-paperback';
				break;
			case 'hardcover':
				$classes .= ' mbs-cover-hardcover';
				break;
			case 'audiobook':
				$classes .= ' mbs-cover-audiobook';
				break;
			case 'boardbook':
				$classes .= ' mbs-cover-boardbook';
				break;
			case 'special':
				$classes .= ' mbs-cover-special';
				break;
			default:
				$classes .= ' mbs-cover-default';
		}

		// Dynamic descriptive alt text for WCAG 2.1 AA compliance (ADV-03)
		$title = $post_id ? get_the_title( $post_id ) : '';
		$alt   = $title 
			? esc_attr( $title ) . ' book cover' 
			: 'Book cover image';

		// FREE-tier watermark overlay (anti-tamper rule per test spec).
		$watermark = ! mbs_is_pro()
			? '<span class="mbs-watermark" aria-hidden="true"></span>'
			: '';

		$inner = sprintf(
			'<div class="%s"><img src="%s" alt="%s" loading="lazy">%s</div>',
			esc_attr( $classes ),
			esc_url( $url ),
			$alt,
			$watermark
		);

		return '<div class="mbs-wrapper">' . $inner . '</div>';
	}
}
