<?php
use App\Core\Auth;
use function App\Core\{e, url};

$heroTrack = $heroTrack ?? null;
$latest = $latest ?? [];
$popular = $popular ?? [];
$genres = $genres ?? [];
$artists = $artists ?? [];
$sitePlans = $sitePlans ?? [];
?>
<section class="music-hero">
    <div class="container music-hero-grid">
        <div class="music-hero-copy">
            <p class="eyebrow">Zalorox Music</p>
            <h1>Zalorox Music</h1>
            <p>Yeni cikan sarkilar, creator parcalari, premium kayitlar ve oyun enerjisine yakin listeler tek music alaninda.</p>
            <?php if ($heroTrack): ?>
                <?php
                $heroAudio = (string) (($heroTrack['audio_file'] ?? $heroTrack['audio'] ?? '') ?: '');
                $heroAudioSrc = (str_starts_with($heroAudio, '/uploads/') || str_starts_with($heroAudio, '/assets/')) ? url($heroAudio) : '';
                $heroLocked = !Auth::userCanAccessTrack($heroTrack);
                ?>
                <div class="music-hero-actions">
                    <a class="pill primary" href="<?= url('/music/sarki/' . ($heroTrack['slug'] ?? '')) ?>">One Cikan Sarkiyi Ac</a>
                    <button
                        class="pill"
                        type="button"
                        data-track-play
                        data-track-id="<?= (int) ($heroTrack['id'] ?? 0) ?>"
                        data-track-title="<?= e($heroTrack['title'] ?? 'Track') ?>"
                        data-track-artist="<?= e($heroTrack['artist_name'] ?? $heroTrack['artist'] ?? 'Zalorox Music') ?>"
                        data-track-cover="<?= e(url($heroTrack['cover_image'] ?? $heroTrack['cover'] ?? '/uploads/music-covers/track-1.svg')) ?>"
                        data-audio-src="<?= e($heroAudioSrc) ?>"
                        data-detail-url="<?= e(url('/music/sarki/' . ($heroTrack['slug'] ?? ''))) ?>"
                        data-track-genre="<?= e($heroTrack['genre'] ?? 'Music') ?>"
                        data-track-genre-slug="<?= e($heroTrack['genre_slug'] ?? 'music') ?>"
                        data-play-endpoint="<?= e(url('/api/music/play')) ?>"
                        data-locked="<?= $heroLocked ? '1' : '0' ?>"
                    >Play</button>
                </div>
            <?php endif; ?>
        </div>

        <?php if ($heroTrack): ?>
            <?php $cover = (string) (($heroTrack['cover_image'] ?? $heroTrack['cover'] ?? '') ?: '/uploads/music-covers/track-1.svg'); ?>
            <a class="music-hero-track" href="<?= url('/music/sarki/' . ($heroTrack['slug'] ?? '')) ?>">
                <img src="<?= url($cover) ?>" alt="<?= e($heroTrack['title'] ?? 'Track') ?>">
                <span class="music-hero-track-glow" aria-hidden="true"></span>
                <span class="music-hero-track-info">
                    <strong><?= e($heroTrack['title'] ?? 'Track') ?></strong>
                    <em><?= e($heroTrack['artist_name'] ?? $heroTrack['artist'] ?? 'Zalorox Music') ?></em>
                    <small>
                        <span data-play-count="<?= (int) ($heroTrack['id'] ?? 0) ?>"><?= number_format((int) ($heroTrack['play_count'] ?? $heroTrack['plays'] ?? 0), 0, ',', '.') ?> dinlenme</span>
                        <span data-like-count="<?= (int) ($heroTrack['id'] ?? 0) ?>"><?= number_format((int) ($heroTrack['like_count'] ?? $heroTrack['likes'] ?? 0), 0, ',', '.') ?> begeni</span>
                    </small>
                </span>
            </a>
        <?php endif; ?>
    </div>
</section>

<section class="container section music-section" id="yeni-sarkilar">
    <div class="section-head">
        <div>
            <p class="eyebrow">Yeni cikanlar</p>
            <h2>Yeni Sarkilar</h2>
        </div>
        <a class="section-link" href="<?= url('/music/arama?sirala=newest') ?>">Tumunu gor</a>
    </div>
    <?php if ($latest !== []): ?>
        <div class="grid music-grid">
            <?php foreach ($latest as $track): ?>
                <?php require BASE_PATH . '/app/Views/partials/music-card.php'; ?>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="empty-state"><strong>Yeni sarki yok.</strong><p>Studio yukleme akisi tamamlandikca bu alan dolacak.</p></div>
    <?php endif; ?>
</section>

