<?php

declare(strict_types=1);

define('ROOT_PATH', __DIR__ . '/..');

require_once ROOT_PATH . '/app/Core/Autoloader.php';
\App\Core\Autoloader::register();
restore_error_handler();
restore_exception_handler();
require_once ROOT_PATH . '/app/helpers/env.php';
require_once ROOT_PATH . '/app/helpers/config.php';
require_once ROOT_PATH . '/app/helpers/url.php';

use App\Modules\Invitation\InvitationValidator;

echo "=== GOOGLE MAPS UX REFACTOR (PHASE 2) VERIFICATION ===\n\n";

$validator = new InvitationValidator();
$wsId = 83;

$basePayload = [
    'groom_display_name' => 'Ahmad',
    'bride_display_name' => 'Fatimah',
    'love_story'         => 'Story',
    'theme_version_id'   => 1,
    'invitation_status'  => 'published',
    'events'             => [
        [
            'id'               => 0,
            'event_name'       => 'Holy Matrimony',
            'event_date'       => '2026-09-12',
            'event_time'       => '10:00',
            'invitation_times' => ['10:00'],
            'venue_name'       => 'Grand Ballroom',
            'venue_address'    => 'Main Address',
            'maps_url'         => 'https://maps.app.goo.gl/sampleShareUrl',
            'maps_embed_code'  => 'https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3966.521260322283!2d106.819561314769!3d-6.194741395515383!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x2e69f421f1c7d2eb%3A0xb3dfc067d5ff789!2sGrand%20Hyatt%20Jakarta!5e0!3m2!1sen!2sid!4v1620000000000!5m2!1sen!2sid',
        ]
    ]
];

try {
    // TEST 1: Valid Payload
    $errors1 = $validator->validate($basePayload, $wsId);
    if (empty($errors1)) {
        echo "[PASS] Valid payload with Share URL in maps_url and Embed URL in maps_embed_code passed with 0 errors!\n";
    } else {
        echo "[FAIL] Valid payload rejected: " . json_encode($errors1) . "\n";
    }

    // TEST 2: Valid Full IFrame HTML Payload
    $iframePayload = $basePayload;
    $iframePayload['events'][0]['maps_embed_code'] = '<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3966.521260322283!2d106.819561314769!3d-6.194741395515383!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x2e69f421f1c7d2eb%3A0xb3dfc067d5ff789!2sGrand%20Hyatt%20Jakarta!5e0!3m2!1sen!2sid!4v1620000000000!5m2!1sen!2sid" width="600" height="450" style="border:0;" allowfullscreen="" loading="lazy"></iframe>';
    $errorsIframe = $validator->validate($iframePayload, $wsId);
    if (empty($errorsIframe)) {
        echo "[PASS] Full <iframe> HTML code in maps_embed_code passed with 0 errors!\n";
    } else {
        echo "[FAIL] Full <iframe> HTML code rejected: " . json_encode($errorsIframe) . "\n";
    }

    // TEST 3: Case A — Share URL pasted into Embed URL field
    $caseAPayload = $basePayload;
    $caseAPayload['events'][0]['maps_embed_code'] = 'https://maps.app.goo.gl/wrongLocation';
    $errorsCaseA = $validator->validate($caseAPayload, $wsId);
    if (!empty($errorsCaseA['event_0_maps_embed_code'])) {
        echo "[PASS] Case A correctly caught Share URL in Embed field:\n  '{$errorsCaseA['event_0_maps_embed_code']}'\n";
    } else {
        echo "[FAIL] Case A validation failed to catch Share URL in Embed field.\n";
    }

    // TEST 4: Case B — Embed URL pasted into Open URL field
    $caseBPayload = $basePayload;
    $caseBPayload['events'][0]['maps_url'] = 'https://www.google.com/maps/embed?pb=!1m18!1m12!...';
    $errorsCaseB = $validator->validate($caseBPayload, $wsId);
    if (!empty($errorsCaseB['event_0_maps_url'])) {
        echo "[PASS] Case B correctly caught Embed URL in Open URL field:\n  '{$errorsCaseB['event_0_maps_url']}'\n";
    } else {
        echo "[FAIL] Case B validation failed to catch Embed URL in Open field.\n";
    }
} catch (\Throwable $e) {
    echo "[FAIL] Exception during validation test: " . $e->getMessage() . "\n" . $e->getTraceAsString() . "\n";
}

echo "\nGoogle Maps Phase 2 Verification Complete.\n";
