<?php
declare(strict_types=1);

$app = app();
$pdo = $app->pdo();
$userId = $_SESSION['user_id'] ?? null;
$error = '';
$threadId = (int)($_GET['id'] ?? 0);

$communityCfg = require __DIR__ . '/../../../config/community.php';
$community = $pdo ? new \App\Community($pdo, $communityCfg) : null;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'reply') {
    if (!$userId) {
        $error = 'Bitte einloggen, um zu antworten.';
    } elseif ($community) {
        $body = trim((string)($_POST['body'] ?? ''));
        if ($body === '') {
            $error = 'Antwort darf nicht leer sein.';
        } else {
            $community->createPost((int)$userId, $threadId, $body);
            redirect('/community_thread?id=' . $threadId);
        }
    }
}

$thread = $community ? $community->getThread($threadId) : null;
$posts = $community ? $community->listPosts($threadId) : [];

if (!$thread) {
    http_response_code(404);
    echo "<p>Thread nicht gefunden.</p>";
    return;
}
?>
<main class="section">
  <div class="container">
    <p class="eyebrow">Community</p>
    <h1><?= htmlspecialchars($thread['title'], ENT_QUOTES) ?></h1>
    <p class="muted">Von <?= htmlspecialchars($thread['display_name'] ?: 'Mitglied', ENT_QUOTES) ?> · <?= htmlspecialchars($thread['created_at'], ENT_QUOTES) ?></p>
    <article class="card" style="margin-top:12px;">
      <div class="event__body">
        <p><?= nl2br(htmlspecialchars($thread['body'], ENT_QUOTES)) ?></p>
      </div>
    </article>

    <h3 style="margin-top:16px;">Antworten (<?= count($posts) ?>)</h3>
    <div class="stack gap-12" style="margin-top:10px;">
      <?php foreach ($posts as $p): ?>
        <article class="card">
          <div class="event__body">
            <div class="event__meta">
              <span><?= htmlspecialchars($p['created_at'], ENT_QUOTES) ?></span>
              <span><?= htmlspecialchars($p['display_name'] ?: 'Mitglied', ENT_QUOTES) ?></span>
            </div>
            <p><?= nl2br(htmlspecialchars($p['body'], ENT_QUOTES)) ?></p>
          </div>
        </article>
      <?php endforeach; ?>
      <?php if (!$posts): ?>
        <p class="muted">Noch keine Antworten.</p>
      <?php endif; ?>
    </div>

    <?php if ($error): ?>
      <div class="toast-bar" style="margin-top:12px; border-color:#f87171; color:#991b1b;"><?= htmlspecialchars($error, ENT_QUOTES) ?></div>
    <?php endif; ?>

    <?php if ($userId): ?>
      <form method="post" class="stack gap-12 card" style="margin-top:14px; padding:16px;">
        <input type="hidden" name="action" value="reply">
        <div class="stack gap-6">
          <label class="label" for="replyBody">Antwort</label>
          <textarea id="replyBody" name="body" class="textarea" rows="4" required></textarea>
        </div>
        <button class="btn" type="submit">Antwort senden</button>
      </form>
    <?php else: ?>
      <p class="muted" style="margin-top:12px;">Bitte <a href="/login">einloggen</a>, um zu antworten.</p>
    <?php endif; ?>
  </div>
</main>
