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

Resumo:
Home estilo Spotify completa.
Inclui:
- Hero destaque
- Recomendação inteligente
- Categorias
- Em alta
- Recentes
Totalmente integrado com player e sistema de recomendação.
*/

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

get_header();
?>

<div class="home container">

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

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

            $meta = cancao_get_musica_meta(get_the_ID());
            $capa = get_the_post_thumbnail_url(get_the_ID(), 'large');
    ?>

    <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 the_title(); ?></h1>

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

            <button class="btn-play"
                data-mp3="<?php echo esc_url($meta['mp3']); ?>"
                data-title="<?php the_title(); ?>"
                data-artist="<?php echo esc_attr($meta['artista']); ?>"
                data-cover="<?php echo esc_url($capa); ?>">
                ▶ Tocar agora
            </button>
        </div>
    </section>

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


    <!-- =========================
         RECOMENDADAS
    ========================= -->
    <section class="section">
        <h2>🤖 Recomendado para você</h2>

        <div class="grid-musicas">
            <?php
            $recomendadas = function_exists('cancao_get_recommendations')
                ? cancao_get_recommendations(8)
                : null;

            if ($recomendadas && $recomendadas->have_posts()):
                while ($recomendadas->have_posts()): $recomendadas->the_post();
                    get_template_part('template-parts/card', 'musica');
                endwhile;
                wp_reset_postdata();
            endif;
            ?>
        </div>
    </section>


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

        <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>


    <!-- =========================
         EM ALTA
    ========================= -->
    <section class="section">
        <h2>🔥 Em alta</h2>

        <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">
        <h2>🎵 Adicionadas recentemente</h2>

        <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(); ?>