<?php
if (!defined('ABSPATH')) exit; // Exit if accessed directly
require_once WPKGINST_PLUGIN_DIR . 'vendor/autoload.php';
use phpseclib3\Net\SSH2;
function wpkginst_execute_ssh_command($command) {
    $host = get_option('wpkginst_ssh_host', 'localhost');
    $username = get_option('wpkginst_ssh_username', 'root');
    $port = get_option('wpkginst_ssh_port', 22);
    $auth_type = get_option('wpkginst_ssh_auth_type', 'key');
    try {
        $ssh = new SSH2($host, $port);
        if ($auth_type === 'key') {
            $private_key = get_option('wpkginst_ssh_private_key', '');
            if (empty($private_key)) {
                $message = 'SSH private key not configured.';
                update_option('wpkginst_ssh_last_error', $message);
                return ['success' => false, 'message' => $message];
            }
            if (!$ssh->login($username, $private_key)) {
                $message = 'SSH login failed with private key.';
                update_option('wpkginst_ssh_last_error', $message);
                return ['success' => false, 'message' => $message];
            }
        } else {
            $password = get_option('wpkginst_ssh_password', '');
            if (empty($password)) {
                $message = 'SSH password not configured.';
                update_option('wpkginst_ssh_last_error', $message);
                return ['success' => false, 'message' => $message];
            }
            if (!$ssh->login($username, $password)) {
                $message = 'SSH login failed with password.';
                update_option('wpkginst_ssh_last_error', $message);
                return ['success' => false, 'message' => $message];
            }
        }
        $output = $ssh->exec($command);
        update_option('wpkginst_ssh_last_error', ''); // Clear error on success
        return ['success' => true, 'output' => $output];
    } catch (Exception $e) {
        $message = 'SSH error: ' . $e->getMessage();
        update_option('wpkginst_ssh_last_error', $message);
        return ['success' => false, 'message' => $message];
    }
}
function wpkginst_get_package_status($package) {
    $command = "dpkg-query -W -f='\${Status}' $package 2>&1";
    $result = wpkginst_execute_ssh_command($command);
    // Log raw output for debugging
    error_log('wpkginst_get_package_status: Package=' . $package . ', Command=' . $command . ', Output=' . print_r($result['output'], true));
    $installed = $result['success'] && (strpos($result['output'], 'install ok installed') !== false || strpos($result['output'], 'installé') !== false);
    return [
        'success' => $result['success'],
        'installed' => $installed,
        'message' => $result['output'] ?: ($result['message'] ?? '')
    ];
}
function wpkginst_test_ssh_connection() {
    $command = 'whoami';
    $result = wpkginst_execute_ssh_command($command);
    error_log('wpkginst_test_ssh_connection: Command=' . $command . ', Output=' . print_r($result, true));
    return [
        'success' => $result['success'],
        'message' => $result['success'] ? 'SSH connection successful' : ($result['message'] ?? 'Unknown error')
    ];
}
?>
