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

$mode = $mode ?? 'create';
$track = $track ?? $content ?? null;
$isEdit = $mode === 'edit' && is_array($track);
$section = $section ?? ($isEdit ? 'muzik-duzenle' : 'muzik-yukle');
$configForUpload = $configForUpload ?? [];
$creator = Auth::user() ?? [];
$cover = (string) ($track['cover_image'] ?? '');
$audio = (string) ($track['audio_file'] ?? '');
$selectedPrivacy = (string) ($track['privacy'] ?? 'public');
$selectedGenreId = (int) ($track['category_id'] ?? (($track['genre_ids'][0] ?? 0)));
$action = $isEdit ? url('/api/studio/music/update') : url('/api/studio/music/create');
$artistGate = $artistGate ?? ['status' => 'missing', 'artist' => null];
$artistStatus = (string) ($artistGate['status'] ?? 'missing');
if (!$isEdit && $artistStatus !== 'approved'):
    $gateTitle = match ($artistStatus) {
        'pending' => 'Sanatci profilin inceleme bekliyor.',
        'rejected' => 'Sanatci profilin reddedildi.',
        default => 'Muzik yuklemek icin Artist Connect gerekli.',
    };
    $gateText = match ($artistStatus) {
        'pending' => 'ZPanel onayi tamamlandiginda muzik yukleme formu otomatik acilir.',
        'rejected' => 'Red gerekcesini Artist Connect sayfasinda gorebilir ve basvurunu guncelleyebilirsin.',
        default => 'Muzik yuklemek icin once Zalorox Artist Connect profili olusturmalisin.',
    };
?>
<div class="panel creator-shell">
    <?php require BASE_PATH . '/app/Views/partials/studio-sidebar.php'; ?>
    <section class="panel-body creator-upload-page">
        <div class="artist-lock-card">
            <p class="eyebrow">Zalorox Artist Connect</p>
            <h1><?= e($gateTitle) ?></h1>
            <p><?= e($gateText) ?></p>
            <div class="settings-action-list">
                <a class="pill primary" href="<?= e(url('/studyo/artist')) ?>">Artist Connect'e Git</a>
                <a class="pill" href="<?= e(url('/studyo/muziklerim')) ?>">Muziklerim</a>
            </div>
        </div>
    </section>
</div>
<?php
return;
endif;

