#!/bin/bash
set -e

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Load environment variables
if [ -f .env ]; then
    export $(cat .env | sed 's/#.*//g' | xargs)
fi

echo -e "${YELLOW}Fixing missing trainer pages on staging server...${NC}"

# Create pages via SSH
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

echo "Checking and fixing trainer pages..."

# Get parent page IDs
trainer_parent_id=$(wp post list --post_type=page --name="trainer" --format=ids | head -n1)
echo "Trainer parent ID: $trainer_parent_id"

# Function to create page properly
create_page() {
    local parent_id="$1"
    local slug="$2"
    local title="$3"
    local template="$4"
    
    echo "Creating page: $title (slug: $slug, template: $template)"
    
    # Create the page with proper parameters
    page_id=$(wp post create \
        --post_type=page \
        --post_status=publish \
        --post_title="$title" \
        --post_name="$slug" \
        --post_parent="$parent_id" \
        --post_content="" \
        --porcelain)
    
    if [ -n "$page_id" ]; then
        # Set the page template
        wp post meta update "$page_id" "_wp_page_template" "$template"
        echo "  ✓ Created page '$title' (ID: $page_id) with template: $template"
    else
        echo "  ✗ Failed to create page '$title'"
    fi
}

# Fix venue pages
echo -e "\nFixing venue pages..."
venue_parent_id=$(wp post list --post_type=page --name="venue" --post_parent="$trainer_parent_id" --format=ids | head -n1)

if [ -z "$venue_parent_id" ]; then
    echo "Creating venue parent page..."
    venue_parent_id=$(wp post create --post_type=page --post_status=publish --post_title="Venue" --post_name="venue" --post_parent="$trainer_parent_id" --porcelain)
fi

# Check if manage page exists
manage_venue_id=$(wp post list --post_type=page --name="manage" --post_parent="$venue_parent_id" --format=ids | head -n1)
if [ -z "$manage_venue_id" ]; then
    create_page "$venue_parent_id" "manage" "Manage Venue" "page-trainer-venue-manage.php"
else
    echo "  ✓ Manage Venue page already exists"
fi

# Fix organizer pages
echo -e "\nFixing organizer pages..."
organizer_parent_id=$(wp post list --post_type=page --name="organizer" --post_parent="$trainer_parent_id" --format=ids | head -n1)

if [ -z "$organizer_parent_id" ]; then
    echo "Creating organizer parent page..."
    organizer_parent_id=$(wp post create --post_type=page --post_status=publish --post_title="Organizer" --post_name="organizer" --post_parent="$trainer_parent_id" --porcelain)
fi

# Check if list page exists
list_organizer_id=$(wp post list --post_type=page --name="list" --post_parent="$organizer_parent_id" --format=ids | head -n1)
if [ -z "$list_organizer_id" ]; then
    create_page "$organizer_parent_id" "list" "Organizers List" "page-trainer-organizers-list.php"
else
    echo "  ✓ Organizers List page already exists"
fi

# Check if manage page exists
manage_organizer_id=$(wp post list --post_type=page --name="manage" --post_parent="$organizer_parent_id" --format=ids | head -n1)
if [ -z "$manage_organizer_id" ]; then
    create_page "$organizer_parent_id" "manage" "Manage Organizer" "page-trainer-organizer-manage.php"
else
    echo "  ✓ Manage Organizer page already exists"
fi

echo -e "\nListing all trainer pages..."
wp post list --post_type=page --post_parent="$trainer_parent_id" --fields=ID,post_title,post_name,post_status --format=table

echo -e "\nFlushing rewrite rules..."
wp rewrite flush

echo -e "\n✅ Page fixing complete!"
EOF

echo -e "${GREEN}Trainer pages fixed successfully!${NC}"