<?php

declare(strict_types=1);

$dsn = 'mysql:host=localhost;dbname=inveetaire;charset=utf8mb4';
$user = 'root';
$pass = '';

try {
    $pdo = new PDO($dsn, $user, $pass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]);

    // 1. Database Value
    $stmt = $pdo->prepare("
        SELECT id, event_name, maps_embed_url 
        FROM wedding_events 
        WHERE maps_embed_url IS NOT NULL AND maps_embed_url != '' AND is_deleted = 0 
        ORDER BY id ASC LIMIT 1
    ");
    $stmt->execute();
    $dbRow = $stmt->fetch();
    $dbValue = $dbRow['maps_embed_url'] ?? null;

    // 2. PHP Variable Value
    $mEmbedUrl = $dbValue;

    // 3. Rendered HTML iframe src attribute (identity.php line 478)
    $renderedSrc = htmlspecialchars((string) $mEmbedUrl);

    echo "DB value: " . ($dbValue !== null ? $dbValue : 'NULL') . "\n";
    echo "PHP variable value: " . ($mEmbedUrl !== null ? $mEmbedUrl : 'NULL') . "\n";
    echo "Rendered HTML: <iframe src=\"" . $renderedSrc . "\" ...></iframe>\n";

    // 4. Browser Direct Test (cURL HTTP fetch)
    if (!empty($mEmbedUrl)) {
        $ch = curl_init($mEmbedUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
        curl_close($ch);

        if ($httpCode === 200 && strpos((string)$response, 'google') !== false) {
            echo "Browser test result: SUCCESS (HTTP 200 - Google Maps embed page loaded successfully)\n";
        } else {
            echo "Browser test result: HTTP " . $httpCode . " - Response length: " . strlen((string)$response) . " bytes\n";
        }
    } else {
        echo "Browser test result: N/A (maps_embed_url is empty)\n";
    }

} catch (Exception $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
}
