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

$section = $section ?? 'profilim';
$profile = $profile ?? [];
$avatar = (string) ($profile['avatar'] ?? '');
$cover = (string) ($profile['cover_image'] ?? '');
$nickname = (string) (($profile['nickname'] ?? '') ?: ($profile['username'] ?? 'profil'));
$displayName = (string) (($profile['display_name'] ?? '') ?: $nickname);
$about = (string) ($profile['about'] ?? '');
$privacy = (string) (($profile['privacy'] ?? '') ?: 'public');
$messagePrivacy = (string) (($profile['message_privacy'] ?? '') ?: 'everyone');
$communityPosts = $communityPosts ?? [];
$stories = $stories ?? [];
$privacyOptions = [
    ['public', 'Herkese acik'],
    ['logged_in', 'Uyeler'],
    ['private', 'Gizli'],
];
$messageOptions = [
    ['everyone', 'Herkes'],
    ['friends', 'Arkadaslar'],
    ['none', 'Kapali'],
];
?>
<div class="panel creator-shell">
    <?php require BASE_PATH . '/app/Views/partials/studio-sidebar.php'; ?>
    <section class="panel-body studio-profile-page">
        <header class="studio-section-head">
            <div>
                <p class="eyebrow">Studio</p>
                <h1>Profilim</h1>
                <p>Profil sayfanda gorunen kapak, avatar, aciklama ve sosyal baglantilari buradan duzenle.</p>
            </div>
            <a class="pill" href="<?= url('/kullanici/' . $nickname) ?>">Profili Gor</a>
        </header>

        <?php if (!empty($loadError)): ?>
            <div class="dev-alert"><?= e((string) $loadError) ?></div>
        <?php endif; ?>

        <div class="studio-profile-layout">
            <div class="studio-profile-main">
                <section class="studio-card studio-profile-media-card">
                    <div class="studio-card-head">
                        <div>
                            <h2>Profil Medyasi</h2>
                            <p>JPG, PNG, WEBP ve GIF desteklenir.</p>
                        </div>
                    </div>

                    <div class="studio-profile-cover-preview" data-profile-cover-preview>
                        <?php if ($cover !== ''): ?>
                            <img src="<?= e(url($cover)) ?>" alt="">
                        <?php else: ?>
                            <span>Kapak</span>
                        <?php endif; ?>
                    </div>

                    <div class="studio-profile-avatar-row">
                        <div class="studio-profile-avatar-preview" data-profile-avatar-preview>
                            <?php if ($avatar !== ''): ?>
                                <img src="<?= e(url($avatar)) ?>" alt="">
                            <?php else: ?>
                                <?= e(strtoupper(substr($displayName, 0, 1) ?: 'Z')) ?>
                            <?php endif; ?>
                        </div>
                        <div>
                            <strong><?= e($displayName) ?></strong>
                            <span>@<?= e($nickname) ?></span>
                        </div>
                    </div>

                    <div class="studio-profile-upload-grid">
                        <form action="<?= url('/api/studio/profile/avatar') ?>" method="post" enctype="multipart/form-data" data-profile-image-form data-profile-image-type="avatar">
                            <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                            <label class="studio-profile-dropzone" data-profile-dropzone>Profil resmi
                                <span class="upload-dropzone-icon" aria-hidden="true">+</span>
                                <strong>Avatar sec veya surukle</strong>
                                <small>JPG, PNG, WEBP, GIF</small>
                                <input type="file" name="avatar" accept="image/jpeg,image/png,image/webp,image/gif" data-profile-image-input>
                            </label>
                            <span class="field-error" data-error-for="avatar"></span>
                            <div class="profile-upload-actions">
                                <button class="pill primary" type="submit">Degistir</button>
                                <button class="pill" type="button" data-profile-image-remove>Sil</button>
                            </div>
                        </form>

                        <form action="<?= url('/api/studio/profile/cover') ?>" method="post" enctype="multipart/form-data" data-profile-image-form data-profile-image-type="cover">
                            <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                            <label class="studio-profile-dropzone" data-profile-dropzone>Kapak resmi
                                <span class="upload-dropzone-icon" aria-hidden="true">+</span>
                                <strong>Kapak sec veya surukle</strong>
                                <small>Genis 16:9 gorsel onerilir</small>
                                <input type="file" name="cover" accept="image/jpeg,image/png,image/webp,image/gif" data-profile-image-input>
                            </label>
                            <span class="field-error" data-error-for="cover"></span>
                            <div class="profile-upload-actions">
                                <button class="pill primary" type="submit">Degistir</button>
                                <button class="pill" type="button" data-profile-image-remove>Sil</button>
                            </div>
                        </form>
                    </div>
                </section>

                <form class="studio-card studio-profile-form" action="<?= url('/api/studio/profile/update') ?>" method="post" data-studio-profile-form>
                    <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                    <div class="studio-card-head">
                        <div>
                            <h2>Profil Bilgileri</h2>
                            <p>Degisiklikler localhostta gercek MySQL kaydina yazilir.</p>
                        </div>
                    </div>

                    <div class="form-grid">
                        <label>Gorunen ad
                            <input name="display_name" value="<?= e($displayName) ?>" maxlength="120" required>
                            <span class="field-error" data-error-for="display_name"></span>
                        </label>
                        <label>Takma ad / nickname
                            <input name="nickname" value="<?= e($nickname) ?>" maxlength="80" pattern="[a-z0-9._-]{3,80}" required>
                            <span class="field-error" data-error-for="nickname"></span>
                        </label>
                    </div>

                    <label>Hakkimda
                        <textarea name="about" maxlength="2000" rows="7"><?= e($about) ?></textarea>
                        <span class="field-error" data-error-for="about"></span>
                    </label>

                    <div class="form-grid">
                        <label>Instagram
                            <input name="instagram" type="url" value="<?= e((string) ($profile['instagram'] ?? '')) ?>" placeholder="https://instagram.com/...">
                            <span class="field-error" data-error-for="instagram"></span>
                        </label>
                        <label>YouTube
                            <input name="youtube" type="url" value="<?= e((string) ($profile['youtube'] ?? '')) ?>" placeholder="https://youtube.com/@...">
                            <span class="field-error" data-error-for="youtube"></span>
                        </label>
                        <label>Website
                            <input name="profile_website" type="url" value="<?= e((string) ($profile['website'] ?? '')) ?>" placeholder="https://...">
                            <span class="field-error" data-error-for="profile_website"></span>
                        </label>
                    </div>

                    <div class="studio-profile-options">
                        <fieldset>
                            <legend>Profil gorunurlugu</legend>
                            <?php foreach ($privacyOptions as [$value, $label]): ?>
                                <label><input type="radio" name="privacy" value="<?= e($value) ?>" <?= $privacy === $value ? 'checked' : '' ?>> <?= e($label) ?></label>
                            <?php endforeach; ?>
                            <span class="field-error" data-error-for="privacy"></span>
                        </fieldset>
                        <fieldset>
                            <legend>Mesaj gizliligi</legend>
                            <?php foreach ($messageOptions as [$value, $label]): ?>
                                <label><input type="radio" name="message_privacy" value="<?= e($value) ?>" <?= $messagePrivacy === $value ? 'checked' : '' ?>> <?= e($label) ?></label>
                            <?php endforeach; ?>
                            <span class="field-error" data-error-for="message_privacy"></span>
                        </fieldset>
                    </div>

                    <div class="profile-form-actions">
                        <button class="pill primary" type="submit">Profili Kaydet</button>
                        <a class="pill" href="<?= url('/kullanici/' . $nickname) ?>">Public Profili Ac</a>
                    </div>
                </form>

                <?php require BASE_PATH . '/app/Views/partials/community-composer.php'; ?>

                <section class="studio-community-list">
                    <div class="studio-card-head">
                        <div>
                            <h2>Son Topluluk Gonderileri</h2>
                            <p>Yayinda, incelemede veya engellenmis son paylasimlarin.</p>
                        </div>
                    </div>
                    <?php if ($communityPosts === []): ?>
                        <div class="empty-state small"><strong>Henuz topluluk gonderisi yok.</strong><p>Ilk duyurunu yukaridan yayinlayabilirsin.</p></div>
                    <?php else: ?>
                        <?php foreach ($communityPosts as $post): ?>
                            <?php require BASE_PATH . '/app/Views/partials/community-post-card.php'; ?>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </section>
            </div>

            <aside class="studio-profile-preview">
                <div class="profile-preview-card">
                    <div class="profile-preview-cover">
                        <?php if ($cover !== ''): ?><img src="<?= e(url($cover)) ?>" alt=""><?php endif; ?>
                    </div>
                    <div class="profile-preview-body">
                        <div class="studio-profile-avatar-preview small">
                            <?php if ($avatar !== ''): ?><img src="<?= e(url($avatar)) ?>" alt=""><?php else: ?><?= e(strtoupper(substr($displayName, 0, 1) ?: 'Z')) ?><?php endif; ?>
                        </div>
                        <strong><?= e($displayName) ?></strong>
                        <span>@<?= e($nickname) ?></span>
                        <p><?= e($about !== '' ? $about : 'Hakkimda metni henuz eklenmedi.') ?></p>
                    </div>
                </div>

                <?php
                $storyTitle = 'Story / Durum';
                $storyEmpty = 'Aktif storyn yok.';
                $storyAllowCreate = true;
                $storyInlineComposer = true;
                require BASE_PATH . '/app/Views/partials/story-strip.php';
                ?>
            </aside>
        </div>
    </section>
</div>
<?php require BASE_PATH . '/app/Views/partials/story-viewer.php'; ?>
