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

$post = $post ?? [];
$viewerId = (int) (Auth::id() ?? 0);
$postId = (int) ($post['id'] ?? 0);
$displayName = (string) (($post['display_name'] ?? '') ?: ($post['nickname'] ?? $post['username'] ?? 'User'));
$nickname = (string) (($post['nickname'] ?? '') ?: ($post['username'] ?? 'user'));
$avatar = (string) ($post['avatar'] ?? '');
$content = (string) ($post['content'] ?? '');
$imagePath = (string) ($post['image_path'] ?? '');
$createdAt = (string) ($post['created_at'] ?? '');
$status = (string) ($post['status'] ?? 'published');
$postType = (string) ($post['post_type'] ?? 'text');
$likeCount = (int) ($post['like_count'] ?? 0);
$dislikeCount = (int) ($post['dislike_count'] ?? 0);
$commentCount = (int) ($post['comment_count'] ?? 0);
$viewerReaction = (string) ($post['viewer_reaction'] ?? '');
$canDelete = $viewerId > 0 && ($viewerId === (int) ($post['user_id'] ?? 0) || Auth::role('admin'));
$isCompact = (bool) ($communityCardCompact ?? false);
$shareUrl = url('/kullanici/' . $nickname) . '?post=' . $postId;
$mediaUrl = static function (string $path): string {
    $path = trim($path);
    if ($path === '') {
        return '';
    }

    return preg_match('~^https?://~i', $path) ? $path : url('/' . ltrim($path, '/'));
};
$formattedDate = $createdAt !== '' ? date('d.m.Y H:i', strtotime($createdAt)) : '';
?>
<article
    class="community-post-card<?= $isCompact ? ' is-compact' : '' ?>"
    data-community-post
    data-post-id="<?= $postId ?>"
    data-share-url="<?= e($shareUrl) ?>"
>
    <header class="community-post-head">
        <a class="community-post-author" href="<?= e(url('/kullanici/' . $nickname)) ?>">
            <span class="avatar community-avatar">
                <?php if ($avatar !== ''): ?><img src="<?= e($mediaUrl($avatar)) ?>" alt="<?= e($displayName) ?>"><?php else: ?><?= e(strtoupper(substr($displayName, 0, 1) ?: 'Z')) ?><?php endif; ?>
            </span>
            <span>
                <strong><?= e($displayName) ?></strong>
                <small>@<?= e($nickname) ?><?= $formattedDate !== '' ? ' - ' . e($formattedDate) : '' ?></small>
            </span>
        </a>
        <div class="community-post-tools">
            <?php if ($status !== 'published'): ?><span class="community-status-badge status-<?= e($status) ?>"><?= e($status) ?></span><?php endif; ?>
            <?php if ($canDelete): ?><button class="icon-btn community-mini-action" type="button" data-community-delete aria-label="Gonderiyi sil">&times;</button><?php endif; ?>
        </div>
    </header>

    <?php if ($content !== ''): ?>
        <p class="community-post-text"><?= nl2br(e($content)) ?></p>
    <?php endif; ?>

    <?php if ($imagePath !== ''): ?>
        <figure class="community-post-image">
            <img src="<?= e($mediaUrl($imagePath)) ?>" alt="<?= e($displayName) ?> topluluk gorseli" loading="lazy">
        </figure>
    <?php endif; ?>

    <?php if ($postType === 'poll'): ?>
        <?php
        $pollOptions = $post['poll_options'] ?? [];
        $pollTotal = (int) ($post['poll_total_votes'] ?? 0);
        ?>
        <div class="community-poll" data-community-poll>
            <strong><?= e((string) ($post['poll_question'] ?? 'Anket')) ?></strong>
            <div class="community-poll-options">
                <?php foreach ($pollOptions as $option): ?>
                    <?php $percent = max(0, min(100, (int) ($option['percent'] ?? 0))); ?>
                    <button
                        type="button"
                        class="community-poll-option<?= !empty($option['viewer_voted']) ? ' is-selected' : '' ?>"
                        style="--poll-percent: <?= $percent ?>%;"
                        data-community-poll-option
                        data-option-id="<?= (int) ($option['id'] ?? 0) ?>"
                        <?= $viewerId > 0 ? '' : 'data-auth-required data-auth-title="Giris gerekli" data-auth-message="Ankete oy vermek icin giris yapmalisin."' ?>
                    >
                        <span class="community-poll-fill"></span>
                        <span><?= e((string) ($option['option_text'] ?? 'Secenek')) ?></span>
                        <em><?= $percent ?>%</em>
                    </button>
                <?php endforeach; ?>
            </div>
            <small><?= e(number_format($pollTotal, 0, ',', '.')) ?> oy</small>
        </div>
    <?php endif; ?>

    <footer class="community-post-actions">
        <button class="pill community-react<?= $viewerReaction === 'like' ? ' is-active' : '' ?>" type="button" data-community-react="like" <?= $viewerId > 0 ? '' : 'data-auth-required data-auth-title="Giris gerekli" data-auth-message="Begenmek icin giris yapmalisin."' ?>>
            <span>Like</span><strong data-like-count><?= e((string) $likeCount) ?></strong>
        </button>
        <button class="pill community-react<?= $viewerReaction === 'dislike' ? ' is-active' : '' ?>" type="button" data-community-react="dislike" <?= $viewerId > 0 ? '' : 'data-auth-required data-auth-title="Giris gerekli" data-auth-message="Dislike icin giris yapmalisin."' ?>>
            <span>Dislike</span><strong data-dislike-count><?= e((string) $dislikeCount) ?></strong>
        </button>
        <button class="pill" type="button" data-community-comments-toggle>
            <span>Yorum</span><strong data-comment-count><?= e((string) $commentCount) ?></strong>
        </button>
        <button class="pill" type="button" data-community-share>Paylas</button>
    </footer>

    <section class="community-comments" data-community-comments hidden>
        <div class="community-comment-list" data-community-comment-list>
            <?php if ($commentCount > 0): ?><p class="muted">Yeni yorumlar burada gorunur; mevcut yorum sayisi <?= e((string) $commentCount) ?>.</p><?php endif; ?>
        </div>
        <?php if ($viewerId > 0): ?>
            <form class="community-comment-form" data-community-comment-form>
                <input type="hidden" name="_csrf" value="<?= e(\App\Core\Csrf::token()) ?>">
                <textarea name="content" rows="3" maxlength="3000" placeholder="Yorum yaz..."></textarea>
                <button class="pill primary" type="submit">Yorumla</button>
            </form>
        <?php else: ?>
            <button class="pill" type="button" data-auth-required data-auth-title="Giris gerekli" data-auth-message="Yorum yazmak icin giris yapmalisin.">Yorum icin giris yap</button>
        <?php endif; ?>
    </section>
</article>
