<?php
require_once __DIR__ . '/config.php';
$extra_head = '<link rel="stylesheet" href="/styles/blog.css">';

$slug = $_GET['slug'] ?? '';
if (!$slug) {
    http_response_code(404);
    echo "<h1>404 Not Found</h1>";
    exit;
}

try {
    $stmt = $pdo->prepare("
        SELECT posts.*, admin_users.username AS author_name 
        FROM posts 
        LEFT JOIN admin_users ON posts.created_by = admin_users.id 
        WHERE posts.slug = ?
    ");
    $stmt->execute([$slug]);
    $post = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$post) {
        http_response_code(404);
        echo "<h1>Post not found</h1>";
        exit;
    }
} catch (PDOException $e) {
    die("Database error: " . htmlspecialchars($e->getMessage()));
}

$authorUsername = !empty($post['author_name']) ? $post['author_name'] : 'Admin';
$featuredImage = !empty($post['image_path']) ? '/' . $post['image_path'] : '/assets/hero.jpg';

$pageTitle = "ThrillSense | " . htmlspecialchars($post['title']);

$plainText = trim(strip_tags($post['content']));
$metaDescription = substr($plainText, 0, 155);
$metaDescription = preg_replace('/\s+\S*$/', '', $metaDescription);

$canonicalUrl = "https://www.thrillsense.com/tickets-and-offers/" . urlencode($post['slug']);

$wordCount = str_word_count($plainText);
$readingTime = ceil($wordCount / 200);

$extra_head .= "
<meta name='description' content='" . htmlspecialchars($metaDescription) . "'>
<link rel='canonical' href='{$canonicalUrl}'>

<meta property='og:title' content='" . htmlspecialchars($post['title']) . "'>
<meta property='og:description' content='" . htmlspecialchars($metaDescription) . "'>
<meta property='og:image' content='https://www.thrillsense.com{$featuredImage}'>
<meta property='og:url' content='{$canonicalUrl}'>
<meta property='og:type' content='article'>

<meta name='twitter:card' content='summary_large_image'>
<meta name='twitter:title' content='" . htmlspecialchars($post['title']) . "'>
<meta name='twitter:description' content='" . htmlspecialchars($metaDescription) . "'>
<meta name='twitter:image' content='https://www.thrillsense.com{$featuredImage}'>
";

$structuredData = [
    "@context" => "https://schema.org",
    "@type" => "Article",
    "headline" => $post['title'],
    "description" => $metaDescription,
    "image" => "https://www.thrillsense.com" . $featuredImage,
    "author" => [
        "@type" => "Person",
        "name" => $authorUsername
    ],
    "publisher" => [
        "@type" => "Organization",
        "name" => "ThrillSense",
        "logo" => [
            "@type" => "ImageObject",
            "url" => "https://www.thrillsense.com/assets/logo.png"
        ]
    ],
    "datePublished" => date('c', strtotime($post['created_at'])),
    "dateModified" => date('c', strtotime($post['updated_at'] ?? $post['created_at']))
];

$extra_head .= "\n<script type=\"application/ld+json\">" . json_encode($structuredData, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "</script>";

require_once __DIR__ . '/includes/header.php';
?>

<main>

  <!-- HERO -->
  <section style="background:#262541; padding-top:80px; text-align:center;">

  <img 
      src="<?= htmlspecialchars($featuredImage) ?>" 
      alt="<?= htmlspecialchars($post['title']) ?>" 
      style="max-width:100px; width:100%; border-radius:10px;"
    />

    <!-- Title -->
    <h1 style="color:#fff !important;">
      <?= htmlspecialchars($post['title']) ?> Tickets & Offers
    </h1>

    <!-- Divider -->
    <div style="width:60px; height:3px; background:#ff6a00; margin:10px auto 20px auto;"></div>

    <!-- Meta -->
    <p style="font-size:0.9rem; color:#ccc; margin-bottom:30px;">
      <?= date('F j, Y', strtotime($post['created_at'])) ?> 
      • <?= $readingTime ?> min read 
      • By <?= htmlspecialchars($authorUsername) ?>
    </p>

  

  </section>

  <!-- CONTENT -->
  <section class="container py-5">
    <article class="post-content">
      <?= $post['content'] ?>
    </article>
  </section>

</main>

<?php include 'includes/footer.php'; ?>