<section class="container section music-section" id="populer">
    <div class="section-head">
        <div>
            <p class="eyebrow">Populer</p>
            <h2>Populer Sarkilar</h2>
        </div>
        <a class="section-link" href="<?= url('/music/arama?sirala=popular') ?>">Populere git</a>
    </div>
    <?php if ($popular !== []): ?>
        <div class="music-rank-list">
            <?php foreach ($popular as $index => $track): ?>
                <a class="music-rank-item" href="<?= url('/music/sarki/' . ($track['slug'] ?? '')) ?>">
                    <span><?= $index + 1 ?></span>
                    <img loading="lazy" src="<?= url($track['cover_image'] ?? $track['cover'] ?? '/uploads/music-covers/track-1.svg') ?>" alt="">
                    <strong><?= e($track['title'] ?? 'Track') ?></strong>
                    <em><?= e($track['artist_name'] ?? $track['artist'] ?? 'Zalorox Music') ?></em>
                    <small><?= number_format((int) ($track['play_count'] ?? $track['plays'] ?? 0), 0, ',', '.') ?> dinlenme</small>
                </a>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="empty-state"><strong>Populer sarki yok.</strong><p>Dinlenme verisi geldikce bu liste siralanacak.</p></div>
    <?php endif; ?>
</section>

<section class="container section music-section" id="turler">
    <div class="section-head">
        <div>
            <p class="eyebrow">Turler</p>
            <h2>Tarzlar</h2>
        </div>
    </div>
    <div class="music-genre-grid">
        <?php foreach ($genres as $genre): ?>
            <a class="music-genre-card" href="<?= url('/music/tarz/' . ($genre['slug'] ?? 'music')) ?>">
                <span><?= e($genre['icon'] ?? '') ?></span>
                <strong><?= e($genre['name'] ?? 'Music') ?></strong>
                <small><?= e($genre['description'] ?? '') ?></small>
            </a>
        <?php endforeach; ?>
    </div>
</section>

<section class="container section music-section" id="sanatcilar">
    <div class="section-head">
        <div>
            <p class="eyebrow">Sanatcilar</p>
            <h2>One Cikan Sanatcilar</h2>
        </div>
    </div>
    <?php if ($artists !== []): ?>
        <div class="music-artist-grid">
            <?php foreach ($artists as $artist): ?>
                <a class="music-artist-card" href="<?= url('/music/sanatci/' . ($artist['slug'] ?? 'artist')) ?>">
                    <img loading="lazy" src="<?= url($artist['cover_image'] ?? '/uploads/music-covers/track-1.svg') ?>" alt="">
                    <div>
                        <strong><?= e($artist['name'] ?? 'Zalorox Music') ?><?= !empty($artist['verified']) ? ' *' : '' ?></strong>
                        <span><?= number_format((int) ($artist['track_count'] ?? 0), 0, ',', '.') ?> sarki</span>
                        <small><?= number_format((int) ($artist['play_count'] ?? 0), 0, ',', '.') ?> dinlenme</small>
                    </div>
                </a>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="empty-state"><strong>Sanatci listesi hazirlaniyor.</strong><p>Gelismis sanatci sayfasi sonraki bolumde genisletilecek.</p></div>
    <?php endif; ?>
</section>

<section class="container section music-section" id="premium">
    <div class="music-premium-band">
        <div>
            <p class="eyebrow">Site Premium</p>
            <h2>Reklamsiz ve premium player ile dinle</h2>
            <p>Muzik tarafinda creator paketi yoktur. Premium sarkilar site genel premium aboneligiyle acilir; mobil/desktop app erisimi ileride bu plana baglanacak.</p>
        </div>
        <div class="music-premium-actions">
            <?php foreach (array_slice($sitePlans, 0, 2) as $plan): ?>
                <a class="pill primary" href="<?= url('/checkout?site_plan=' . ($plan['slug'] ?? 'premium')) ?>"><?= e($plan['name'] ?? 'Premium') ?></a>
            <?php endforeach; ?>
            <?php if ($sitePlans === []): ?><a class="pill primary" href="<?= url('/checkout?site_plan=premium') ?>">Premium</a><?php endif; ?>
        </div>
    </div>
</section>

<section class="container section music-section" id="listeler">
    <div class="music-playlist-band">
        <div>
            <p class="eyebrow">Listeler</p>
            <h2>Haftalik Music Listeleri</h2>
            <p>Playlist CRUD ve gelismis sanatci sayfalari Bolum 7 icin TODO olarak birakildi; bu alan MVP kesif listelerine baglidir.</p>
        </div>
        <a class="pill primary" href="<?= url('/music/arama') ?>">Kesfet</a>
    </div>
</section>
