<?php
/**
 * Web-based installer for HVAC Community Events plugin
 * This file should be uploaded to the staging server root and accessed via web browser
 */

// Security check - use a secret key parameter to prevent unauthorized access
$security_key = isset($_GET['key']) ? $_GET['key'] : '';
$valid_key = 'hvac_installer_2025'; // Change this to your preferred key

if ($security_key !== $valid_key) {
    die('Unauthorized access');
}

// Configuration
$staging_url = 'https://upskill-staging.measurequick.com';
$plugin_dir = ABSPATH . 'wp-content/plugins/hvac-community-events';
$plugin_zip_url = isset($_GET['zip_url']) ? $_GET['zip_url'] : '';
$action = isset($_GET['action']) ? $_GET['action'] : '';

// Initialize WordPress
define('WP_USE_THEMES', false);
require_once('wp-load.php');

// Check admin capabilities
if (!current_user_can('install_plugins')) {
    die('You do not have permission to install plugins');
}

// Set up HTML response
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
    <title>HVAC Community Events Plugin Installer</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 20px auto; padding: 20px; }
        pre { background: #f5f5f5; padding: 10px; overflow: auto; }
        .success { color: green; }
        .error { color: red; }
        .step { margin-bottom: 20px; padding: 15px; border: 1px solid #ddd; }
    </style>
</head>
<body>
    <h1>HVAC Community Events Plugin Installer</h1>
    
    <?php if ($action === 'install' && !empty($plugin_zip_url)): ?>
        <div class="step">
            <h2>Installing Plugin from URL</h2>
            <?php
            // Include WordPress plugin installer
            require_once(ABSPATH . 'wp-admin/includes/file.php');
            require_once(ABSPATH . 'wp-admin/includes/plugin-install.php');
            require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
            require_once(ABSPATH . 'wp-admin/includes/plugin.php');
            
            // Set up the upgrader
            $skin = new WP_Ajax_Upgrader_Skin();
            $upgrader = new Plugin_Upgrader($skin);
            
            // Install the plugin
            echo '<pre>';
            echo "Attempting to install plugin from: $plugin_zip_url\n";
            $result = $upgrader->install($plugin_zip_url);
            
            if ($result) {
                echo "Plugin installed successfully.\n";
                
                // Activate the plugin
                $plugin_main_file = 'hvac-community-events/hvac-community-events.php';
                $activate = activate_plugin($plugin_main_file);
                
                if (is_wp_error($activate)) {
                    echo "Error activating plugin: " . $activate->get_error_message() . "\n";
                } else {
                    echo "Plugin activated successfully.\n";
                    
                    // Create .env file with Zoho settings
                    $env_file = $plugin_dir . '/.env';
                    $env_content = "# Zoho API Credentials\n";
                    $env_content .= "ZOHO_CLIENT_ID=your_client_id_here\n";
                    $env_content .= "ZOHO_CLIENT_SECRET=your_client_secret_here\n";
                    $env_content .= "ZOHO_REDIRECT_URI={$staging_url}/wp-admin/admin-ajax.php?action=zoho_oauth_callback\n";
                    $env_content .= "ZOHO_REFRESH_TOKEN=your_refresh_token_here\n\n";
                    $env_content .= "# Site URL Settings\n";
                    $env_content .= "UPSKILL_STAGING_URL={$staging_url}\n";
                    
                    if (file_put_contents($env_file, $env_content)) {
                        echo "Created .env file with default settings.\n";
                    } else {
                        echo "Could not create .env file. Please create it manually.\n";
                    }
                }
            } else {
                echo "Error installing plugin.\n";
                if (is_wp_error($skin->result)) {
                    echo "Error: " . $skin->result->get_error_message() . "\n";
                }
            }
            echo '</pre>';
            ?>
        </div>
    <?php endif; ?>
    
    <div class="step">
        <h2>Plugin Installation Form</h2>
        <form method="GET" action="">
            <input type="hidden" name="key" value="<?php echo esc_attr($valid_key); ?>">
            <input type="hidden" name="action" value="install">
            <p>
                <label for="zip_url">Plugin ZIP URL:</label><br>
                <input type="text" id="zip_url" name="zip_url" style="width: 100%;" 
                       placeholder="https://example.com/path/to/hvac-community-events.zip" required>
            </p>
            <p><input type="submit" value="Install Plugin"></p>
        </form>
    </div>
    
    <div class="step">
        <h2>Manual Plugin Upload</h2>
        <p>If the automatic installation fails, you can manually upload the plugin:</p>
        <ol>
            <li>Go to <a href="<?php echo esc_url(admin_url('plugin-install.php?tab=upload')); ?>" target="_blank">WordPress Plugin Upload</a></li>
            <li>Upload the plugin ZIP file</li>
            <li>Activate the plugin</li>
        </ol>
    </div>
    
    <div class="step">
        <h2>Create .env File</h2>
        <p>After installing the plugin, create a .env file in the plugin directory with these settings:</p>
        <pre># Zoho API Credentials
ZOHO_CLIENT_ID=your_client_id_here
ZOHO_CLIENT_SECRET=your_client_secret_here
ZOHO_REDIRECT_URI=<?php echo esc_html($staging_url); ?>/wp-admin/admin-ajax.php?action=zoho_oauth_callback
ZOHO_REFRESH_TOKEN=your_refresh_token_here

# Site URL Settings
UPSKILL_STAGING_URL=<?php echo esc_html($staging_url); ?></pre>
    </div>
    
    <div class="step">
        <h2>Plugin Status</h2>
        <?php
        // Check if plugin is installed and active
        if (is_plugin_active('hvac-community-events/hvac-community-events.php')) {
            echo '<p class="success">HVAC Community Events plugin is installed and active.</p>';
            
            // Check .env file
            if (file_exists($plugin_dir . '/.env')) {
                echo '<p class="success">.env file exists in the plugin directory.</p>';
            } else {
                echo '<p class="error">.env file does not exist in the plugin directory.</p>';
            }
        } else {
            if (file_exists($plugin_dir)) {
                echo '<p class="error">HVAC Community Events plugin is installed but not active.</p>';
            } else {
                echo '<p class="error">HVAC Community Events plugin is not installed.</p>';
            }
        }
        ?>
    </div>
</body>
</html>
<?php
// End of file