<?php
/**
 * Book List Renderer
 * 
 * Reusable function to render book list items in a 1-column list layout.
 * Used by both the Book List Block (with Load More) and Archive Template (with pagination).
 * 
 * @package Author_Website_Templates
 * @since 1.0.9
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Render Book List Items
 * 
 * @param WP_Query $query The WordPress query object containing book posts
 * @param array $settings Optional settings for customization
 * @return string HTML output of the book list
 */
function rswpthemes_awt_render_book_list( $query, $settings = array() ) {
    if ( ! $query->have_posts() ) {
        return '<p class="text-center text-xl text-navy/70">' . esc_html__( 'No books found.', 'author-website-templates' ) . '</p>';
    }
    
    $placeholder_cover = RSWPTHEMES_AWT_PLUGIN_URL . 'includes/assets/images/book-placeholder.png';
    
    ob_start();
    
    while ( $query->have_posts() ) : $query->the_post();
        $book_id = get_the_ID();
        $cover_url = get_the_post_thumbnail_url( $book_id, 'large' );
        $description = get_post_meta( $book_id, '_rsbs_short_description', true );
        
        if ( empty( $description ) ) {
            $description = get_the_excerpt();
        }
        
        $buy_btn_text = get_post_meta( $book_id, '_rsbs_buy_btn_text', true );
        $buy_btn_link = get_post_meta( $book_id, '_rsbs_buy_btn_link', true );
        
        if ( empty( $buy_btn_text ) ) {
            $buy_btn_text = __( 'Buy Now', 'author-website-templates' );
        }
        
        // Get first category for badge
        $categories = get_the_terms( $book_id, 'book-category' );
        $category = ( $categories && ! is_wp_error( $categories ) ) ? $categories[0] : null;
        ?>
        
        <article class="grid grid-cols-1 md:grid-cols-3 gap-8 md:gap-12 items-center border-t border-yellow-sun/30 pt-16 first:border-0 first:pt-0">
            <!-- Cover Image -->
            <div class="md:col-span-1 flex justify-center">
                <a href="<?php the_permalink(); ?>">
                    <img src="<?php echo esc_url( $cover_url ? $cover_url : $placeholder_cover ); ?>"
                         alt="<?php echo esc_attr( sprintf( __( 'Book cover for %s', 'author-website-templates' ), get_the_title() ) ); ?>"
                         class="rounded-lg shadow-2xl w-full max-w-xs transition-all duration-300 hover:scale-105 hover:rotate-2">
                </a>
            </div>
            
            <!-- Book Info -->
            <div class="md:col-span-2 text-center md:text-left">
                <?php if ( $category ) : ?>
                    <span class="font-bold text-coral-pop uppercase tracking-wider">
                        <?php echo esc_html( $category->name ); ?>
                    </span>
                <?php endif; ?>
                
                <a href="<?php the_permalink(); ?>">
                    <h3 class="text-3xl md:text-4xl font-black text-navy my-2 hover:text-blue-sky">
                        <?php the_title(); ?>
                    </h3>
                </a>
                
                <?php if ( ! empty( $description ) ) : ?>
                    <p class="text-lg text-navy/80 mb-8">
                        <?php echo esc_html( wp_trim_words( $description, 30 ) ); ?>
                    </p>
                <?php endif; ?>
                
                <div class="flex flex-col sm:flex-row gap-4 justify-center md:justify-start">
                    <?php if ( ! empty( $buy_btn_link ) ) : ?>
                        <a href="<?php echo esc_url( $buy_btn_link ); ?>" 
                           class="btn-fun btn-yellow"
                           target="_blank"
                           rel="noopener noreferrer nofollow">
                            <?php echo esc_html( $buy_btn_text ); ?>
                        </a>
                    <?php endif; ?>
                    
                    <a href="<?php the_permalink(); ?>" class="btn-fun btn-white">
                        <?php esc_html_e( 'Read More & See Details', 'author-website-templates' ); ?>
                    </a>
                </div>
            </div>
        </article>
        
        <?php
    endwhile;
    
    wp_reset_postdata();
    
    return ob_get_clean();
}
