<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

get_header();
?>

<div class="content-wrapper">    
    <main class="home-main-content">
        
        <!-- 1. Banner Home Top (já feito) -->
        <?php if ( function_exists( 'do_shortcode' ) ) : ?>
            <div class="site-banner-top" role="complementary" aria-label="Publicidade">
                <?php echo do_shortcode( '[banners position="home-top" count="1"]' ); ?>
            </div>
        <?php endif; ?>
        
        <!-- 2. Box com 3 Destaques (Culturando | Matéria da Semana | Matéria de Capa) -->
        <section class="home-section home-highlights-grid">
            <div class="container">
                <div class="highlights-grid">
                    <?php
                    // Configuração base para os 3 destaques
                    $highlight_cats = [
                        'culturando'        => 'Culturando',
                        'materia-da-semana' => 'Matéria da semana',
                        'materia-de-capa'   => 'Matéria de capa'
                    ];
                    
                    foreach ( $highlight_cats as $slug => $label ) :
                        $query = new WP_Query( [
                            'category_name'    => $slug,
                            'posts_per_page'   => 1,
                            'post_status'      => 'publish',
                            'ignore_sticky_posts' => 1
                        ] );
                        
                        if ( $query->have_posts() ) :
                            while ( $query->have_posts() ) : $query->the_post();
                                $thumb = get_the_post_thumbnail_url( get_the_ID(), 'medium' );
                                $is_video = has_post_format( 'video' ) || 
                                           strpos( get_the_content(), 'youtube.com' ) !== false || 
                                           strpos( get_the_content(), 'youtu.be' ) !== false;
                    ?>
                    <article class="highlight-card">
                        <a href="<?php the_permalink(); ?>" class="highlight-link">
                            <?php if ( $thumb ) : ?>
                                <div class="highlight-thumb">
                                    <img src="<?php echo esc_url( $thumb ); ?>" 
                                         alt="<?php the_title_attribute(); ?>" 
                                         loading="lazy">
                                    <?php if ( $is_video ) : ?>
                                        <span class="play-badge">▶</span>
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>
                            <div class="highlight-content">
                                <span class="highlight-label"><?php echo esc_html( $label ); ?></span>
                                <h3 class="highlight-title"><?php the_title(); ?></h3>
                                <p class="highlight-excerpt"><?php echo wp_trim_words( get_the_excerpt(), 15, '…' ); ?></p>
                            </div>
                        </a>
                    </article>
                    <?php
                            endwhile;
                            wp_reset_postdata();
                        else :
                            // Fallback se não houver post na categoria
                    ?>
                    <article class="highlight-card highlight-empty">
                        <div class="highlight-content">
                            <span class="highlight-label"><?php echo esc_html( $label ); ?></span>
                            <p class="no-content-msg">Em breve</p>
                        </div>
                    </article>
                    <?php endif; ?>
                    <?php endforeach; ?>
                </div>
            </div>
        </section>

        <!-- 3. Últimos Posts (3 em linha, excluindo "Culturando") -->
        <section class="home-section home-latest-posts">
            <div class="container">
                <header class="section-header">
                    <h2 class="section-title">Últimas do Blog</h2>
                    <a href="<?php echo esc_url( get_permalink( get_option( 'page_for_posts' ) ) ); ?>" class="section-link">Ver todas →</a>
                </header>
                <?php
                $latest_q = new WP_Query( [
                    'post_type'      => 'post',
                    'post_status'    => 'publish',
                    'posts_per_page' => 3,
                    'tax_query'      => [
                        [
                            'taxonomy' => 'category',
                            'field'    => 'slug',
                            'terms'    => [ 
                                'culturando', 
                                'materia-da-semana', 
                                'materia-de-capa' 
                            ],
                            'operator' => 'NOT IN'
                        ]
                    ],
                    'ignore_sticky_posts' => 1
                ] );

                if ( $latest_q->have_posts() ) : ?>
                    <div class="latest-posts-grid">
                        <?php while ( $latest_q->have_posts() ) : $latest_q->the_post(); ?>
                            <article class="latest-post-card">
                                <a href="<?php the_permalink(); ?>" class="latest-post-link">
                                    <?php if ( has_post_thumbnail() ) : ?>
                                        <div class="latest-post-thumb">
                                            <?php the_post_thumbnail( 'medium', ['loading' => 'lazy'] ); ?>
                                        </div>
                                    <?php endif; ?>
                                    <div class="latest-post-content">
                                        <h3 class="latest-post-title"><?php the_title(); ?></h3>
                                        <div class="latest-post-meta">
                                            <time class="post-date" datetime="<?php echo get_the_date( 'c' ); ?>">
                                                <?php echo get_the_date( 'd/m/Y' ); ?>
                                            </time>
                                            <?php $cats = get_the_category(); if ( $cats ) : ?>
                                                <span class="post-cat"><?php echo esc_html( $cats[0]->name ); ?></span>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </a>
                            </article>
                        <?php endwhile; wp_reset_postdata(); ?>
                    </div>
                <?php else : ?>
                    <p class="no-content-msg">Nenhuma postagem recente.</p>
                <?php endif; ?>
            </div>
        </section>

        <!-- 4. Banner Home Middle -->
        <?php if ( function_exists( 'do_shortcode' ) ) : ?>
            <div class="site-banner-middle" role="complementary" aria-label="Publicidade">
                <?php echo do_shortcode( '[banners position="home-middle" count="1"]' ); ?>
            </div>
        <?php endif; ?>

        <!-- 5. Instagram (placeholder até instalar o plugin) -->
        <section class="home-section home-instagram-placeholder">
            <div class="container">
                <header class="section-header">
                    <h2 class="section-title">Siga no Instagram</h2>
                </header>
                <div class="instagram-grid">
                    <!-- Placeholder: substitua pelo shortcode do plugin depois -->
                    <div class="instagram-placeholder-msg">
                        <p>📸 <strong>Feed do Instagram</strong></p>
                        <p class="small">Em breve: fotos recentes de @seu_perfil</p>
                        <!-- Quando instalar o plugin, troque por: -->
                        <!-- <?php // echo do_shortcode('[instagram_feed]'); ?> -->
                    </div>
                </div>
            </div>
        </section>

        <!-- 6. Links / Telefones Úteis -->
        <section class="home-section home-useful-links">
            <div class="container">
                <header class="section-header">
                    <h2 class="section-title">Contatos Úteis</h2>
                </header>
                <div class="useful-links-grid">
                    <?php culturando_links_column(1); ?>
                    <?php culturando_links_column(2); ?>
                    <?php culturando_links_column(3); ?>
                </div>
            </div>
        </section>

    </main>
</div>

<?php get_footer(); ?>