#!/bin/bash

echo "Creating HVAC Plugin Installer Package..."

# Create the installer directory structure
mkdir -p installer-package/plugin-backups

# Copy the updated plugin zip
cp plugin-backups/hvac-community-events-updated.zip installer-package/plugin-backups/

# Copy the installer script
cp plugin-backups/complete-hvac-installer.php installer-package/

# Create a simple index.php file to access the installer
cat > installer-package/index.php << 'EOF'
<?php
/**
 * HVAC Plugin Installer Access Point
 */
?>
<!DOCTYPE html>
<html>
<head>
    <title>HVAC Plugin Installer</title>
    <style>
        body { 
            font-family: Arial, sans-serif; 
            max-width: 800px; 
            margin: 50px auto; 
            padding: 20px;
            background: #f1f1f1;
        }
        .installer-box {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .warning {
            background: #fff3cd;
            border: 1px solid #ffeaa7;
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 20px;
        }
        .btn {
            background: #0073aa;
            color: white;
            padding: 12px 24px;
            text-decoration: none;
            border-radius: 4px;
            display: inline-block;
        }
        .btn:hover {
            background: #005a87;
        }
    </style>
</head>
<body>
    <div class="installer-box">
        <h1>HVAC Community Events Plugin Installer</h1>
        
        <div class="warning">
            <strong>Warning:</strong> This installer will:
            <ul>
                <li>Install/Update the HVAC Community Events plugin</li>
                <li>Apply Zoho CRM domain fixes</li>
                <li>Create test user accounts</li>
                <li>Activate the plugin</li>
            </ul>
            Only run this if you have administrator access.
        </div>
        
        <h3>Installation Options:</h3>
        
        <p>
            <a href="complete-hvac-installer.php?install_key=hvac-staging-deploy-2025" class="btn">
                Run Complete Installation
            </a>
        </p>
        
        <h3>Manual Steps:</h3>
        <ol>
            <li>Download the plugin: <a href="plugin-backups/hvac-community-events-updated.zip">Plugin ZIP</a></li>
            <li>Upload via WordPress admin → Plugins → Add New → Upload</li>
            <li>Activate the plugin</li>
            <li>Create test users manually</li>
        </ol>
        
        <h3>Test Credentials (will be created):</h3>
        <ul>
            <li><strong>Admin:</strong> test_admin / hvac_staging_2025</li>
            <li><strong>Trainer:</strong> test_trainer / hvac_staging_2025</li>
        </ul>
    </div>
</body>
</html>
EOF

# Create a README for manual installation
cat > installer-package/README.md << 'EOF'
# HVAC Plugin Installation Package

## Automatic Installation
1. Upload this entire folder to your staging server
2. Access `index.php` in your browser
3. Click "Run Complete Installation"

## Manual Installation
1. Download `plugin-backups/hvac-community-events-updated.zip`
2. Go to WordPress Admin → Plugins → Add New → Upload Plugin
3. Upload and activate the plugin
4. Create test users:
   - test_admin (administrator)
   - test_trainer (trainer role)

## Files Included
- `complete-hvac-installer.php` - Automated installer script
- `plugin-backups/hvac-community-events-updated.zip` - Updated plugin with Zoho fixes
- `index.php` - Web interface for installation

## Test Credentials
- Admin: test_admin / hvac_staging_2025  
- Trainer: test_trainer / hvac_staging_2025

## Post-Installation
1. Login to wp-admin with test_admin
2. Go to HVAC → Zoho CRM Settings
3. Test the connection to verify domain fixes
EOF

# Create the final package
echo "Creating installer package archive..."
cd installer-package
zip -r ../hvac-installer-package.zip .
cd ..

echo "✓ Installer package created: hvac-installer-package.zip"
echo "✓ Package contents:"
ls -la installer-package/

echo ""
echo "Deployment Instructions:"
echo "1. Upload hvac-installer-package.zip to staging server"
echo "2. Extract to web-accessible directory"
echo "3. Access index.php in browser"
echo "4. Follow installation instructions"