$pageTitle = $isEdit ? 'Müzik Düzenle' : 'Müzik Yükle';
?>
<div class="panel creator-shell">
    <?php require BASE_PATH . '/app/Views/partials/studio-sidebar.php'; ?>
    <section class="panel-body creator-upload-page">
        <form
            class="creator-upload-form"
            action="<?= e($action) ?>"
            method="post"
            enctype="multipart/form-data"
            data-studio-upload-form
            data-upload-type="music"
        >
            <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
            <?php if ($isEdit): ?><input type="hidden" name="id" value="<?= (int) $track['id'] ?>"><?php endif; ?>
            <input type="hidden" name="submit_action" value="publish" data-submit-action>

            <div class="upload-layout">
                <div class="upload-main">
                    <header class="upload-head">
                        <div>
                            <p class="eyebrow">Studio</p>
                            <h1><?= e($pageTitle) ?></h1>
                            <p>Şarkını yükle, kapağını ekle, platform linklerini ve premium erişim ayarlarını tek ekranda düzenle.</p>
                        </div>
                        <span class="dev-chip">Audio upload</span>
                    </header>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Temel Bilgiler</h2>
                            <span>Zorunlu</span>
                        </div>
                        <div class="form-grid">
                            <label>
                                Sarki basligi
                                <input name="title" data-live-title required value="<?= e($track['title'] ?? '') ?>" placeholder="Gece Bloklari">
                                <span class="field-error" data-error-for="title"></span>
                            </label>
                            <label>
                                Sanatci adi
                                <input name="artist_name" data-live-artist required value="<?= e($track['artist_name'] ?? '') ?>" placeholder="Zalorox Beats">
                                <span class="field-error" data-error-for="artist_name"></span>
                            </label>
                        </div>
                        <label>
                            Aciklama
                            <textarea name="description" data-live-description rows="7" placeholder="Parca hikayesi, kullanim notlari veya altyapi bilgisi"><?= e($track['description'] ?? '') ?></textarea>
                            <span class="field-error" data-error-for="description"></span>
                        </label>
                        <?php $aiType = 'music'; require BASE_PATH . '/app/Views/partials/ai-tag-suggester.php'; ?>
                        <label>
                            Etiketler
                            <input name="tags" data-tag-input value="<?= e($track['tags'] ?? '') ?>" placeholder="arabesk rap, phonk, türkçe rap">
                            <span class="field-error" data-error-for="tags"></span>
                        </label>
                    </section>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Ses Dosyasi</h2>
                            <span><?= e($configForUpload['musicAllowedExtensions'] ?? 'mp3, wav, ogg, m4a') ?>, max <?= (int) ($configForUpload['musicMaxUploadMb'] ?? 100) ?> MB</span>
                        </div>
                        <?php
                        $id = 'music-audio';
                        $name = 'audio_file';
                        $label = 'Audio dosyasini surukle veya sec';
                        $hint = 'MP3, WAV, OGG, M4A';
                        $accept = '.mp3,.wav,.ogg,.m4a,.flac,audio/*';
                        $kind = 'audio';
                        $existing = '';
                        require BASE_PATH . '/app/Views/partials/upload-dropzone.php';
                        require BASE_PATH . '/app/Views/partials/audio-preview-card.php';
                        ?>
                        <span class="field-error" data-error-for="audio_file"></span>
                    </section>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Kapak Gorseli</h2>
                            <span>1:1 veya 16:9</span>
                        </div>
                        <?php
                        $id = 'music-cover';
                        $name = 'cover';
                        $label = 'Kapak gorselini surukle veya sec';
                        $hint = 'JPG, PNG, WEBP, GIF';
                        $accept = 'image/jpeg,image/png,image/webp,image/gif';
                        $kind = 'image';
                        $existing = $cover;
                        require BASE_PATH . '/app/Views/partials/upload-dropzone.php';
                        ?>
                    </section>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Muzik Linkleri</h2>
                            <span>Opsiyonel platform baglantilari</span>
                        </div>
                        <div class="form-grid">
                            <label>YouTube<input type="url" name="youtube_url" value="<?= e($track['youtube_url'] ?? '') ?>" placeholder="https://youtube.com/..."><span class="field-error" data-error-for="youtube_url"></span></label>
                            <label>Spotify<input type="url" name="spotify_url" value="<?= e($track['spotify_url'] ?? '') ?>" placeholder="https://open.spotify.com/..."><span class="field-error" data-error-for="spotify_url"></span></label>
                            <label>Apple Music<input type="url" name="apple_music_url" value="<?= e($track['apple_music_url'] ?? '') ?>" placeholder="https://music.apple.com/..."><span class="field-error" data-error-for="apple_music_url"></span></label>
                            <label>Diger platform<input type="url" name="external_link" value="<?= e($track['external_link'] ?? '') ?>" placeholder="https://..."><span class="field-error" data-error-for="external_link"></span></label>
                        </div>
                    </section>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Muzik Turu</h2>
                            <span>Chip/card secimi</span>
                        </div>
                        <?php
                        $selectedId = $selectedGenreId;
                        $name = 'genre_id';
                        $mode = 'music';
                        require BASE_PATH . '/app/Views/partials/category-selector.php';
                        ?>
                    </section>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Gizlilik ve Site Premium</h2>
                            <span>Creator paketi yok</span>
                        </div>
                        <p class="upload-note">Muzik tarafinda creator bazli paket secilmez. Premium secilen parcalar site premium aboneligi olan kullanicilara acilir.</p>
                        <div class="privacy-selector" data-privacy-selector>
                            <div class="privacy-grid">
                                <?php foreach ([['public', 'Herkese acik', 'Free dinleme'], ['premium', 'Site Premium', 'Reklamsiz/premium player'], ['private', 'Gizli', 'Sadece Studio/ZPanel']] as [$value, $label, $hint]): ?>
                                    <label class="privacy-card <?= $selectedPrivacy === $value ? 'is-selected' : '' ?>">
                                        <input type="radio" name="privacy" value="<?= e($value) ?>" <?= $selectedPrivacy === $value ? 'checked' : '' ?>>
                                        <strong><?= e($label) ?></strong>
                                        <span><?= e($hint) ?></span>
                                    </label>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </section>
                </div>

                <aside class="upload-preview-panel">
                    <div class="live-preview-card music-live" data-live-preview>
                        <div class="live-cover is-square">
                            <?php if ($cover !== ''): ?>
                                <img src="<?= e($cover) ?>" alt="" data-live-cover>
                            <?php else: ?>
                                <img alt="" data-live-cover hidden>
                                <span data-live-cover-empty>Kapak</span>
                            <?php endif; ?>
                        </div>
                        <div class="live-preview-body">
                            <span class="live-badge" data-live-privacy><?= in_array($selectedPrivacy, ['premium', 'subscriber'], true) ? 'Premium' : 'Free' ?></span>
                            <h3 data-live-preview-title><?= e($track['title'] ?? 'Sarki adi') ?></h3>
                            <p data-live-preview-artist><?= e($track['artist_name'] ?? 'Sanatci') ?></p>
                            <button class="mini-play" type="button" aria-label="Preview play">▶</button>
                        </div>
                    </div>
                    <div class="upload-progress" data-upload-progress hidden>
                        <span><b data-progress-label>0%</b></span>
                        <div><i data-progress-bar></i></div>
                    </div>
                    <div class="submit-bar">
                        <button class="pill" type="submit" data-submit-intent="draft">Taslak Kaydet</button>
                        <button class="pill primary" type="submit" data-submit-intent="publish">Yayinla / Incelemeye Gonder</button>
                        <button class="pill" type="reset" data-form-clear>Temizle</button>
                        <?php if ($isEdit): ?>
                            <button class="pill danger" type="button" data-delete-content data-content-id="<?= (int) $track['id'] ?>" data-delete-endpoint="<?= e(url('/api/studio/music/delete')) ?>">Sil</button>
                        <?php endif; ?>
                    </div>
                </aside>
            </div>
        </form>
    </section>
</div>
