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

$track = $track ?? [];
$canAccess = (bool) ($canAccess ?? true);
$trackId = (int) ($track['id'] ?? 0);
$title = (string) (($track['title'] ?? '') ?: 'Untitled Track');
$artist = (string) (($track['artist_name'] ?? $track['artist'] ?? '') ?: 'Zalorox Music');
$cover = (string) (($track['cover_image'] ?? $track['cover'] ?? '') ?: '/uploads/music-covers/track-1.svg');
$audioPath = (string) (($track['audio_file'] ?? $track['audio'] ?? '') ?: '');
$audioSrc = (str_starts_with($audioPath, '/uploads/') || str_starts_with($audioPath, '/assets/')) ? url($audioPath) : '';
$isPremium = !empty($track['is_premium']) || !empty($track['premium']);

$safeExternal = static function (?string $value): string {
    $value = trim((string) $value);
    if ($value === '' || !filter_var($value, FILTER_VALIDATE_URL)) {
        return '';
    }

    $scheme = strtolower((string) parse_url($value, PHP_URL_SCHEME));
    return in_array($scheme, ['http', 'https'], true) ? $value : '';
};

$links = [
    'YouTube' => $safeExternal($track['youtube_url'] ?? ''),
    'Spotify' => $safeExternal($track['spotify_url'] ?? ''),
    'Apple Music' => $safeExternal($track['apple_music_url'] ?? ''),
    'External' => $safeExternal($track['external_link'] ?? ''),
];
$links = array_filter($links);
?>
<?php if (!$canAccess): ?>
    <section class="music-lock-card" data-premium-lock>
        <span class="music-lock-icon" aria-hidden="true">+</span>
        <div>
            <p class="eyebrow">Site Premium</p>
            <h2>Bu sarki premium dinleme gerektiriyor</h2>
            <p>Muzik tarafinda creator paketi kullanilmaz. Reklamsiz dinleme ve premium player site premium aboneligiyle acilir.</p>
        </div>
        <a class="pill primary" href="<?= url('/checkout?site_plan=premium') ?>">Premium'a Gec</a>
    </section>
<?php else: ?>
    <section
        class="music-player-panel"
        data-music-player
        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/' . ($track['slug'] ?? ''))) ?>"
        data-track-genre="<?= e($track['genre'] ?? 'Music') ?>"
        data-track-genre-slug="<?= e($track['genre_slug'] ?? 'music') ?>"
        data-track-locked="0"
        data-play-endpoint="<?= url('/api/music/play') ?>"
    >
        <div class="music-player-art">
            <img class="music-player-cover" src="<?= e(url($cover)) ?>" alt="">
            <span class="music-player-art-glow" aria-hidden="true"></span>
        </div>

        <div class="music-player-main">
            <div class="music-player-info">
                <span class="music-player-kicker"><?= e($track['genre'] ?? 'Zalorox Music') ?><?= $isPremium ? ' - Premium' : ' - Free' ?></span>
                <strong><?= e($title) ?></strong>
                <span><?= e($artist) ?></span>
            </div>

            <div class="music-player-controls" aria-label="Muzik kontrolleri">
                <button class="music-player-skip" type="button" data-player-prev aria-label="Onceki sarki">
                    <span aria-hidden="true">&#9664;&#9664;</span>
                </button>
                <button class="music-player-toggle" type="button" data-player-toggle aria-label="Oynat veya duraklat">
                    <span aria-hidden="true">&#9658;</span>
                </button>
                <button class="music-player-skip" type="button" data-player-next aria-label="Sonraki sarki">
                    <span aria-hidden="true">&#9654;&#9654;</span>
                </button>
            </div>
        </div>

        <?php if ($audioSrc !== ''): ?>
            <div class="music-player-timeline">
                <span data-player-current>0:00</span>
                <input type="range" min="0" max="1000" value="0" data-player-progress aria-label="Ilerleme">
                <span data-player-remaining>-0:00</span>
            </div>
            <div class="music-player-volume">
                <button class="music-player-icon" type="button" data-player-mute aria-label="Sesi kapat veya ac">Ses</button>
                <input type="range" min="0" max="1" step="0.01" value="0.82" data-player-volume aria-label="Ses">
            </div>
        <?php else: ?>
            <div class="music-player-empty">
                <strong>Audio dosyasi eklenmemis.</strong>
                <p>Bu demo kayitta tarayici player yerine dis platform linkleri kullanilabilir.</p>
            </div>
        <?php endif; ?>

        <div class="music-player-inline-actions">
            <button class="pill" 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>
            <button class="pill" type="button" data-player-queue-toggle>Siradaki</button>
        </div>

        <?php if ($links !== []): ?>
            <div class="music-external-links">
                <?php foreach ($links as $label => $href): ?>
                    <a href="<?= e($href) ?>" target="_blank" rel="noopener noreferrer"><?= e($label) ?></a>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </section>
<?php endif; ?>
