#!/bin/bash

# Setup TEC integration pages on staging
source .env

echo "=== Setting up TEC Integration Pages ==="

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

echo "Creating TEC integration pages..."

# Create parent events page under trainer
wp post create --post_type=page --post_title="Events" --post_name="events" --post_status="publish" --post_parent=$(wp post list --post_type=page --name=trainer --field=ID) --porcelain 2>/dev/null || echo "Events page may already exist"

# Get the events page ID
EVENTS_ID=$(wp post list --post_type=page --pagename=trainer/events --field=ID)
echo "Events page ID: $EVENTS_ID"

if [ ! -z "$EVENTS_ID" ]; then
    # Create sub-pages with proper templates
    
    # Create Event page
    CREATE_ID=$(wp post create --post_type=page --post_title="Create Event" --post_name="create" --post_status="publish" --post_parent=$EVENTS_ID --porcelain 2>/dev/null || wp post list --post_type=page --pagename=trainer/events/create --field=ID)
    if [ ! -z "$CREATE_ID" ]; then
        wp post meta update $CREATE_ID _wp_page_template "templates/page-tec-create-event.php"
        echo "✅ Create Event page setup with template"
    fi
    
    # Edit Event page  
    EDIT_ID=$(wp post create --post_type=page --post_title="Edit Event" --post_name="edit" --post_status="publish" --post_parent=$EVENTS_ID --porcelain 2>/dev/null || wp post list --post_type=page --pagename=trainer/events/edit --field=ID)
    if [ ! -z "$EDIT_ID" ]; then
        wp post meta update $EDIT_ID _wp_page_template "templates/page-tec-edit-event.php"
        echo "✅ Edit Event page setup with template"
    fi
    
    # My Events page
    MY_EVENTS_ID=$(wp post create --post_type=page --post_title="My Events" --post_name="my-events" --post_status="publish" --post_parent=$EVENTS_ID --porcelain 2>/dev/null || wp post list --post_type=page --pagename=trainer/events/my-events --field=ID)
    if [ ! -z "$MY_EVENTS_ID" ]; then
        wp post meta update $MY_EVENTS_ID _wp_page_template "templates/page-tec-my-events.php"
        echo "✅ My Events page setup with template"
    fi
    
    # Manage Events page
    MANAGE_ID=$(wp post create --post_type=page --post_title="Manage Events" --post_name="manage" --post_status="publish" --post_parent=$EVENTS_ID --porcelain 2>/dev/null || wp post list --post_type=page --pagename=trainer/events/manage --field=ID)
    if [ ! -z "$MANAGE_ID" ]; then
        wp post meta update $MANAGE_ID _wp_page_template "templates/page-manage-event-integrated.php"
        echo "✅ Manage Events page setup with template"
    fi
fi

# Update the old manage event page to redirect
OLD_MANAGE_ID=$(wp post list --post_type=page --pagename=trainer/event/manage --field=ID)
if [ ! -z "$OLD_MANAGE_ID" ]; then
    wp post meta update $OLD_MANAGE_ID _wp_page_template "default"
    echo "✅ Updated old manage page"
fi

# Flush rewrite rules
wp rewrite flush
echo "✅ Rewrite rules flushed"

echo ""
echo "Pages created/updated. Verifying..."
echo ""

# List all trainer event pages
echo "Trainer Event Pages:"
wp post list --post_type=page --post_parent=$EVENTS_ID --fields=ID,post_title,post_name,post_status

echo ""
echo "Done!"
REMOTE_COMMANDS

echo ""
echo "=== TEC Integration Pages Setup Complete ==="
echo ""
echo "New URLs:"
echo "- Create Event: https://upskill-staging.measurequick.com/trainer/events/create/"
echo "- Edit Event: https://upskill-staging.measurequick.com/trainer/events/edit/{id}/"
echo "- My Events: https://upskill-staging.measurequick.com/trainer/events/my-events/"
echo "- Manage Events: https://upskill-staging.measurequick.com/trainer/events/manage/"