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

$title = (string) ($mod['title'] ?? 'Untitled Mod');
$category = (string) ($mod['category'] ?? $mod['category_name'] ?? 'Mods');
$categorySlug = (string) ($mod['category_slug'] ?? '');
$authorName = (string) ($mod['author_name'] ?? $mod['author'] ?? 'Creator');
$authorSlug = (string) ($mod['author'] ?? $mod['creator_username'] ?? 'creator');
$authorAvatar = (string) ($mod['author_avatar'] ?? '');
$downloads = (int) ($mod['download_count'] ?? $mod['downloads'] ?? 0);
$views = (int) ($mod['view_count'] ?? $mod['views'] ?? 0);
$likes = (int) ($mod['like_count'] ?? 0);
$dislikes = (int) ($mod['dislike_count'] ?? 0);
$isPremium = !empty($mod['premium']) || !empty($mod['is_premium']);
$isFeatured = !empty($mod['featured']);
$description = trim((string) ($mod['description'] ?? $mod['summary'] ?? 'Bu mod icin aciklama daha sonra eklenecek.'));
$createdAt = (string) ($mod['published_at'] ?? $mod['created_at'] ?? '');
$updatedAt = (string) ($mod['updated_at'] ?? '');
$version = (string) ($mod['version'] ?? '1.0.0');
$gameVersion = (string) ($mod['game_version'] ?? 'FS25');
$externalStatus = (string) ($mod['external_link_status'] ?? 'unchecked');
$riskScore = (int) ($mod['external_link_risk_score'] ?? 0);
$userReaction = $userReaction ?? null;
$canDownload = $canDownload ?? Auth::userCanAccessMod($mod);

$compact = static function (int $value): string {
    if ($value >= 1000000) {
        return rtrim(rtrim(number_format($value / 1000000, 1, '.', ''), '0'), '.') . 'M';
    }
    if ($value >= 1000) {
        return rtrim(rtrim(number_format($value / 1000, 1, '.', ''), '0'), '.') . 'K';
    }
    return (string) $value;
};

