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

$track = $track ?? [];
$comments = $comments ?? [];
$related = $related ?? [];
$canAccess = (bool) ($canAccess ?? true);
$userLiked = (bool) ($userLiked ?? false);
$canEdit = (bool) ($canEdit ?? false);
$sitePlans = $sitePlans ?? [];
$cover = (string) (($track['cover_image'] ?? $track['cover'] ?? '') ?: '/uploads/music-covers/track-1.svg');
$trackId = (int) ($track['id'] ?? 0);
$title = (string) (($track['title'] ?? '') ?: 'Untitled Track');
$artist = (string) (($track['artist_name'] ?? $track['artist'] ?? '') ?: 'Zalorox Music');
?>
<section class="container music-detail" data-music-detail data-track-id="<?= $trackId ?>">
    <div class="music-detail-cover">
        <img src="<?= url($cover) ?>" alt="<?= e($title) ?>">
        <?php if (!empty($track['featured'])): ?><span class="music-badge featured">Featured</span><?php endif; ?>
        <span class="music-badge <?= !empty($track['premium']) || !empty($track['is_premium']) ? 'premium' : 'free' ?>"><?= !empty($track['premium']) || !empty($track['is_premium']) ? 'Premium' : 'Free' ?></span>
    </div>

    <div class="music-detail-main">
        <p class="eyebrow"><?= e($track['genre'] ?? 'Music') ?></p>
        <h1><?= e($title) ?></h1>
        <div class="music-detail-artist">
            <a class="avatar" href="<?= url('/music/sanatci/' . ($track['artist_slug'] ?? 'artist')) ?>">
                <?php if (!empty($track['artist_avatar'])): ?><img src="<?= url((string) $track['artist_avatar']) ?>" alt=""><?php else: ?><?= e(strtoupper(substr($artist, 0, 1))) ?><?php endif; ?>
            </a>
            <div>
                <a href="<?= url('/music/sanatci/' . ($track['artist_slug'] ?? 'artist')) ?>"><strong><?= e($artist) ?><?= !empty($track['artist_verified']) ? ' *' : '' ?></strong></a>
                <small><?= e($track['creator_display_name'] ?? $artist) ?></small>
            </div>
        </div>

        <div class="music-detail-stats">
            <span data-play-count="<?= $trackId ?>"><?= number_format((int) ($track['play_count'] ?? $track['plays'] ?? 0), 0, ',', '.') ?> dinlenme</span>
            <span><?= number_format((int) ($track['view_count'] ?? 0), 0, ',', '.') ?> goruntulenme</span>
            <span data-like-count="<?= $trackId ?>"><?= number_format((int) ($track['like_count'] ?? $track['likes'] ?? 0), 0, ',', '.') ?> begeni</span>
        </div>

        <?php require BASE_PATH . '/app/Views/partials/music-player.php'; ?>

        <div class="music-detail-actions">
            <button
                class="pill <?= $userLiked ? 'is-active' : '' ?>"
                type="button"
                data-music-like
                data-track-id="<?= $trackId ?>"
            >Begen</button>
            <button class="pill" type="button" data-music-share data-track-id="<?= $trackId ?>">Paylas</button>
            <?php if ($canEdit): ?>
                <a class="pill primary" href="<?= url('/studyo/muzik-duzenle/' . $trackId) ?>">Studio'da Duzenle</a>
            <?php endif; ?>
        </div>

        <section class="music-description">
            <h2>Aciklama</h2>
            <p><?= nl2br(e((string) ($track['description'] ?? ''))) ?></p>
        </section>
    </div>
</section>

<section class="container music-detail-comments">
    <?php
    $contentType = 'music';
    $contentId = $trackId;
    require BASE_PATH . '/app/Views/partials/comment-box.php';
    ?>
</section>

<?php if ($related !== []): ?>
    <section class="container section music-section">
        <div class="section-head">
            <div>
                <p class="eyebrow">Onerilen</p>
                <h2>Benzer Sarkilar</h2>
            </div>
        </div>
        <div class="grid music-grid">
            <?php foreach ($related as $track): ?>
                <?php require BASE_PATH . '/app/Views/partials/music-card.php'; ?>
            <?php endforeach; ?>
        </div>
    </section>
<?php endif; ?>
