#!/bin/bash

# Fix event occurrences

# Get absolute path to this script's directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Navigate to wordpress-dev directory
cd "$(dirname "$SCRIPT_DIR")" || exit 1

# Load environment variables
ENV_FILE=".env"
if [ ! -f "$ENV_FILE" ]; then
    echo "Error: .env file not found at: $ENV_FILE"
    exit 1
fi

source "$ENV_FILE"

echo "=== Fixing Event Occurrences ==="
echo "Remote host: $UPSKILL_STAGING_IP"
echo "==============================="

# Fix occurrences
sshpass -p "${UPSKILL_STAGING_PASS}" ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" <<'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

# Create PHP script to fix occurrences
cat > fix-occurrences.php << 'PHP'
<?php
require_once 'wp-load.php';

echo "Creating occurrences for our events...\n";

// For each of our events
$event_ids = array(5482, 5483, 5484, 5485, 5486);

foreach ($event_ids as $event_id) {
    echo "Processing event $event_id...\n";
    
    // Get event data
    $start = get_post_meta($event_id, '_EventStartDate', true);
    $end = get_post_meta($event_id, '_EventEndDate', true);
    
    if ($start && $end) {
        // Check if occurrence already exists
        global $wpdb;
        $existing = $wpdb->get_var($wpdb->prepare(
            "SELECT occurrence_id FROM {$wpdb->prefix}tec_occurrences WHERE post_id = %d",
            $event_id
        ));
        
        if (!$existing) {
            // Create occurrence
            $wpdb->insert(
                $wpdb->prefix . 'tec_occurrences',
                array(
                    'post_id' => $event_id,
                    'start_date' => $start,
                    'start_date_utc' => $start,
                    'end_date' => $end,
                    'end_date_utc' => $end,
                    'duration' => strtotime($end) - strtotime($start),
                    'hash' => md5($event_id . $start . $end)
                ),
                array('%d', '%s', '%s', '%s', '%s', '%d', '%s')
            );
            echo "Created occurrence for event $event_id\n";
        } else {
            echo "Occurrence already exists for event $event_id\n";
        }
    }
}

echo "\nTesting query again...\n";
$args = array(
    'post_type' => 'tribe_events',
    'author' => 17,
    'posts_per_page' => -1,
    'post_status' => 'any'
);
$query = new WP_Query($args);
echo "Found events: " . $query->found_posts . "\n";

// Test dashboard data
echo "\nTesting dashboard data...\n";
require_once '/home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/includes/class-hvac-dashboard-data.php';
$dashboard = new HVAC_Dashboard_Data(17);
echo "Total events: " . $dashboard->get_total_events_count() . "\n";
echo "Upcoming events: " . $dashboard->get_upcoming_events_count() . "\n";
echo "Past events: " . $dashboard->get_past_events_count() . "\n";

PHP

php fix-occurrences.php
rm fix-occurrences.php
EOF

echo "Fix completed!"