$dateLabel = static function (string $value): string {
    $time = $value !== '' ? strtotime($value) : false;
    return $time ? date('d.m.Y', $time) : 'Tarih yok';
};
?>
<section class="container mod-detail mod-watch" data-mod-page data-mod-id="<?= (int) ($mod['id'] ?? 0) ?>" data-mod-slug="<?= e((string) ($mod['slug'] ?? '')) ?>" data-mod-category="<?= e($categorySlug) ?>" data-view-endpoint="<?= url('/api/mods/' . (int) ($mod['id'] ?? 0) . '/view') ?>">
    <div class="mod-main">
        <?php require BASE_PATH . '/app/Views/partials/mod-gallery.php'; ?>

        <header class="mod-title-block">
            <div class="mod-kicker">
                <?php if ($categorySlug !== ''): ?>
                    <a href="<?= url('/kategori/' . $categorySlug) ?>"><?= e($category) ?></a>
                <?php else: ?>
                    <span><?= e($category) ?></span>
                <?php endif; ?>
                <?php if ($isPremium): ?><span>Premium</span><?php else: ?><span>Free</span><?php endif; ?>
                <?php if ($isFeatured): ?><span>One cikan</span><?php endif; ?>
            </div>
            <h1><?= e($title) ?></h1>
            <?php if (!empty($translationNote)): ?><p class="translation-note"><?= e((string) $translationNote) ?></p><?php endif; ?>
            <div class="mod-meta-inline">
                <span data-view-count><?= number_format($views, 0, ',', '.') ?> goruntulenme</span>
                <span><?= e($dateLabel($createdAt)) ?></span>
                <span><?= e($category) ?></span>
                <?php if ($updatedAt !== ''): ?><span>Guncellendi <?= e($dateLabel($updatedAt)) ?></span><?php endif; ?>
            </div>
        </header>

        <div class="mod-social-row">
            <section class="creator-bar">
                <a class="creator-identity" href="<?= url('/kullanici/' . $authorSlug) ?>">
                    <span class="avatar large">
                        <?php if ($authorAvatar !== ''): ?>
                            <img src="<?= url($authorAvatar) ?>" alt="">
                        <?php else: ?>
                            <?= e(strtoupper(substr($authorName, 0, 1))) ?>
                        <?php endif; ?>
                    </span>
                    <span>
                        <strong><?= e($authorName) ?></strong>
                        <small>@<?= e($authorSlug) ?></small>
                    </span>
                </a>
                <div class="creator-actions">
                    <button class="pill" type="button" data-follow-placeholder>Takip Et</button>
                    <a class="pill primary" href="<?= url('/checkout') ?>">Abone Ol</a>
                </div>
            </section>

            <section class="mod-action-bar" aria-label="Mod aksiyonlari">
                <button class="pill reaction <?= $userReaction === 'like' ? 'is-active' : '' ?>" type="button" data-reaction="like" data-endpoint="<?= url('/api/mods/' . (int) ($mod['id'] ?? 0) . '/reaction') ?>">
                    Like <span data-like-count><?= $compact($likes) ?></span>
                </button>
                <button class="pill reaction <?= $userReaction === 'dislike' ? 'is-active' : '' ?>" type="button" data-reaction="dislike" data-endpoint="<?= url('/api/mods/' . (int) ($mod['id'] ?? 0) . '/reaction') ?>">
                    Dislike <span data-dislike-count><?= $compact($dislikes) ?></span>
                </button>
                <button class="pill" type="button" data-share data-share-title="<?= e($title) ?>">Paylas</button>
                <?php if ($canDownload): ?>
                    <button class="pill primary download-pill" type="button" data-download-action data-endpoint="<?= url('/api/mods/' . (int) ($mod['id'] ?? 0) . '/download') ?>" data-download-label="Indir">
                        Indir <span data-download-count><?= $compact($downloads) ?></span>
                    </button>
                <?php else: ?>
                    <span class="pill download-pill premium-locked-pill">Premium Kilitli</span>
                <?php endif; ?>
            </section>
        </div>

        <?php if (!$canDownload && $isPremium): ?>
            <section class="premium-lock-card">
                <div>
                    <p class="eyebrow">Premium erisim</p>
                    <h2>Bu mod abonelik gerektiriyor</h2>
                    <p><?= e($authorName) ?> paketlerinden birine aktif aboneligin varsa indirme akisi Zalorox Shield kontroluyle normal calisir.</p>
                </div>
                <a class="pill primary" href="<?= url('/kullanici/' . $authorSlug) ?>#packages">Creator Paketleri</a>
            </section>
        <?php endif; ?>

        <article class="description mod-description" data-description>
            <div class="description-body" data-description-body>
                <h2>Aciklama</h2>
                <p><?= nl2br(e($description)) ?></p>
                <dl class="mod-meta-list">
                    <div><dt>Kurulum notu</dt><dd>Kurulum notu henuz eklenmedi.</dd></div>
                    <div><dt>Link durumu</dt><dd><?= e($externalStatus) ?> / risk <?= $riskScore ?></dd></div>
                    <div><dt>Kategori</dt><dd><?= e($category) ?></dd></div>
                    <div><dt>Etiketler</dt><dd><?= e($gameVersion) ?>, v<?= e($version) ?>, <?= $isPremium ? 'premium' : 'free' ?></dd></div>
                    <?php if ($updatedAt !== ''): ?><div><dt>Guncelleme</dt><dd><?= e($dateLabel($updatedAt)) ?></dd></div><?php endif; ?>
                </dl>
            </div>
            <button class="description-toggle" type="button" data-description-toggle data-more="Daha fazla" data-less="Daha az">Daha fazla</button>
        </article>

        <?php
        $contentType = 'mod';
        $contentId = (int) ($mod['id'] ?? 0);
        require BASE_PATH . '/app/Views/partials/comment-box.php';
        ?>
    </div>

    <aside class="related" aria-label="Onerilen modlar">
        <h2>Onerilen Modlar</h2>
        <?php if (($related ?? []) === []): ?>
            <div class="empty-state small">
                <strong>Oneri yok</strong>
                <p>Yeni modlar eklendikce burada gorunecek.</p>
            </div>
        <?php endif; ?>
        <?php foreach (($related ?? []) as $item): ?>
            <?php require BASE_PATH . '/app/Views/partials/related-mod-card.php'; ?>
        <?php endforeach; ?>
    </aside>
</section>
