<?php
/**
 * WCBA API connection test.
 *
 * Standalone script for the shop root directory. When called, it performs a request to
 * https://api2.bexio-shop.ch/api/client using the API class of the
 * "raptus-wc-bexio-connector" plugin. It uses the same authorization that the
 * plugin itself uses from its options.
 *
 * Remove again after troubleshooting - the script displays internal connection details.
 */

require_once __DIR__ . '/wp-load.php';

// Restrict access to administrators, as connection details are displayed.
if (!current_user_can('manage_options')) {
    wp_die('Access denied. This page is only available to logged-in administrators.');
}

if (!class_exists('Raptus\WC\BexioConnector\Api')) {
    wp_die('The "raptus-wc-bexio-connector" plugin is not active, the API class could not be found.');
}

$api = \Raptus\WC\BexioConnector\Api::instance();

// Ignore the skipping transient, if set
$skip_transient_key = 'raptus_wp_bexio_connector_skip_request_after_timeout';
add_filter("pre_transient_{$skip_transient_key}", function() {
    return 0; // falsy, but !== false, so get_transient() directly returns this value
});

// client_status() internally calls $this->request('GET', '/client'), i.e.
// GET https://api2.bexio-shop.ch/api/client, using the authorization stored
// in the WCBA options (Options::get('authorization')).
$request_sent_at = new DateTime();
$result = $api->client_status();
$response_received_at = new DateTime();

$error = $api->get_error();
$duration_ms = round(($response_received_at->format('U.u') - $request_sent_at->format('U.u')) * 1000, 1);

header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WCBA API Connection Test</title>
<style>
    body { font-family: -apple-system, Segoe UI, Arial, sans-serif; max-width: 900px; margin: 2rem auto; padding: 0 1rem; color: #1a1a1a; }
    h1 { font-size: 1.3rem; }
    table { border-collapse: collapse; margin-bottom: 1.5rem; }
    td, th { border: 1px solid #ccc; padding: 0.4rem 0.8rem; text-align: left; vertical-align: top; }
    th { background: #f2f2f2; white-space: nowrap; }
    pre { background: #f7f7f7; border: 1px solid #ccc; padding: 1rem; overflow-x: auto; white-space: pre-wrap; word-break: break-word; }
    .status-ok { color: #146c2e; font-weight: bold; }
    .status-error { color: #b3261e; font-weight: bold; }
</style>
</head>
<body>

<h1>WCBA API Connection Test</h1>
<p>Request via <code>Raptus\WC\BexioConnector\Api::client_status()</code> to <code>GET <?= htmlspecialchars(\Raptus\WC\BexioConnector\Api::REMOTE_URL) ?>/client</code>.</p>

<table>
    <tr><th>Request sent at</th><td><?= htmlspecialchars($request_sent_at->format('Y-m-d H:i:s.u')) ?></td></tr>
    <tr><th>Response received at</th><td><?= htmlspecialchars($response_received_at->format('Y-m-d H:i:s.u')) ?></td></tr>
    <tr><th>Duration</th><td><?= htmlspecialchars($duration_ms) ?> ms</td></tr>
    <tr>
        <th>Status</th>
        <td>
            <?php if ($result !== false): ?>
                <span class="status-ok">Success</span>
            <?php else: ?>
                <span class="status-error">Error</span>
            <?php endif; ?>
        </td>
    </tr>
</table>

<?php if ($result !== false): ?>
    <h2>Response Content</h2>
    <pre><?= htmlspecialchars(print_r($result, true)) ?></pre>
<?php else: ?>
    <h2>Error Details</h2>
    <pre><?= htmlspecialchars(print_r($error, true)) ?></pre>
<?php endif; ?>

</body>
</html>
