#!/bin/bash

# Fix TEC Community Events URLs on Staging
# Ensures proper page creation and permalink structure

set -e

STAGING_IP="146.190.76.204"
STAGING_USER="roodev"
STAGING_PATH="/home/974670.cloudwaysapps.com/uberrxmprk/public_html"

echo "=== Fixing TEC Community Events URLs ==="
echo ""

# Create the fix script
cat > /tmp/fix_tec_urls.sh << 'SCRIPT'
#!/bin/bash
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

echo "Checking TEC Community Events status..."
wp plugin list | grep tribe-events-community

echo ""
echo "Flushing rewrite rules..."
wp rewrite flush

echo ""
echo "Checking for Community Events pages..."

# Check if community events pages exist
echo "Looking for existing community pages..."
wp post list --post_type=page --meta_key=_wp_page_template --format=table | grep -i community || true

# Create/update the main community events page if needed
echo ""
echo "Ensuring Community Events main page exists..."
COMMUNITY_PAGE_ID=$(wp post list --post_type=page --title="Community" --format=ids)

if [ -z "$COMMUNITY_PAGE_ID" ]; then
    echo "Creating Community Events main page..."
    COMMUNITY_PAGE_ID=$(wp post create \
        --post_type=page \
        --post_title="Community" \
        --post_name="community" \
        --post_status=publish \
        --post_content="[tribe_community_events]" \
        --porcelain)
    echo "Created Community page with ID: $COMMUNITY_PAGE_ID"
else
    echo "Community page exists with ID: $COMMUNITY_PAGE_ID"
    # Ensure it has the shortcode
    wp post update $COMMUNITY_PAGE_ID --post_content="[tribe_community_events]"
fi

# Set the community events main page in TEC settings
echo ""
echo "Updating TEC Community Events settings..."
wp option update tribe_events_community_main_page $COMMUNITY_PAGE_ID

# Check/create the events page
echo ""
echo "Checking Events page..."
EVENTS_PAGE_ID=$(wp post list --post_type=page --title="Events" --format=ids)

if [ -z "$EVENTS_PAGE_ID" ]; then
    echo "Creating Events page..."
    EVENTS_PAGE_ID=$(wp post create \
        --post_type=page \
        --post_title="Events" \
        --post_name="events" \
        --post_status=publish \
        --post_content="" \
        --porcelain)
    echo "Created Events page with ID: $EVENTS_PAGE_ID"
else
    echo "Events page exists with ID: $EVENTS_PAGE_ID"
fi

# Set the events page in TEC settings
wp option update tribe_events_page $EVENTS_PAGE_ID

# Enable community events features
echo ""
echo "Enabling Community Events features..."
wp option patch update tribe_events_community '{"allowAnonymousSubmissions":"0","defaultStatus":"draft","blockRolesFromSubmissions":"","blockRolesList":"","allowUsersToEditSubmissions":"1","allowUsersToDeleteSubmissions":"1","trashItemsVsDelete":"1","emailAlertsEnabled":"1","emailAlertsList":""}' --format=json 2>/dev/null || true

# Create the community events subpages
echo ""
echo "Creating Community Events subpages..."

# Add Event page
ADD_PAGE_ID=$(wp post list --post_type=page --name="add" --post_parent=$COMMUNITY_PAGE_ID --format=ids)
if [ -z "$ADD_PAGE_ID" ]; then
    ADD_PAGE_ID=$(wp post create \
        --post_type=page \
        --post_title="Add Event" \
        --post_name="add" \
        --post_parent=$COMMUNITY_PAGE_ID \
        --post_status=publish \
        --post_content="[tribe_community_events view='edit']" \
        --porcelain)
    echo "Created Add Event page with ID: $ADD_PAGE_ID"
fi

