<?php
/**
 * Book Archive Template
 * 
 * Custom template for displaying book archives (post type archive and taxonomies).
 * Uses global header/footer with context manager checks.
 * 
 * @package Author_Website_Templates
 * @since 1.0.9
 */

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

// Retrieve Global Settings
$awt_settings = get_option( 'awt_global_settings', [] );

// Header Logic
$header_slug = isset( $awt_settings['awt_global_header_block'] ) ? $awt_settings['awt_global_header_block'] : '';
$force_header_global = isset( $awt_settings['awt_header_force_global'] ) ? $awt_settings['awt_header_force_global'] : false;

$header_id = false;
if ( ! empty( $header_slug ) && function_exists( 'rswpthemes_awt_get_block_id_by_slug' ) ) {
    $header_id = rswpthemes_awt_get_block_id_by_slug( $header_slug );
}

// Check conditions: We use custom header if ID found AND Force Global is enabled.
// If Force Global is unchecked, we assume the user wants the Theme's header.
$use_custom_header = ( $header_id && $force_header_global );

if ( $use_custom_header ) {
    ?>
    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>
    <head>
        <meta charset="<?php bloginfo( 'charset' ); ?>">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <?php wp_head(); ?>
    </head>
    <body <?php body_class( 'awt-wrapper' ); ?>>
        <div class="bg-cream font-sans text-navy antialiased awt-body-inner">
    <?php
    echo do_blocks( '<!-- wp:block {"ref":' . absint( $header_id ) . '} /-->' );
} else {
    get_header();
}
?>

<main>
<?php
// Content Template Logic
$archive_template_slug = isset( $awt_settings['awt_book_archive_template_id'] ) ? $awt_settings['awt_book_archive_template_id'] : '';

$archive_template_id = 0;

if ( ! empty( $archive_template_slug ) && function_exists( 'rswpthemes_awt_get_block_id_by_slug' ) ) {
    $archive_template_id = rswpthemes_awt_get_block_id_by_slug( $archive_template_slug );
}

if ( ! empty( $archive_template_id ) ) {
    // Render the selected Reusable Block
    $template_post = get_post( $archive_template_id );
    
    if ( $template_post && $template_post->post_type === 'wp_block' && $template_post->post_status === 'publish' ) {
        // Render the block content
        echo do_blocks( $template_post->post_content );
    } else {
        // Fallback if block doesn't exist but setting is set
        echo '<p class="awt-container mx-auto px-6 py-16 text-center">' . esc_html__( 'Selected template block not found. Please update in settings.', 'author-website-templates' ) . '</p>';
    }
} else {
    // Use default hardcoded layout
    ?>
    <!-- Hero Section -->
    <section id="books-hero" class="relative overflow-hidden bg-blue-sky/10 pt-20 pb-28">
        <div class="absolute bottom-0 left-0 w-full">
            <svg viewBox="0 0 1440 100" fill="none" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none">
                <path d="M0 100H1440V0C1192.5 49.497 919.5 74.497 720 74.497C520.5 74.497 247.5 49.497 0 0V100Z" fill="#FFFDF5"/>
            </svg>
        </div>

        <div class="awt-container mx-auto px-6 relative z-10 text-center">
            <h1 class="text-5xl md:text-7xl font-black text-navy mb-4">
                <?php
                if ( is_tax( 'book-category' ) || is_tax( 'book-author' ) || is_tax( 'book-series' ) ) {
                    single_term_title();
                } else {
                    esc_html_e( 'My Book Shelf', 'author-website-templates' );
                }
                ?>
            </h1>
            <p class="text-xl text-navy/80 max-w-lg mx-auto">
                <?php
                if ( is_tax() ) {
                    echo esc_html( term_description() );
                } else {
                    esc_html_e( 'Adventures for every reader, big and small!', 'author-website-templates' );
                }
                ?>
            </p>
        </div>
    </section>

    <!-- Books List -->
    <section class="py-24">
        <div class="awt-container mx-auto px-6 max-w-5xl">
            <div class="space-y-20">
                <?php
                // Use global WP_Query for archive
                global $wp_query;
                
                // Render book list using shared renderer
                if ( function_exists( 'rswpthemes_awt_render_book_list' ) ) {
                    echo rswpthemes_awt_render_book_list( $wp_query );
                } elseif ( have_posts() ) {
                    // Fallback if renderer not available
                    while ( have_posts() ) : the_post();
                        echo '<p>' . esc_html( get_the_title() ) . '</p>';
                    endwhile;
                } else {
                    echo '<p class="text-center text-xl text-navy/70">' . esc_html__( 'No books found.', 'author-website-templates' ) . '</p>';
                }
                ?>
            </div>
            
            <!-- Pagination (SEO-friendly for archives) -->
            <?php if ( have_posts() ) : ?>
                <div class="mt-16">
                    <?php
                    the_posts_pagination( array(
                        'mid_size'  => 2,
                        'prev_text' => __( '← Previous', 'author-website-templates' ),
                        'next_text' => __( 'Next →', 'author-website-templates' ),
                        'class'     => 'flex justify-center gap-4 font-bold',
                    ) );
                    ?>
                </div>
            <?php endif; ?>
        </div>
    </section>
    <?php
}
?>
</main>

<?php
// Footer Logic
$footer_slug = isset( $awt_settings['awt_global_footer_block'] ) ? $awt_settings['awt_global_footer_block'] : '';
$force_footer_global = isset( $awt_settings['awt_footer_force_global'] ) ? $awt_settings['awt_footer_force_global'] : false;

$footer_id = false;
if ( ! empty( $footer_slug ) && function_exists( 'rswpthemes_awt_get_block_id_by_slug' ) ) {
    $footer_id = rswpthemes_awt_get_block_id_by_slug( $footer_slug );
}

$use_custom_footer = ( $footer_id && $force_footer_global );

if ( $use_custom_footer ) {
    echo do_blocks( '<!-- wp:block {"ref":' . absint( $footer_id ) . '} /-->' );
    wp_footer();
    echo '</div></body></html>';
} else {
    get_footer();
}
?>
