<?php
/**
 * File Path: includes/helpers/cover-strategies/class-mbs-cover-strategy-video.php
 *
 * Cover Strategy — Spinning Video (Level 1, PRO)
 *
 * Highest-priority strategy in the cover resolution chain. Renders a spinning
 * book video when a spinning_video_id is present on the format row or as
 * post meta. Supports hover-play and auto-play modes via the video_hover_mode flag.
 * Returns '' immediately when the site is not on a PRO license, when no video ID
 * is set, or when the attachment URL cannot be resolved.
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Helpers/Cover_Strategies
 * @since      1.0.0
 */

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

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

	/**
	 * @inheritDoc
	 */
	public static function resolve( int $post_id, ?object $format_row ): string {
		if ( ! mbs_is_pro() ) {
			return '';
		}

		// Format row takes precedence over post meta for spinning_video_id.
		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 ) {
			return '';
		}

		$video_url = wp_get_attachment_url( $video_id );
		if ( ! $video_url ) {
			return '';
		}

		$hover_mode = ! empty( $format_row->video_hover_mode )
			|| get_post_meta( $post_id, 'mbs_video_hover_mode', true );

		// Resolve static fallback cover image (Level 2 → Level 3 → placeholder).
		// This ensures a visible cover at rest and for screen readers / non-video contexts.
		$cover_img_url = '';
		if ( $format_row && ! empty( $format_row->cover_image_id ) ) {
			$cover_img_url = (string) wp_get_attachment_image_url( (int) $format_row->cover_image_id, 'medium' ); // R5-exempt: IS the MBS_Cover_Renderer video strategy
		}
		if ( ! $cover_img_url ) {
			$thumb_id = get_post_thumbnail_id( $post_id ); // R5-exempt: IS the MBS_Cover_Renderer video strategy
			if ( $thumb_id ) {
				$cover_img_url = (string) wp_get_attachment_image_url( $thumb_id, 'medium' ); // R5-exempt: IS the MBS_Cover_Renderer video strategy
			}
		}
		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 ) . ' ' . esc_attr__( 'book cover', 'my-book-showroom' )
			: esc_attr__( 'Book cover image', 'my-book-showroom' );

		$class   = $hover_mode ? 'mbs-cover mbs-cover-video-hover' : 'mbs-cover mbs-cover-video-auto';
		$autoplay = $hover_mode ? '' : ' autoplay';

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

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