<?php

declare(strict_types=1);

header('Content-Type: text/html; charset=UTF-8');

require_once __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'app.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_public_session.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_install.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_public_member_auth.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_template.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_page_counter.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_blog.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_seo.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_admin_nav_visibility.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_plugins.php';

fate_public_session_start();
fate_install_maybe_redirect();

$fatePublicMember = fate_public_member_current();
$frontendDb = fate_frontend_db_bootstrap();
$pdo = $frontendDb['pdo'];
$dbError = $frontendDb['dbError'];

if ($fatePublicMember !== null && $pdo instanceof PDO) {
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_member_profile.php';
    fate_public_member_ensure_admin_flag($pdo);
    fate_member_profile_hydrate_session($pdo, (int) $fatePublicMember['id']);
    $fatePublicMember = fate_public_member_current();
}

if (!fate_blog_enabled()) {
    http_response_code(404);
    echo '<!DOCTYPE html><html lang="zh-Hant"><head><meta charset="UTF-8"><title>找不到頁面</title></head><body><p>此功能尚未啟用。</p><p><a href="./">返回首頁</a></p></body></html>';
    exit;
}

$postId = isset($_GET['id']) ? max(0, (int) $_GET['id']) : 0;
if ($postId <= 0) {
    header('Location: blog.php', true, 302);
    exit;
}

$fateAdminNavVisible = fate_admin_nav_visible_for_frontend();
$fateForumNavVisible = fate_store_forum_plugin_active();
$fateBlogNavVisible = true;
$fateBlogNavLabel = fate_blog_nav_label();

$blogCanManage = $pdo instanceof PDO && fate_blog_viewer_can_manage($pdo);
$editMode = isset($_GET['edit']) && $_GET['edit'] === '1' && $blogCanManage;
$csrf = fate_public_member_csrf_token();

$msgOk = isset($_SESSION['fate_blog_flash_ok']) ? (string) $_SESSION['fate_blog_flash_ok'] : '';
$msgBad = isset($_SESSION['fate_blog_flash_bad']) ? (string) $_SESSION['fate_blog_flash_bad'] : '';
unset($_SESSION['fate_blog_flash_ok'], $_SESSION['fate_blog_flash_bad']);

$blogPost = $pdo instanceof PDO ? fate_blog_post_get($pdo, $postId, $blogCanManage) : null;
if ($blogPost === null) {
    http_response_code(404);
    echo '<!DOCTYPE html><html lang="zh-Hant"><head><meta charset="UTF-8"><title>找不到文章</title></head><body><p>找不到文章。</p><p><a href="blog.php">返回列表</a></p></body></html>';
    exit;
}

$blogCategories = $pdo instanceof PDO ? fate_blog_categories_list($pdo, true) : [];

$fatePageStatsKey = 'blog.post.' . $postId;
$fatePageTitle = (string) ($blogPost['title'] ?? '文章');
$fateSeoContext = fate_seo_simple_page_context(
    $fatePageTitle,
    fate_blog_excerpt_from_body((string) ($blogPost['body_html'] ?? ''), 120)
);
$fatePageStatsFooter = fate_page_stats_for_footer($pdo, $fatePageStatsKey, fate_page_stats_member_id($fatePublicMember), $fatePageTitle);
$fatePageHitCount = $fatePageStatsFooter['fatePageHitCount'];
$fatePageStatsDbOk = $fatePageStatsFooter['fatePageStatsDbOk'];

$sidebarLayout = fate_blog_sidebar_layout_vars($pdo instanceof PDO ? $pdo : null);

fate_render_blog_post(array_merge(compact(
    'pdo',
    'dbError',
    'fatePublicMember',
    'fateAdminNavVisible',
    'fateForumNavVisible',
    'fateBlogNavVisible',
    'fateBlogNavLabel',
    'blogCanManage',
    'editMode',
    'csrf',
    'msgOk',
    'msgBad',
    'blogPost',
    'blogCategories',
    'postId',
    'fatePageHitCount',
    'fatePageStatsDbOk',
    'fatePageTitle',
    'fateSeoContext'
), $sidebarLayout));
