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

$comments = $comments ?? [];
$contentType = $contentType ?? 'mod';
$contentId = (int) ($contentId ?? ($mod['id'] ?? 0));
$isAuthed = Auth::check();
?>
<section class="comments mod-comments" data-comments data-content-type="<?= e($contentType) ?>" data-content-id="<?= $contentId ?>" data-list-endpoint="<?= url('/api/comments/list?content_type=' . rawurlencode((string) $contentType) . '&content_id=' . $contentId) ?>">
    <div class="comments-head">
        <div>
            <h2>Yorumlar</h2>
            <p><?= count($comments) ?> yorum gorunuyor</p>
        </div>
    </div>

    <?php if ($isAuthed): ?>
        <form class="comment-form" data-comment-form action="<?= url('/api/comments/create') ?>" method="post">
            <?= Csrf::field() ?>
            <input type="hidden" name="content_type" value="<?= e($contentType) ?>">
            <input type="hidden" name="content_id" value="<?= $contentId ?>">
            <input type="hidden" name="parent_id" value="">
            <textarea name="content" placeholder="Yorum yaz..." maxlength="3000" required></textarea>
            <div class="comment-form-actions">
                <span>Topluluk kurallarina uygun ve yapici kal.</span>
                <div class="comment-tools" data-comment-emoji>
                    <button type="button">😀</button>
                    <button type="button">🔥</button>
                    <button type="button">👍</button>
                    <button type="button">💜</button>
                </div>
                <button class="pill primary" type="submit">Gonder</button>
            </div>
        </form>
    <?php else: ?>
        <div class="login-note">
            <p>Yorum yazmak icin giris yapmalisin.</p>
            <a class="pill" href="<?= url('/giris-yap') ?>">Giris Yap</a>
            <a class="pill primary" href="<?= url('/kayit') ?>">Kayit Ol</a>
        </div>
    <?php endif; ?>

    <div class="comment-list" data-comment-list>
        <?php if ($comments === []): ?>
            <div class="empty-state small" data-comment-empty>
                <strong>Henuz yorum yok</strong>
                <p>Ilk yorumu giris yapmis bir kullanici birakabilir.</p>
            </div>
        <?php endif; ?>

        <?php foreach ($comments as $comment): ?>
            <?php
            $display = (string) ($comment['display_name'] ?? $comment['username'] ?? 'User');
            $avatar = (string) ($comment['avatar'] ?? '');
            ?>
            <article class="comment<?= !empty($comment['parent_id']) ? ' is-reply' : '' ?>" data-comment-id="<?= (int) ($comment['id'] ?? 0) ?>" data-parent-id="<?= e((string) ($comment['parent_id'] ?? '')) ?>">
                <span class="avatar">
                    <?php if ($avatar !== ''): ?>
                        <img src="<?= url($avatar) ?>" alt="">
                    <?php else: ?>
                        <?= e(strtoupper(substr($display, 0, 1))) ?>
                    <?php endif; ?>
                </span>
                <div>
                    <header>
                        <strong><?= e($display) ?></strong>
                        <time><?= e(date('d.m.Y H:i', strtotime((string) ($comment['created_at'] ?? 'now')))) ?></time>
                    </header>
                    <p data-comment-body><?= nl2br(e((string) ($comment['content'] ?? ''))) ?></p>
                    <div class="comment-actions">
                        <button type="button" data-comment-reply>Yanitla</button>
                        <button type="button" data-comment-like>Begen <span><?= (int) ($comment['like_count'] ?? 0) ?></span></button>
                        <?php if (Auth::id() && (int) Auth::id() === (int) ($comment['user_id'] ?? 0)): ?>
                            <button type="button" data-comment-edit>Duzenle</button>
                            <button type="button" data-comment-delete>Sil</button>
                        <?php endif; ?>
                    </div>
                </div>
            </article>
        <?php endforeach; ?>
    </div>
</section>