# List Events page
LIST_PAGE_ID=$(wp post list --post_type=page --name="list" --post_parent=$COMMUNITY_PAGE_ID --format=ids)
if [ -z "$LIST_PAGE_ID" ]; then
    LIST_PAGE_ID=$(wp post create \
        --post_type=page \
        --post_title="My Events" \
        --post_name="list" \
        --post_parent=$COMMUNITY_PAGE_ID \
        --post_status=publish \
        --post_content="[tribe_community_events view='list']" \
        --porcelain)
    echo "Created My Events page with ID: $LIST_PAGE_ID"
fi

# Edit Event page (dynamic)
EDIT_PAGE_ID=$(wp post list --post_type=page --name="edit" --post_parent=$COMMUNITY_PAGE_ID --format=ids)
if [ -z "$EDIT_PAGE_ID" ]; then
    EDIT_PAGE_ID=$(wp post create \
        --post_type=page \
        --post_title="Edit Event" \
        --post_name="edit" \
        --post_parent=$COMMUNITY_PAGE_ID \
        --post_status=publish \
        --post_content="[tribe_community_events view='edit']" \
        --porcelain)
    echo "Created Edit Event page with ID: $EDIT_PAGE_ID"
fi

echo ""
echo "Flushing rewrite rules again..."
wp rewrite flush

echo ""
echo "Verifying URLs..."
echo "Main Community Page: https://upskill-staging.measurequick.com/community/"
echo "Add Event: https://upskill-staging.measurequick.com/community/add/"
echo "My Events: https://upskill-staging.measurequick.com/community/list/"
echo "Edit Event: https://upskill-staging.measurequick.com/community/edit/"

echo ""
echo "Creating test events with WP-CLI..."

# Ensure test users exist
TRAINER_ID=$(wp user get test_trainer@example.com --field=ID 2>/dev/null || wp user create test_trainer test_trainer@example.com --user_pass=TestTrainer123! --role=hvac_trainer --first_name=Test --last_name=Trainer --porcelain)

echo "Test Trainer ID: $TRAINER_ID"

# Create a simple test event
echo "Creating test event..."
TEST_EVENT_ID=$(wp post create \
    --post_type=tribe_events \
    --post_title="HVAC System Diagnostics Workshop" \
    --post_content="Learn advanced diagnostic techniques for modern HVAC systems." \
    --post_status=publish \
    --post_author=$TRAINER_ID \
    --porcelain)

wp post meta update $TEST_EVENT_ID _EventStartDate "2025-08-25 09:00:00"
wp post meta update $TEST_EVENT_ID _EventEndDate "2025-08-25 17:00:00"
wp post meta update $TEST_EVENT_ID _EventTimezone "America/Chicago"

echo "Created test event with ID: $TEST_EVENT_ID"

echo ""
echo "Listing all events..."
wp post list --post_type=tribe_events --fields=ID,post_title,post_status,post_author --format=table

echo ""
echo "✅ TEC Community Events URLs fixed!"
echo ""
echo "Test URLs:"
echo "1. Add Event: https://upskill-staging.measurequick.com/community/add/"
echo "2. My Events: https://upskill-staging.measurequick.com/community/list/"
echo "3. Edit Event: https://upskill-staging.measurequick.com/community/edit/{event_id}/"
echo ""
echo "Test Account:"
echo "Email: test_trainer@example.com"
echo "Password: TestTrainer123!"

SCRIPT

echo "Uploading fix script to staging..."
scp -q /tmp/fix_tec_urls.sh $STAGING_USER@$STAGING_IP:/tmp/ 2>/dev/null || {
    echo "Note: Manual SSH password required"
}

echo "Executing fix script on staging..."
ssh $STAGING_USER@$STAGING_IP "bash /tmp/fix_tec_urls.sh" 2>/dev/null || {
    echo ""
    echo "=== Manual Execution Required ==="
    echo "SSH to staging and run:"
    echo "bash /tmp/fix_tec_urls.sh"
}

# Cleanup
rm /tmp/fix_tec_urls.sh