#!/bin/bash

# Investigate MapGeo plugin usage
# Usage: ./scripts/investigate-mapgeo.sh

source .env

echo "=== Investigating MapGeo Plugin ==="

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

echo "=== Checking if map shortcode exists ==="
wp eval '
global $shortcode_tags;
$map_shortcodes = [];
foreach ($shortcode_tags as $tag => $callback) {
    if (strpos(strtolower($tag), "map") !== false || strpos(strtolower($tag), "igm") !== false) {
        $map_shortcodes[] = $tag;
    }
}
echo "Map-related shortcodes found: " . implode(", ", $map_shortcodes) . "\n";
'

echo -e "\n=== Checking for existing maps in database ==="
wp db query "SELECT ID, post_title FROM wp_posts WHERE post_type = 'igmap' OR post_type = 'maps' OR post_type = 'interactive_map' LIMIT 10;" 2>/dev/null || echo "No maps found in database"

echo -e "\n=== Checking post types ==="
wp eval '
$post_types = get_post_types(["public" => true], "names");
foreach ($post_types as $post_type) {
    if (strpos(strtolower($post_type), "map") !== false || strpos(strtolower($post_type), "igm") !== false) {
        echo "Map-related post type: " . $post_type . "\n";
    }
}
'

echo -e "\n=== Checking map with ID 5872 ==="
wp eval '
$post = get_post(5872);
if ($post) {
    echo "Post ID 5872 exists:\n";
    echo "  Type: " . $post->post_type . "\n";
    echo "  Title: " . $post->post_title . "\n";
    echo "  Status: " . $post->post_status . "\n";
} else {
    echo "Post ID 5872 does not exist\n";
}
'

echo -e "\n=== Testing display-map shortcode ==="
wp eval '
if (shortcode_exists("display-map")) {
    echo "display-map shortcode is registered\n";
    $output = do_shortcode("[display-map id=\"5872\"]");
    echo "Shortcode output length: " . strlen($output) . " characters\n";
    if (strlen($output) > 0 && strlen($output) < 500) {
        echo "Output: " . $output . "\n";
    }
} else {
    echo "display-map shortcode is NOT registered\n";
}
'

echo -e "\n=== Looking for alternative map shortcodes ==="
wp eval '
// Common map shortcode patterns
$test_shortcodes = ["igm", "interactive-map", "interactive_map", "igmap", "maps"];
foreach ($test_shortcodes as $shortcode) {
    if (shortcode_exists($shortcode)) {
        echo "Found shortcode: [$shortcode]\n";
    }
}
'

echo -e "\n=== Checking MapGeo plugin files ==="
ls -la wp-content/plugins/interactive-geo-maps-premium/src/ 2>/dev/null | head -n 10 || echo "Plugin directory not accessible"

ENDSSH

echo "=== Investigation Complete ==="