<?php
/**
 * Heritage Voice — partner-og.php
 * Social share proxy cho đối tác
 * URL: /partner-og.php?uuid=...
 */
require_once __DIR__ . '/api/config.php';
require_once __DIR__ . '/api/db.php';

$uuid = trim($_GET['uuid'] ?? '');
$base = 'https://tads.ctv.hanoi.vn';

// Default
$title  = 'Heritage Voice — Khám phá di sản Việt Nam';
$desc   = 'Ứng dụng du lịch di sản văn hóa Việt Nam với thuyết minh audio tự động.';
$image  = $base . '/assets/og-default.jpg';
$url    = $base . '/partner-detail.html?uuid=' . htmlspecialchars($uuid);

if ($uuid) {
    $stmt = db()->prepare("SELECT name, description, address, images FROM hv_partners WHERE uuid = :uuid AND status = 'active'");
    $stmt->execute([':uuid' => $uuid]);
    $p = $stmt->fetch();

    if ($p) {
        $title = htmlspecialchars($p['name']) . ' — Heritage Voice';
        $desc  = $p['description']
            ? htmlspecialchars(mb_substr($p['description'], 0, 160))
            : htmlspecialchars($p['address'] ?? '');
        $images = $p['images'] ? json_decode($p['images'], true) : [];
        if (!empty($images[0])) $image = htmlspecialchars($images[0]);
    }
}

// Bot detection — redirect user thật sang trang JS
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
$isCrawler = preg_match('/facebookexternalhit|Twitterbot|LinkedInBot|WhatsApp|Slackbot|TelegramBot|Zalobot|googlebot|bingbot/i', $ua);

if (!$isCrawler && $uuid) {
    header('Location: ' . $url);
    exit;
}
?>
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8"/>
<meta property="og:type"        content="website"/>
<meta property="og:url"         content="<?= $base.'/partner-og.php?uuid='.htmlspecialchars($uuid) ?>"/>
<meta property="og:title"       content="<?= $title ?>"/>
<meta property="og:description" content="<?= $desc ?>"/>
<meta property="og:image"       content="<?= $image ?>"/>
<meta property="og:image:type"  content="image/jpeg"/>
<meta property="og:site_name"   content="Heritage Voice"/>
<meta name="twitter:card"       content="summary_large_image"/>
<meta name="twitter:title"      content="<?= $title ?>"/>
<meta name="twitter:description"content="<?= $desc ?>"/>
<meta name="twitter:image"      content="<?= $image ?>"/>
<title><?= $title ?></title>
<meta http-equiv="refresh" content="0;url=<?= $url ?>"/>
</head>
<body>
<script>window.location.replace('<?= $url ?>');</script>
</body>
</html>
