<?php
/**
 * Archive card: Boxes (grid) mode.
 *
 * Compact card for a 2-3 column responsive grid. Shows media preview,
 * author avatar, title, excerpt snippet, and action buttons.
 *
 * @package FansPaysite
 * @since   2.3
 */

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

// Extract embedded media for video and 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];
	}
}

// Gallery image count.
$gallery_count = 0;
if ( 'gallery' === $format ) {
	$galleries = get_post_galleries_images( get_the_ID() );
	foreach ( $galleries as $gallery ) {
		$gallery_count += count( $gallery );
	}
}

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

// Format icon map.
$format_icons = array(
	'video'   => 'fa-video',
	'audio'   => 'fa-music',
	'image'   => 'fa-image',
	'gallery' => 'fa-images',
	'link'    => 'fa-link',
	'quote'   => 'fa-quote-left',
	'aside'   => 'fa-sticky-note',
);
$icon_class = $format_icons[ $format ] ?? '';
?>

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

<article id="post-<?php the_ID(); ?>" <?php post_class( 'fps-box-card' . ( 'standard' !== $format ? ' fps-format-' . $format : '' ) ); ?>>

	<div class="fps-box-media">
		<?php if ( $media_html && 'video' === $format ) : ?>
			<div class="fps-responsive-embed">
				<?php echo $media_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			</div>
		<?php elseif ( has_post_thumbnail() ) : ?>
			<a href="<?php the_permalink(); ?>" aria-label="<?php echo esc_attr( sprintf( __( 'View %s', 'fanspaysite' ), get_the_title() ) ); ?>">
				<?php the_post_thumbnail( 'medium_large', array( 'class' => 'fps-box-thumb', 'loading' => 'lazy' ) ); ?>
			</a>
			<?php if ( 'gallery' === $format && $gallery_count > 0 ) : ?>
				<span class="fps-gallery-count"><i class="fas fa-images"></i> <?php echo esc_html( $gallery_count ); ?></span>
			<?php endif; ?>
		<?php else : ?>
			<a href="<?php the_permalink(); ?>" aria-label="<?php echo esc_attr( sprintf( __( 'View %s', 'fanspaysite' ), get_the_title() ) ); ?>">
				<?php
				$default_thumbnail = get_theme_mod( 'default_post_thumbnail', get_template_directory_uri() . '/default.png' );
				echo '<img src="' . esc_url( $default_thumbnail ) . '" class="fps-box-thumb" alt="' . esc_attr( get_the_title() ) . '" loading="lazy">';
				?>
			</a>
		<?php endif; ?>
		<?php if ( 'audio' === $format && $media_html ) : ?>
			<div class="fps-box-audio-overlay">
				<?php echo $media_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			</div>
		<?php endif; ?>
		<?php if ( $icon_class ) : ?>
			<span class="fps-box-format-badge"><i class="fas <?php echo esc_attr( $icon_class ); ?>"></i></span>
		<?php endif; ?>
	</div>

	<div class="fps-box-body">
		<div class="fps-box-author">
			<?php echo get_avatar( get_the_author_meta( 'ID' ), 28, '', '', array( 'class' => 'fps-box-avatar' ) ); ?>
			<span class="fps-box-author-name"><?php the_author(); ?></span>
			<span class="fps-box-date"><?php echo esc_html( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) ); ?></span>
		</div>

		<?php if ( 'quote' === $format ) : ?>
			<blockquote class="fps-box-quote"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 20 ) ); ?></blockquote>
		<?php elseif ( 'aside' === $format ) : ?>
			<div class="fps-box-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 20 ) ); ?></div>
		<?php elseif ( 'link' === $format ) : ?>
			<?php if ( get_the_title() ) : ?>
				<h3 class="fps-box-title"><a href="<?php echo esc_url( $link_url ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
			<?php endif; ?>
			<span class="fps-format-meta"><?php echo esc_html( wp_parse_url( $link_url, PHP_URL_HOST ) ); ?></span>
		<?php else : ?>
			<?php if ( get_the_title() ) : ?>
				<h3 class="fps-box-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
			<?php endif; ?>
			<div class="fps-box-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 15 ) ); ?></div>
		<?php endif; ?>

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

		<div class="fps-card-actions fps-box-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, 'boxes' ); ?>
		</div>
	</div>
</article>

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