<?php
/*
Arquivo: wp-content/themes/cancao-verdadeira/home.php

Resumo:
Template completo da home estilo Spotify.
Inclui:
- Estrutura completa do WordPress (header/footer)
- Hero com música destaque
- Seção de gêneros
- Ranking (em alta)
- Músicas recentes
- Compatibilidade com player global
- Integração com sidebar (logo removida do centro)
Mantém consistência estrutural e evita perda de funcionalidades.
*/

if (!defined('ABSPATH')) {
    exit;
}

get_header();
?>

<div class="home container">

    <!-- =========================
         HERO (DESTAQUE PRINCIPAL)
    ========================= -->
    <?php
    $destaque = cancao_get_top_musicas(1);

    if ($destaque && $destaque->have_posts()) :
        while ($destaque->have_posts()) : $destaque->the_post();

            $post_id = get_the_ID();
            $meta = function_exists('cancao_get_musica_meta') ? cancao_get_musica_meta($post_id) : [];

            $capa    = get_the_post_thumbnail_url($post_id, 'large');
            $titulo  = get_the_title($post_id);
            $artista = $meta['artista'] ?? '';
            $mp3     = $meta['mp3'] ?? '';
    ?>

    <section class="hero">

        <div class="hero-bg"
             style="background-image:url('<?php echo esc_url($capa); ?>')"></div>

        <div class="hero-content">

            <h1 class="hero-title">
                <?php echo esc_html($titulo); ?>
            </h1>

            <p class="hero-artist">
                <?php echo esc_html($artista); ?>
            </p>

            <button class="btn-play"
                data-mp3="<?php echo esc_url($mp3); ?>"
                data-title="<?php echo esc_attr($titulo); ?>"
                data-artist="<?php echo esc_attr($artista); ?>"
                data-cover="<?php echo esc_url($capa); ?>">
                ▶ Tocar agora
            </button>

        </div>

    </section>

    <?php
        endwhile;
        wp_reset_postdata();
    endif;
    ?>


    <!-- =========================
         CATEGORIAS
    ========================= -->
    <section class="section section-categorias">

        <header class="section-header">
            <h2>🎼 Gêneros</h2>
        </header>

        <div class="categorias">

            <a href="<?php echo home_url('/genero/sertanejo'); ?>" class="cat sertanejo">
                🎸 Sertanejo
            </a>

            <a href="<?php echo home_url('/genero/gospel'); ?>" class="cat gospel">
                ✝ Gospel
            </a>

            <a href="<?php echo home_url('/genero/romantico'); ?>" class="cat romantico">
                ❤️ Romântico
            </a>

        </div>

    </section>


    <!-- =========================
         TRENDING
    ========================= -->
    <section class="section section-trending">

        <header class="section-header">
            <h2>🔥 Em alta</h2>
        </header>

        <div class="grid-musicas">

            <?php
            $trending = cancao_get_top_musicas(8);

            if ($trending && $trending->have_posts()) :
                while ($trending->have_posts()) : $trending->the_post();

                    get_template_part('template-parts/card', 'musica');

                endwhile;
                wp_reset_postdata();
            endif;
            ?>

        </div>

    </section>


    <!-- =========================
         RECENTES
    ========================= -->
    <section class="section section-recentes">

        <header class="section-header">
            <h2>🎵 Adicionadas recentemente</h2>
        </header>

        <div class="grid-musicas">

            <?php
            $recentes = new WP_Query([
                'post_type'      => 'musica',
                'posts_per_page' => 8,
                'post_status'    => 'publish',
                'orderby'        => 'date',
                'order'          => 'DESC'
            ]);

            if ($recentes->have_posts()) :
                while ($recentes->have_posts()) : $recentes->the_post();

                    get_template_part('template-parts/card', 'musica');

                endwhile;
                wp_reset_postdata();
            endif;
            ?>

        </div>

    </section>

</div>

<?php get_footer(); ?>