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

$mode = $mode ?? 'create';
$mod = $mod ?? $content ?? null;
$isEdit = $mode === 'edit' && is_array($mod);
$section = $section ?? ($isEdit ? 'mod-duzenle' : 'mod-yukle');
$configForUpload = $configForUpload ?? [];
$creator = Auth::user() ?? [];
$cover = (string) ($mod['cover_image'] ?? $mod['cover_path'] ?? '');
$selectedPrivacy = (string) ($mod['privacy'] ?? 'public');
$selectedPackageId = (int) ($mod['package_id'] ?? 0);
$action = $isEdit ? url('/api/studio/mod/update') : url('/api/studio/mod/create');
$pageTitle = $isEdit ? 'Mod Düzenle' : 'Mod 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="mod"
        >
            <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
            <?php if ($isEdit): ?><input type="hidden" name="id" value="<?= (int) $mod['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>Modunu paylaş, kapak ve galeri görsellerini ekle, güvenli indirme linkini Zalorox Shield ile denetlet.</p>
                        </div>
                        <span class="dev-chip">Zalorox Shield</span>
                    </header>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Temel Bilgiler</h2>
                            <span>Zorunlu</span>
                        </div>
                        <div class="form-grid">
                            <label>
                                Mod basligi
                                <input name="title" data-live-title required value="<?= e($mod['title'] ?? '') ?>" placeholder="FS25 Pro Farm Pack">
                                <span class="field-error" data-error-for="title"></span>
                            </label>
                        </div>
                        <label>
                            Aciklama
                            <textarea name="description" data-live-description rows="8" placeholder="Mod detaylari, surum notlari ve kurulum bilgileri"><?= e($mod['description'] ?? '') ?></textarea>
                            <span class="field-error" data-error-for="description"></span>
                        </label>
                        <?php $aiType = 'mod'; require BASE_PATH . '/app/Views/partials/ai-tag-suggester.php'; ?>
                        <label>
                            Etiketler
                            <input name="tags" data-tag-input data-live-tags value="<?= e($mod['tags'] ?? '') ?>" placeholder="fs25, john deere, traktör">
                            <span class="field-error" data-error-for="tags"></span>
                        </label>
                    </section>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Indirme Linki</h2>
                            <span>Drive, Dropbox, OneDrive, MediaFire, ShareMods, Mega.nz</span>
                        </div>
                        <p class="upload-note">Mod dosyası siteye direkt yüklenmez. Google Drive, Dropbox, OneDrive, MediaFire, ShareMods veya Mega.nz gibi güvenilir harici bağlantı kullan.</p>
                        <div class="link-scan-row">
                            <input name="external_link" type="url" data-link-input value="<?= e($mod['external_link'] ?? $mod['file_url'] ?? '') ?>" placeholder="https://drive.google.com/...">
                            <button class="pill" type="button" data-link-scan>Linki Denetle</button>
                        </div>
                        <input type="hidden" name="link_scan_status" value="<?= e($mod['external_link_status'] ?? 'unchecked') ?>" data-link-status-input>
                        <div class="shield-status" data-shield-status>
                            <strong>Zalorox Shield</strong>
                            <span><?= e($mod['external_link_status'] ?? 'Denetim bekliyor') ?></span>
                        </div>
                        <span class="field-error" data-error-for="external_link"></span>
                    </section>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Kapak Gorseli</h2>
                            <span>16:9, max <?= (int) ($configForUpload['modMaxCoverMb'] ?? 12) ?> MB</span>
                        </div>
                        <?php
                        $id = 'mod-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>Slider / Galeri</h2>
                            <span>Max <?= (int) ($configForUpload['modMaxGalleryItems'] ?? 10) ?> oge</span>
                        </div>
                        <?php
                        $id = 'mod-gallery';
                        $name = 'gallery[]';
                        $label = 'Galeri gorsellerini surukle veya sec';
                        $hint = 'Coklu gorsel, siralama surukle-birak ile';
                        $accept = 'image/jpeg,image/png,image/webp,image/gif';
                        $multiple = true;
                        $kind = 'gallery';
                        $existing = '';
                        require BASE_PATH . '/app/Views/partials/upload-dropzone.php';
                        $id = 'mod-gallery-grid';
                        $images = $mod['images'] ?? [];
                        require BASE_PATH . '/app/Views/partials/image-preview-grid.php';
                        ?>
                    </section>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Video Linkleri</h2>
                            <span>YouTube veya Vimeo</span>
                        </div>
                        <?php $videos = $mod['videos'] ?? []; require BASE_PATH . '/app/Views/partials/video-link-preview.php'; ?>
                    </section>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>3D Model</h2>
                            <span><?= e($configForUpload['modelAllowedExtensions'] ?? 'glb, gltf, usdz') ?>, max <?= (int) ($configForUpload['modelMaxUploadMb'] ?? 100) ?> MB</span>
                        </div>
                        <p class="upload-note">GLB, GLTF ve opsiyonel USDZ dosyalari galeri icinde 3D oge olarak gorunur. OBJ/FBX icin donusturme destegi TODO.</p>
                        <div class="form-grid">
                            <label>
                                Model basligi
                                <input name="model_title" data-model-title placeholder="Traktor 3D onizleme">
                            </label>
                            <label>
                                Harici 3D model linki
                                <input type="url" data-model-link-input placeholder="https://.../model.glb">
                            </label>
                        </div>
                        <?php
                        $id = 'mod-models';
                        $name = 'models[]';
                        $label = '3D model dosyalarini surukle veya sec';
                        $hint = 'GLB, GLTF, USDZ';
                        $accept = '.glb,.gltf,.usdz,model/gltf-binary,model/gltf+json,model/vnd.usdz+zip';
                        $multiple = true;
                        $kind = 'model';
                        $existing = '';
                        require BASE_PATH . '/app/Views/partials/upload-dropzone.php';
                        ?>
                        <label>
                            Model thumbnail
                            <input type="file" name="model_thumbnail" accept="image/jpeg,image/png,image/webp,image/gif">
                        </label>
                        <div class="inline-input-row">
                            <button class="pill" type="button" data-add-model-link>Harici Linki Ekle</button>
                        </div>
                        <div class="model-preview-list" data-model-list>
                            <?php foreach (($mod['models'] ?? []) as $modelItem): ?>
                                <?php
                                $modelPath = (string) ($modelItem['model_path'] ?? '');
                                if ($modelPath === '') {
                                    continue;
                                }
                                ?>
                                <div class="model-preview-item">
                                    <span>3D</span>
                                    <label>Baslik<input name="existing_model_titles[]" value="<?= e((string) ($modelItem['title'] ?? '3D Model')) ?>"></label>
                                    <small><?= e($modelPath) ?></small>
                                    <input type="hidden" name="existing_model_paths[]" value="<?= e($modelPath) ?>">
                                    <input type="hidden" name="existing_model_thumbnails[]" value="<?= e((string) ($modelItem['thumbnail_path'] ?? '')) ?>">
                                    <button type="button" data-remove-model>Sil</button>
                                </div>
                            <?php endforeach; ?>
                        </div>
                        <span class="field-error" data-error-for="models"></span>
                    </section>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Kategori Secimi</h2>
                            <span>Ana kategori ve alt kategori</span>
                        </div>
                        <?php
                        $selectedId = (int) ($mod['category_id'] ?? 0);
                        $name = 'category_id';
                        $mode = 'mod';
                        require BASE_PATH . '/app/Views/partials/category-selector.php';
                        ?>
                    </section>

                    <section class="upload-panel">
                        <div class="upload-panel-head">
                            <h2>Gizlilik ve Paket Erisimi</h2>
                            <span>Free veya premium</span>
                        </div>
                        <?php
                        $privacy = $selectedPrivacy;
                        require BASE_PATH . '/app/Views/partials/privacy-package-selector.php';
                        ?>
                    </section>
                </div>

                <aside class="upload-preview-panel">
                    <div class="live-preview-card" data-live-preview>
                        <div class="live-cover">
                            <?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><?= $selectedPackageId > 0 ? 'Premium' : 'Free' ?></span>
                            <h3 data-live-preview-title><?= e($mod['title'] ?? 'Mod basligi') ?></h3>
                            <p data-live-preview-category>Kategori secilmedi</p>
                            <small><?= e($creator['display_name'] ?? $creator['nickname'] ?? $creator['username'] ?? 'Creator') ?></small>
                        </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) $mod['id'] ?>" data-delete-endpoint="<?= e(url('/api/studio/mod/delete')) ?>">Sil</button>
                        <?php endif; ?>
                    </div>
                </aside>
            </div>
        </form>
    </section>
</div>
