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

$cover = (string) (($track['cover_image'] ?? $track['cover'] ?? '') ?: '/uploads/music-covers/track-1.svg');
$audio = (string) (($track['audio_file'] ?? $track['audio'] ?? '') ?: '');
$audioSrc = (str_starts_with($audio, '/uploads/') || str_starts_with($audio, '/assets/')) ? url($audio) : '';
$title = (string) (($track['title'] ?? '') ?: 'Untitled Track');
$artist = (string) (($track['artist_name'] ?? $track['artist'] ?? '') ?: 'Zalorox Music');
$genre = (string) (($track['genre'] ?? $track['category'] ?? '') ?: 'Music');
$genreSlug = (string) (($track['genre_slug'] ?? $track['category_slug'] ?? '') ?: 'music');
$slug = (string) (($track['slug'] ?? '') ?: '#');
$artistSlug = (string) (($track['artist_slug'] ?? '') ?: strtolower(preg_replace('~[^a-z0-9]+~', '-', $artist) ?: 'artist'));
$trackId = (int) ($track['id'] ?? 0);
$playCount = (int) ($track['play_count'] ?? $track['plays'] ?? 0);
$likeCount = (int) ($track['like_count'] ?? $track['likes'] ?? 0);
$isPremium = !empty($track['is_premium']) || !empty($track['premium']);
$isFeatured = !empty($track['featured']);
$isLocked = !Auth::userCanAccessTrack($track);
?>
<article class="music-card" data-track-card data-track-id="<?= $trackId ?>">
    <a class="music-card-cover" href="<?= url('/music/sarki/' . $slug) ?>" aria-label="<?= e($title) ?>">
        <img loading="lazy" decoding="async" src="<?= url($cover) ?>" alt="<?= e($title) ?>">
        <span class="music-card-gradient" aria-hidden="true"></span>
        <span class="music-card-badges">
            <span class="music-badge <?= $isPremium ? 'premium' : 'free' ?>"><?= $isPremium ? 'Premium' : 'Free' ?></span>
            <?php if ($isFeatured): ?><span class="music-badge featured">Featured</span><?php endif; ?>
        </span>
    </a>
    <div class="music-card-body">
        <a class="music-card-title" href="<?= url('/music/sarki/' . $slug) ?>"><?= e($title) ?></a>
        <p><a href="<?= url('/music/sanatci/' . $artistSlug) ?>"><?= e($artist) ?><?= !empty($track['artist_verified']) ? ' *' : '' ?></a></p>
        <div class="music-card-meta">
            <span><?= e($genre) ?></span>
            <span data-play-count="<?= $trackId ?>"><?= number_format($playCount, 0, ',', '.') ?> dinlenme</span>
            <span data-like-count="<?= $trackId ?>"><?= number_format($likeCount, 0, ',', '.') ?> begeni</span>
        </div>
        <button
            class="music-play-button"
            type="button"
            data-track-play
            data-track-id="<?= $trackId ?>"
            data-track-title="<?= e($title) ?>"
            data-track-artist="<?= e($artist) ?>"
            data-track-cover="<?= e(url($cover)) ?>"
            data-audio-src="<?= e($audioSrc) ?>"
            data-detail-url="<?= e(url('/music/sarki/' . $slug)) ?>"
            data-track-genre="<?= e($genre) ?>"
            data-track-genre-slug="<?= e($genreSlug) ?>"
            data-play-endpoint="<?= e(url('/api/music/play')) ?>"
            data-locked="<?= $isLocked ? '1' : '0' ?>"
        ><span aria-hidden="true">&#9658;</span> Play</button>
    </div>
</article>
