<?php
/**
 * Archive card: Classic mode.
 *
 * Enhanced card with thumbnail/media, title, excerpt, "More" button,
 * comments count, and documented action hooks for plugin extensibility.
 * Handles all post formats inline.
 *
 * @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
/**
 * Fires before each post card in archive listings.
 *
 * @since 2.3
 *
 * @param int    $post_id The current post ID.
 * @param string $format  The post format slug ('standard' when none).
 * @param string $mode    The archive display mode ('classic').
 */
do_action( 'fanspaysite_post_card_before', get_the_ID(), $format, 'classic' );
?>

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

	<?php if ( 'aside' !== $format && 'quote' !== $format ) : ?>
		<div class="fps-post-thumbnail">
			<?php if ( 'video' === $format && $media_html ) : ?>
				<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(); ?>">
					<?php the_post_thumbnail( 'medium', array( 'class' => 'ui image rounded', '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 : ?>
				<?php
				$default_thumbnail = get_theme_mod( 'default_post_thumbnail', get_template_directory_uri() . '/default.png' );
				echo '<img src="' . esc_url( $default_thumbnail ) . '" class="ui image rounded" alt="' . esc_attr( get_the_title() ) . '" width="300" height="200" loading="lazy">';
				?>
			<?php endif; ?>
		</div>
	<?php endif; ?>

	<div class="fps-post-body">
		<?php if ( $icon_class ) : ?>
			<div class="fps-format-icon"><i class="fas <?php echo esc_attr( $icon_class ); ?>"></i></div>
		<?php endif; ?>

		<?php if ( 'audio' === $format && $media_html ) : ?>
			<div class="fps-post-media">
				<?php echo $media_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			</div>
		<?php endif; ?>

		<?php if ( 'aside' === $format ) : ?>
			<?php the_excerpt(); ?>
		<?php elseif ( 'quote' === $format ) : ?>
			<blockquote>
				<?php the_excerpt(); ?>
				<?php if ( get_the_title() ) : ?>
					<cite><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></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; ?>
			<?php the_excerpt(); ?>
			<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() ) : ?>
				<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
			<?php endif; ?>
			<div class="post-excerpt"><?php the_excerpt(); ?></div>
		<?php endif; ?>

		<?php
		/**
		 * Fires in the meta area of each post card.
		 *
		 * Use this hook to add custom metadata such as ratings, view counts,
		 * or custom fields below the post excerpt.
		 *
		 * @since 2.3
		 *
		 * @param int    $post_id The current post ID.
		 * @param string $format  The post format slug.
		 * @param string $mode    The archive display mode.
		 */
		do_action( 'fanspaysite_post_card_meta', get_the_ID(), $format, 'classic' );
		?>

		<footer class="fps-card-footer">
			<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
				/**
				 * Fires in the action buttons area of each post card.
				 *
				 * Use this hook to add interactive buttons such as likes,
				 * votes, bookmarks, or share buttons.
				 *
				 * @since 2.3
				 *
				 * @param int    $post_id The current post ID.
				 * @param string $format  The post format slug.
				 * @param string $mode    The archive display mode.
				 */
				do_action( 'fanspaysite_post_card_actions', get_the_ID(), $format, 'classic' );
				?>
			</div>
		</footer>
	</div>
</article>

<?php
/**
 * Fires after each post card in archive listings.
 *
 * @since 2.3
 *
 * @param int    $post_id The current post ID.
 * @param string $format  The post format slug.
 * @param string $mode    The archive display mode ('classic').
 */
do_action( 'fanspaysite_post_card_after', get_the_ID(), $format, 'classic' );
?>
