#!/bin/bash

# Analyze MapGeo data structure
# Usage: ./scripts/analyze-mapgeo-data.sh

source .env

echo "=== Analyzing MapGeo Data Structure ==="

ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" << 'ENDSSH'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

echo "=== Getting map metadata for ID 5872 ==="
wp eval '
$map_id = 5872;
$meta = get_post_meta($map_id);

echo "Post meta keys for map $map_id:\n";
foreach ($meta as $key => $values) {
    if (strpos($key, "_") !== 0) { // Skip private meta
        echo "  $key: ";
        $value = $values[0];
        if (is_serialized($value)) {
            $unserialized = unserialize($value);
            if (is_array($unserialized)) {
                echo "array with " . count($unserialized) . " items\n";
                if (isset($unserialized["markers"])) {
                    echo "    - Has markers: " . count($unserialized["markers"]) . "\n";
                }
                if (isset($unserialized["regions"])) {
                    echo "    - Has regions: " . count($unserialized["regions"]) . "\n";
                }
            } else {
                echo gettype($unserialized) . "\n";
            }
        } else {
            echo substr($value, 0, 100) . (strlen($value) > 100 ? "..." : "") . "\n";
        }
    }
}
'

echo -e "\n=== Checking igm_maps option ==="
wp eval '
$maps_data = get_option("igm_maps");
if ($maps_data) {
    echo "igm_maps option exists\n";
    if (is_array($maps_data)) {
        echo "Contains " . count($maps_data) . " maps\n";
        if (isset($maps_data[5872])) {
            echo "Map 5872 data found in option\n";
            $map = $maps_data[5872];
            if (is_array($map)) {
                echo "Map data keys: " . implode(", ", array_keys($map)) . "\n";
            }
        }
    }
} else {
    echo "No igm_maps option found\n";
}
'

echo -e "\n=== Testing igm_add_meta filter directly ==="
wp eval '
// Simulate what the plugin does
$map_id = 5872;
$meta = get_post_meta($map_id, "map_info", true);

if ($meta) {
    echo "Original meta structure:\n";
    echo "  Type: " . gettype($meta) . "\n";
    if (is_array($meta)) {
        echo "  Keys: " . implode(", ", array_keys($meta)) . "\n";
        if (isset($meta["id"])) {
            echo "  Map ID in meta: " . $meta["id"] . "\n";
        }
    }
    
    // Apply the filter as the plugin would
    echo "\nApplying igm_add_meta filter...\n";
    $filtered = apply_filters("igm_add_meta", $meta);
    
    if ($filtered !== $meta) {
        echo "Meta was modified by filters\n";
        if (is_array($filtered)) {
            echo "  New keys: " . implode(", ", array_keys($filtered)) . "\n";
        }
    } else {
        echo "Meta unchanged by filters\n";
    }
} else {
    echo "No map_info meta found for map $map_id\n";
}
'

echo -e "\n=== Checking how maps are rendered ==="
wp eval '
// Check what happens when we render a map
echo "Attempting to render map 5872...\n";
$shortcode_output = do_shortcode("[display-map id=\"5872\"]");
if (strpos($shortcode_output, "error") !== false || strpos($shortcode_output, "Error") !== false) {
    echo "Error in shortcode output: " . strip_tags($shortcode_output) . "\n";
} else {
    echo "Shortcode rendered successfully\n";
    // Check if it contains expected elements
    if (strpos($shortcode_output, "igm-map") !== false) {
        echo "  Contains igm-map element\n";
    }
    if (strpos($shortcode_output, "data-map-id") !== false) {
        echo "  Contains data-map-id attribute\n";
    }
}
'

ENDSSH

echo "=== Analysis Complete ==="