#!/bin/bash

# Enhanced Template Deployment Add-on Script
# Run this after main plugin deployment to deploy enhanced TEC template

set -e

# Source environment variables
source .env

echo "🚀 Deploying Enhanced TEC Template to Theme Directory..."

# Get server connection details
SERVER_IP="$UPSKILL_STAGING_IP"
SSH_USER="$UPSKILL_STAGING_SSH_USER"
SERVER_PATH="$UPSKILL_STAGING_PATH"

# Get SSH password from environment
SSH_PASS="$UPSKILL_STAGING_PASS"

if [ -z "$SSH_PASS" ]; then
    echo "Error: SSH password not found in environment variables"
    exit 1
fi

# Deploy enhanced template and partials to theme directory
echo "📁 Creating theme directory structure on server..."
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SERVER_IP" "
    cd $SERVER_PATH &&
    mkdir -p wp-content/themes/astra-child-hvac/tribe-events/community/partials &&
    echo '✅ Theme directories created'
"

echo "📄 Deploying enhanced TEC template..."
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SERVER_IP" "
    cd $SERVER_PATH &&
    cp wp-content/plugins/hvac-community-events/templates/community-edit-event-enhanced.php wp-content/themes/astra-child-hvac/tribe-events/community/edit-event.php &&
    echo '✅ Enhanced template deployed'
"

echo "🔧 Deploying field partials..."
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SERVER_IP" "
    cd $SERVER_PATH &&
    cp wp-content/plugins/hvac-community-events/templates/partials/excerpt-field.php wp-content/themes/astra-child-hvac/tribe-events/community/partials/ &&
    cp wp-content/plugins/hvac-community-events/templates/partials/categories-field.php wp-content/themes/astra-child-hvac/tribe-events/community/partials/ &&
    cp wp-content/plugins/hvac-community-events/templates/partials/featured-image-field.php wp-content/themes/astra-child-hvac/tribe-events/community/partials/ &&
    cp wp-content/plugins/hvac-community-events/templates/partials/tags-field.php wp-content/themes/astra-child-hvac/tribe-events/community/partials/ &&
    echo '✅ Field partials deployed'
"

echo "🔒 Setting file permissions..."
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SERVER_IP" "
    cd $SERVER_PATH &&
    chmod 644 wp-content/themes/astra-child-hvac/tribe-events/community/edit-event.php &&
    chmod 644 wp-content/themes/astra-child-hvac/tribe-events/community/partials/*.php &&
    echo '✅ Permissions set'
"

echo "✅ Verifying deployment..."
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SERVER_IP" "
    cd $SERVER_PATH &&
    echo '📋 Enhanced Template Files:' &&
    ls -la wp-content/themes/astra-child-hvac/tribe-events/community/ &&
    echo '📋 Field Partials:' &&
    ls -la wp-content/themes/astra-child-hvac/tribe-events/community/partials/
"

echo ""
echo "🎉 Enhanced TEC Template Deployment Complete!"
echo ""
echo "📋 Deployment Summary:"
echo "- Enhanced template: /wp-content/themes/astra-child-hvac/tribe-events/community/edit-event.php"
echo "- Field partials: /wp-content/themes/astra-child-hvac/tribe-events/community/partials/ (4 files)"
echo ""
echo "🔗 Test URL: https://upskill-staging.measurequick.com/?events-community=add"
echo ""
echo "📝 Expected Enhanced Features:"
echo "- ✓ Event excerpt field with character counter"
echo "- ✓ Categories multi-select with search functionality"
echo "- ✓ Featured image upload with WordPress media library"
echo "- ✓ Tags with autocomplete and popular suggestions"
echo "- ✓ Enhanced responsive design and accessibility"
echo ""
echo "🧪 Run test: node test-enhanced-field-deployment.js"