#!/bin/bash

# Direct certificate fix deployment script
# This script creates a ZIP package of all the fixes for direct upload to the staging server

# Directory setup
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
OUTPUT_DIR="$PROJECT_ROOT/certificate-fixes"
mkdir -p "$OUTPUT_DIR"

echo "Creating certificate fix package..."

# Create directory structure
mkdir -p "$OUTPUT_DIR/templates/certificates"
mkdir -p "$OUTPUT_DIR/includes/certificates"
mkdir -p "$OUTPUT_DIR/bin"

# Copy the fixed template files
cp "$PROJECT_ROOT/wordpress/wp-content/plugins/hvac-community-events/templates/certificates/template-certificate-reports.php" "$OUTPUT_DIR/templates/certificates/"
cp "$PROJECT_ROOT/wordpress/wp-content/plugins/hvac-community-events/templates/certificates/template-certificate-reports-simple.php" "$OUTPUT_DIR/templates/certificates/"
cp "$PROJECT_ROOT/wordpress/wp-content/plugins/hvac-community-events/templates/certificates/certificate-fix.php" "$OUTPUT_DIR/templates/certificates/"

# Copy the certificate class files
cp "$PROJECT_ROOT/wordpress/wp-content/plugins/hvac-community-events/includes/certificates/class-certificate-manager.php" "$OUTPUT_DIR/includes/certificates/"
cp "$PROJECT_ROOT/wordpress/wp-content/plugins/hvac-community-events/includes/certificates/class-certificate-installer.php" "$OUTPUT_DIR/includes/certificates/"
cp "$PROJECT_ROOT/wordpress/wp-content/plugins/hvac-community-events/includes/certificates/class-certificate-fix.php" "$OUTPUT_DIR/includes/certificates/"

# Copy the fix scripts
cp "$PROJECT_ROOT/bin/emergency-certificate-fix.php" "$OUTPUT_DIR/bin/"
cp "$PROJECT_ROOT/bin/fix-html-comments.php" "$OUTPUT_DIR/bin/"
cp "$PROJECT_ROOT/bin/test-certificate-system.php" "$OUTPUT_DIR/bin/"

# Copy documentation
cp "$PROJECT_ROOT/CERTIFICATE_TROUBLESHOOTING.md" "$OUTPUT_DIR/"
cp "$PROJECT_ROOT/CERTIFICATE_FIX.md" "$OUTPUT_DIR/"

# Create README with instructions
cat > "$OUTPUT_DIR/README.md" << 'EOF'
# Certificate System Fix Package

This package contains fixes for the certificate system in the HVAC Community Events plugin.

## Quick Fix Instructions

1. Upload the emergency-certificate-fix.php script to your WordPress site (e.g., via FTP)
2. Upload the template-certificate-reports-simple.php file to the same directory as the emergency-certificate-fix.php script
3. Visit the emergency-certificate-fix.php in your browser (e.g., https://your-site.com/emergency-certificate-fix.php)
4. Follow the on-screen instructions to apply the fixes
5. After the fixes are applied, delete the emergency-certificate-fix.php script

## Complete Fix Instructions

For a more thorough fix:

1. Replace the files in your WordPress installation with the ones in this package:
   - Copy `templates/certificates/*` to `wp-content/plugins/hvac-community-events/templates/certificates/`
   - Copy `includes/certificates/*` to `wp-content/plugins/hvac-community-events/includes/certificates/`

2. Run the test-certificate-system.php script to verify the fixes:
   - Upload the script to your WordPress site
   - Visit the script in your browser or run it via command line

## Testing

After applying the fixes, test both certificate pages:
- Generate Certificates page: /generate-certificates/
- Certificate Reports page: /certificate-reports/

## Additional Resources

- CERTIFICATE_TROUBLESHOOTING.md - Detailed troubleshooting guide
- CERTIFICATE_FIX.md - Explanation of all fixes applied

If you have any questions or issues, please contact the development team.
EOF

# Create ZIP file
ZIP_FILE="$PROJECT_ROOT/certificate-fixes.zip"
cd "$OUTPUT_DIR" || exit 1
zip -r "$ZIP_FILE" .

echo "Certificate fix package created at: $ZIP_FILE"
echo "Upload this file to the staging server and follow the instructions in the README.md file."