#!/bin/bash

# Script to deploy the HVAC Community Events plugin to staging
# This uses a web-based approach since SSH may have permission issues

# Configuration
STAGING_URL="https://upskill-staging.measurequick.com"
PLUGIN_DIR="/Users/ben/dev/upskill-event-manager/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events"
TEMP_DIR="/tmp/hvac-plugin-deploy"
PACKAGE_NAME="hvac-community-events.zip"
WP_ADMIN="${STAGING_URL}/wp-admin"
UPLOAD_URL="${WP_ADMIN}/plugin-install.php?tab=upload"

# Create deployment package
echo "Creating plugin deployment package..."
mkdir -p "$TEMP_DIR"
cd "$PLUGIN_DIR" || { echo "Error: Plugin directory not found"; exit 1; }
zip -r "$TEMP_DIR/$PACKAGE_NAME" . -x "*.git*" -x "*.DS_Store" -x "*.idea*" -x "node_modules/*" -x "vendor/*"

echo "Plugin package created at: $TEMP_DIR/$PACKAGE_NAME"
echo "Package size: $(du -h $TEMP_DIR/$PACKAGE_NAME | cut -f1)"

# Open browser to upload page
echo "Please manually upload the plugin package at: $TEMP_DIR/$PACKAGE_NAME"
echo "Upload URL: $UPLOAD_URL"

# Instructions for manual upload and activation
echo "=== Manual Upload Instructions ==="
echo "1. Navigate to: $UPLOAD_URL"
echo "2. Login with your admin credentials"
echo "3. Click 'Browse' and select the file at: $TEMP_DIR/$PACKAGE_NAME"
echo "4. Click 'Install Now'"
echo "5. Once installed, click 'Activate Plugin'"
echo "6. Verify the plugin is active at ${WP_ADMIN}/plugins.php"

# Open browser to upload URL
if [[ "$OSTYPE" == "darwin"* ]]; then
    open "$UPLOAD_URL"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
    xdg-open "$UPLOAD_URL"
fi

# Create instructions for creating the .env file with required Zoho settings
cat > "$TEMP_DIR/zoho-env-instructions.txt" << EOF
### IMPORTANT: After plugin activation, create a .env file in the plugin directory with the following settings:

# Create this file at: /wp-content/plugins/hvac-community-events/.env

# Zoho API Credentials
ZOHO_CLIENT_ID=your_client_id_here
ZOHO_CLIENT_SECRET=your_client_secret_here
ZOHO_REDIRECT_URI=${STAGING_URL}/wp-admin/admin-ajax.php?action=zoho_oauth_callback
ZOHO_REFRESH_TOKEN=your_refresh_token_here

# Site URL Settings 
UPSKILL_STAGING_URL=${STAGING_URL}

# Create this file using the WordPress file editor or via FTP
EOF

echo "Zoho environment instructions created at: $TEMP_DIR/zoho-env-instructions.txt"
echo "Done! Follow the instructions above to complete the installation."