<?php

declare(strict_types=1);

/**
 * 側欄：站內公告／文章（首頁與側欄版共用）。
 *
 * @var list<array<string,mixed>> $blogHomeAnnouncements
 * @var string $fateBlogNavLabel
 * @var bool $fateBlogNavVisible
 * @var array{id:int,username:string,display_name:string,avatar_rel:string}|null $fatePublicMember
 * @var PDO|null $pdo
 */

if (empty($fateBlogNavVisible)) {
    return;
}

require_once dirname(__DIR__, 3) . '/includes/fate_blog.php';

if (!isset($blogHomeAnnouncements) && isset($pdo) && $pdo instanceof PDO && fate_blog_enabled()) {
    fate_blog_ensure_tables($pdo);
    $blogHomeAnnouncements = fate_blog_announcements_for_home($pdo);
}

$annLabel = $fateBlogNavLabel ?? fate_blog_nav_label();
$announcements = is_array($blogHomeAnnouncements ?? null) ? $blogHomeAnnouncements : [];
$hasAnnouncements = $announcements !== [];
$cardTitle = $hasAnnouncements ? '站內公告' : $annLabel;

$blogSidebarCanManage = false;
if (isset($pdo) && $pdo instanceof PDO && ($fatePublicMember ?? null) !== null) {
    require_once dirname(__DIR__, 3) . '/includes/fate_public_member_auth.php';
    fate_public_member_ensure_admin_flag($pdo);
    $blogSidebarCanManage = fate_blog_viewer_can_manage($pdo);
}
?>
<div class="sidebar-card sidebar-card--blog">
    <p class="sidebar-title"><?= htmlspecialchars($cardTitle, ENT_QUOTES, 'UTF-8') ?></p>

    <?php if ($hasAnnouncements): ?>
        <ul class="sidebar-announce-list">
            <?php foreach ($announcements as $post): ?>
                <?php
                $pid = (int) ($post['id'] ?? 0);
                if ($pid <= 0) {
                    continue;
                }
                $title = (string) ($post['title'] ?? '');
                $dateRaw = (string) ($post['published_at'] ?? $post['created_at'] ?? '');
                $dateShort = $dateRaw !== '' ? substr($dateRaw, 5, 5) : '';
                ?>
                <li class="sidebar-announce-list__item">
                    <a class="sidebar-announce-list__link" href="<?= htmlspecialchars(fate_blog_post_url($pid), ENT_QUOTES, 'UTF-8') ?>">
                        <span class="sidebar-announce-list__title"><?= htmlspecialchars($title, ENT_QUOTES, 'UTF-8') ?></span>
                        <?php if ($dateShort !== ''): ?>
                            <time class="sidebar-announce-list__date" datetime="<?= htmlspecialchars(substr($dateRaw, 0, 10), ENT_QUOTES, 'UTF-8') ?>"><?= htmlspecialchars($dateShort, ENT_QUOTES, 'UTF-8') ?></time>
                        <?php endif; ?>
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php else: ?>
        <p class="sidebar-announce-empty">目前沒有公告。</p>
    <?php endif; ?>

    <div class="sidebar-actions sidebar-actions--blog">
        <a class="sidebar-btn" href="blog.php"><?= $hasAnnouncements ? '全部文章' : '文章列表' ?></a>
        <?php if ($blogSidebarCanManage): ?>
            <a class="sidebar-btn sidebar-btn--ghost" href="blog.php?compose=1">發文</a>
            <a class="sidebar-btn sidebar-btn--ghost" href="blog.php?manage=settings">管理</a>
        <?php endif; ?>
    </div>
</div>
