<?php
/**
 * Archive card: Fullpage snap-scroll mode.
 *
 * Each post occupies one full viewport height. Media (image/video)
 * fills the upper portion; post info overlays the bottom.
 * First two posts load eagerly; JS preloads adjacent slides.
 *
 * @package FansPaysite
 * @since   2.3
 */

$format = get_post_format() ?: 'standard';

// Extract embedded media for video/audio formats.
$media_html = '';
if ( in_array( $format, array( 'video', 'audio' ), true ) ) {
	$content = apply_filters( 'the_content', get_the_content() );
	$types   = ( 'video' === $format )
		? array( 'video', 'object', 'embed', 'iframe' )
		: array( 'audio', 'iframe' );
	$found   = get_media_embedded_in_content( $content, $types );
	if ( ! empty( $found ) ) {
		$media_html = $found[0];
	}
}

// Link format URL.
$link_url = '';
if ( 'link' === $format ) {
	$link_url = get_url_in_content( get_the_content() ) ?: get_the_permalink();
}

// Loading strategy: first 2 posts eager, rest lazy (JS handles preloading).
global $wp_query;
$current_index = $wp_query->current_post;
$loading_attr  = ( $current_index <= 1 ) ? 'eager' : 'lazy';
?>

<?php do_action( 'fanspaysite_post_card_before', get_the_ID(), $format, 'fullpage' ); ?>

<section class="fps-fullpage-slide" id="fps-slide-<?php the_ID(); ?>" data-post-index="<?php echo esc_attr( $current_index ); ?>">

	<div class="fps-fullpage-media">
		<?php if ( $media_html && 'video' === $format ) : ?>
			<div class="fps-fullpage-video">
				<?php echo $media_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			</div>
		<?php elseif ( $media_html && 'audio' === $format ) : ?>
			<div class="fps-fullpage-audio-wrap">
				<?php if ( has_post_thumbnail() ) : ?>
					<?php the_post_thumbnail( 'large', array( 'class' => 'fps-fullpage-bg', 'loading' => $loading_attr ) ); ?>
				<?php endif; ?>
				<div class="fps-fullpage-audio">
					<?php echo $media_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				</div>
			</div>
		<?php elseif ( has_post_thumbnail() ) : ?>
			<?php the_post_thumbnail( 'large', array( 'class' => 'fps-fullpage-bg', 'loading' => $loading_attr ) ); ?>
		<?php else : ?>
			<?php
			$default_thumbnail = get_theme_mod( 'default_post_thumbnail', get_template_directory_uri() . '/default.png' );
			echo '<img src="' . esc_url( $default_thumbnail ) . '" class="fps-fullpage-bg" alt="' . esc_attr( get_the_title() ) . '" loading="' . esc_attr( $loading_attr ) . '">';
			?>
		<?php endif; ?>
	</div>

	<div class="fps-fullpage-overlay">
		<div class="fps-fullpage-info">
			<?php if ( 'quote' === $format ) : ?>
				<blockquote>
					<?php the_excerpt(); ?>
					<?php if ( get_the_title() ) : ?>
						<cite><?php the_title(); ?></cite>
					<?php endif; ?>
				</blockquote>
			<?php elseif ( 'link' === $format ) : ?>
				<?php if ( get_the_title() ) : ?>
					<h2><a href="<?php echo esc_url( $link_url ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
				<?php endif; ?>
				<span class="fps-format-meta" style="color:#fff;"><?php echo esc_html( wp_parse_url( $link_url, PHP_URL_HOST ) ); ?></span>
			<?php else : ?>
				<?php if ( get_the_title() ) : ?>
					<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
				<?php endif; ?>
				<?php if ( 'aside' !== $format ) : ?>
					<div class="fps-fullpage-excerpt"><?php the_excerpt(); ?></div>
				<?php endif; ?>
			<?php endif; ?>

			<?php do_action( 'fanspaysite_post_card_meta', get_the_ID(), $format, 'fullpage' ); ?>

			<div class="fps-card-actions">
				<a href="<?php the_permalink(); ?>" class="ui button secondary tiny"><?php esc_html_e( 'More', 'fanspaysite' ); ?></a>

				<?php if ( comments_open() || get_comments_number() ) : ?>
					<a href="<?php comments_link(); ?>" class="fps-comments-count" aria-label="<?php echo esc_attr( sprintf( __( '%d comments', 'fanspaysite' ), get_comments_number() ) ); ?>">
						<i class="fas fa-comment"></i> <?php echo esc_html( get_comments_number() ); ?>
					</a>
				<?php endif; ?>

				<?php do_action( 'fanspaysite_post_card_actions', get_the_ID(), $format, 'fullpage' ); ?>
			</div>
		</div>
	</div>

</section>

<?php do_action( 'fanspaysite_post_card_after', get_the_ID(), $format, 'fullpage' ); ?>
