#!/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}Creating 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 creating trainer pages..."

# Function to create a page if it doesn't exist
create_page_if_not_exists() {
    local slug="$1"
    local title="$2"
    local parent_id="$3"
    local template="$4"
    
    # Check if page exists
    if wp post list --post_type=page --name="$slug" --format=ids | grep -q .; then
        echo "  ✓ Page '$title' already exists"
    else
        echo "  Creating page: $title"
        page_id=$(wp post create --post_type=page --post_status=publish --post_title="$title" --post_name="$slug" --post_parent="$parent_id" --page_template="$template" --porcelain)
        echo "  ✓ Created page '$title' (ID: $page_id)"
    fi
}

# Get parent page IDs
trainer_parent_id=$(wp post list --post_type=page --name="trainer" --format=ids | head -n1)
master_trainer_parent_id=$(wp post list --post_type=page --name="master-trainer" --format=ids | head -n1)

echo "Trainer parent ID: $trainer_parent_id"
echo "Master trainer parent ID: $master_trainer_parent_id"

# Create trainer pages
echo -e "\nCreating trainer pages..."

# Venues 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

create_page_if_not_exists "list" "Venues List" "$venue_parent_id" "page-trainer-venues-list.php"
create_page_if_not_exists "manage" "Manage Venue" "$venue_parent_id" "page-trainer-venue-manage.php"

# Profile pages
profile_parent_id=$(wp post list --post_type=page --name="profile" --post_parent="$trainer_parent_id" --format=ids | head -n1)
if [ -z "$profile_parent_id" ]; then
    echo "  Creating profile parent page..."
    profile_parent_id=$(wp post create --post_type=page --post_status=publish --post_title="Profile" --post_name="profile" --post_parent="$trainer_parent_id" --page_template="page-trainer-profile.php" --porcelain)
fi

create_page_if_not_exists "edit" "Edit Profile" "$profile_parent_id" "page-trainer-profile-edit.php"

# 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

create_page_if_not_exists "list" "Organizers List" "$organizer_parent_id" "page-trainer-organizers-list.php"
create_page_if_not_exists "manage" "Manage Organizer" "$organizer_parent_id" "page-trainer-organizer-manage.php"

echo -e "\n✅ All pages created successfully!"

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

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

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