#!/bin/bash

# Enhanced TEC Template Deployment Script
# Deploys enhanced template and field partials to theme directory

set -e

echo "🚀 Deploying Enhanced TEC Template with Field Partials..."

# Configuration
SOURCE_DIR="/home/ben/dev/upskill-event-manager"
THEME_DIR="/wp-content/themes/astra-child-hvac"
TEC_TEMPLATE_DIR="$THEME_DIR/tribe-events/community"
PARTIALS_DIR="$TEC_TEMPLATE_DIR/partials"

# Create theme directories if they don't exist
echo "📁 Creating theme directory structure..."
mkdir -p "$TEC_TEMPLATE_DIR"
mkdir -p "$PARTIALS_DIR"

# Deploy enhanced template
echo "📄 Deploying enhanced TEC template..."
cp "$SOURCE_DIR/templates/community-edit-event-enhanced.php" "$TEC_TEMPLATE_DIR/edit-event.php"

# Deploy all field partials
echo "🔧 Deploying field partials..."
cp "$SOURCE_DIR/templates/partials/excerpt-field.php" "$PARTIALS_DIR/"
cp "$SOURCE_DIR/templates/partials/categories-field.php" "$PARTIALS_DIR/"
cp "$SOURCE_DIR/templates/partials/featured-image-field.php" "$PARTIALS_DIR/"
cp "$SOURCE_DIR/templates/partials/tags-field.php" "$PARTIALS_DIR/"

# Set proper permissions
echo "🔒 Setting file permissions..."
chmod 644 "$TEC_TEMPLATE_DIR/edit-event.php"
chmod 644 "$PARTIALS_DIR"/*.php

# Verify deployment
echo "✅ Verifying deployment..."
FILES_TO_CHECK=(
    "$TEC_TEMPLATE_DIR/edit-event.php"
    "$PARTIALS_DIR/excerpt-field.php"
    "$PARTIALS_DIR/categories-field.php" 
    "$PARTIALS_DIR/featured-image-field.php"
    "$PARTIALS_DIR/tags-field.php"
)

MISSING_FILES=0
for file in "${FILES_TO_CHECK[@]}"; do
    if [[ ! -f "$file" ]]; then
        echo "❌ Missing: $file"
        MISSING_FILES=$((MISSING_FILES + 1))
    else
        echo "✓ Deployed: $file"
    fi
done

if [[ $MISSING_FILES -eq 0 ]]; then
    echo "🎉 Enhanced TEC Template deployment complete!"
    echo ""
    echo "📋 Deployment Summary:"
    echo "- Enhanced template: $TEC_TEMPLATE_DIR/edit-event.php"
    echo "- Field partials: $PARTIALS_DIR/ (4 files)"
    echo ""
    echo "🔗 Test URL: https://upskill-staging.measurequick.com/?events-community=add"
    echo ""
    echo "📝 Expected features:"
    echo "- ✓ Event excerpt field with character counter"
    echo "- ✓ Categories multi-select with search"
    echo "- ✓ Featured image upload with media library"
    echo "- ✓ Tags with autocomplete functionality"
    echo "- ✓ Enhanced responsive design"
    echo "- ✓ WCAG 2.1 AA accessibility compliance"
else
    echo "❌ Deployment failed! $MISSING_FILES files missing."
    exit 1
fi