<?php
require_once '../config/db.php';

$type = $_GET['type'] === 'found' ? 'found' : 'lost';
$id   = (int)($_GET['id'] ?? 0);

$protocol    = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$site_origin = $protocol . '://' . $_SERVER['HTTP_HOST'];
$browse_url  = $site_origin . BASE_URL . '/public/browse.php?view=' . $type . '&id=' . $id;
$share_url   = $site_origin . BASE_URL . '/public/share.php?type=' . $type . '&id=' . $id;

// Defaults
$og_title       = 'RELINK — Lost & Found | Cauayan City Hall';
$og_description = 'Browse lost and found items reported at Cauayan City Hall.';
$og_image       = $site_origin . BASE_URL . '/cyn_logo.png';

if ($id > 0) {
    $tbl  = $type === 'lost' ? 'lost_items' : 'found_items';
    $row  = $conn->query("SELECT * FROM $tbl WHERE id=$id")->fetch_assoc();
    if ($row) {
        $label          = $type === 'lost' ? 'LOST ITEM' : 'FOUND ITEM';
        $og_title       = $label . ': ' . $row['item_name'] . ' — RELINK Lost & Found';
        $date_col       = $type === 'lost' ? 'date_lost' : 'date_found';
        $date_str       = !empty($row[$date_col]) ? date('F j, Y', strtotime($row[$date_col])) : '';
        $og_description = 'Category: ' . ($row['category'] ?? '') .
                          ' | Location: ' . ($row['location'] ?? '') .
                          ($date_str ? ' | Date: ' . $date_str : '') .
                          ' | Reported at Cauayan City Hall. Help reunite this item with its owner!';
        if (!empty($row['image_path'])) {
            $og_image = $site_origin . BASE_URL . '/' . ltrim($row['image_path'], '/');
        }
    }
}

// Detect Facebook/bot crawler vs real user
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
$is_bot = preg_match('/facebookexternalhit|Facebot|Twitterbot|LinkedInBot|WhatsApp|Slackbot|TelegramBot|curl|wget|python|bot|crawler|spider/i', $ua);

// Real users get redirected to the actual browse page
if (!$is_bot) {
    header('Location: ' . $browse_url);
    exit;
}
?><!DOCTYPE html>
<html prefix="og: https://ogp.me/ns#">
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($og_title) ?></title>
<meta property="og:type"        content="website">
<meta property="og:url"         content="<?= htmlspecialchars($share_url) ?>">
<meta property="og:title"       content="<?= htmlspecialchars($og_title) ?>">
<meta property="og:description" content="<?= htmlspecialchars($og_description) ?>">
<meta property="og:image"       content="<?= htmlspecialchars($og_image) ?>">
<meta property="og:image:width"  content="1200">
<meta property="og:image:height" content="630">
<meta property="og:site_name"   content="RELINK — Cauayan City Hall Lost &amp; Found">
<meta name="twitter:card"        content="summary_large_image">
<meta name="twitter:title"       content="<?= htmlspecialchars($og_title) ?>">
<meta name="twitter:description" content="<?= htmlspecialchars($og_description) ?>">
<meta name="twitter:image"       content="<?= htmlspecialchars($og_image) ?>">
</head>
<body></body>
</html>
