<?php
// tests/test-wp.php — Verify wp-data.php, uptime-check.php, ssl-check.php
require_once __DIR__ . '/run.php';

$base    = defined('TEST_BASE_URL')       ? TEST_BASE_URL       : 'http://localhost/supercerebro';
$session = defined('TEST_SESSION_COOKIE') ? TEST_SESSION_COOKIE : '';

// ── Helper: fetch and return [http_code, json] ────────────────────────────────
function wp_fetch(string $url, string $session = ''): array {
    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => false,
        CURLOPT_TIMEOUT        => 15,
        CURLOPT_HTTPHEADER     => $session ? ["Cookie: {$session}"] : [],
    ]);
    $raw  = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return [$code, json_decode($raw, true)];
}

// ── 1. wp-data.php ───────────────────────────────────────────────────────────
[$code, $json] = wp_fetch("{$base}/api/wp-data.php?action=overview&client_id=1");

if ($code === 404) {
    dm_skip('wp-data: auth block',     'api/wp-data.php not yet deployed — wait for Chat 1');
    dm_skip('wp-data: response shape', 'api/wp-data.php not yet deployed — wait for Chat 1');
} else {
    $blocked = ($code === 401 || $code === 302 || $code === 403)
            || ($json && isset($json['ok']) && $json['ok'] === false);
    dm_assert('wp-data: auth block (no session)', $blocked,
        "Expected 401/302/403 or ok:false, got HTTP {$code}");

    if ($session) {
        [$code2, $json2] = wp_fetch("{$base}/api/wp-data.php?action=overview&client_id=1", $session);
        dm_assert('wp-data: HTTP 200 with session', $code2 === 200, "Got HTTP {$code2}");
        dm_assert('wp-data: has ok field',          array_key_exists('ok', $json2 ?? []), 'Missing ok field');
        if (!empty($json2['ok'])) {
            $d = $json2['data'] ?? [];
            dm_assert('wp-data: has site_name or error', isset($d['site_name']) || isset($d['url']),
                'Missing site_name/url in data');
        } else {
            dm_assert('wp-data: error has message', !empty($json2['error']),
                'ok:false but no error field — WP creds probably not set for client 1');
        }
    } else {
        dm_skip('wp-data: response shape', 'TEST_SESSION_COOKIE not set');
    }
}

// ── 2. uptime-check.php ──────────────────────────────────────────────────────
[$code, $json] = wp_fetch("{$base}/api/uptime-check.php?client_id=1");

if ($code === 404) {
    dm_skip('uptime-check: auth block',     'api/uptime-check.php not yet deployed — wait for Chat 1');
    dm_skip('uptime-check: response shape', 'api/uptime-check.php not yet deployed — wait for Chat 1');
} else {
    $blocked = ($code === 401 || $code === 302 || $code === 403)
            || ($json && isset($json['ok']) && $json['ok'] === false);
    dm_assert('uptime-check: auth block (no session)', $blocked,
        "Expected 401/302/403 or ok:false, got HTTP {$code}");

    if ($session) {
        [$code2, $json2] = wp_fetch("{$base}/api/uptime-check.php?client_id=1", $session);
        dm_assert('uptime-check: HTTP 200 with session', $code2 === 200, "Got HTTP {$code2}");
        dm_assert('uptime-check: has ok field',          array_key_exists('ok', $json2 ?? []), 'Missing ok field');
        if (!empty($json2['ok'])) {
            $d = $json2['data'] ?? [];
            dm_assert('uptime-check: has status',  array_key_exists('status',  $d), 'Missing status');
            dm_assert('uptime-check: has latency',
                array_key_exists('latency', $d) || array_key_exists('latency_ms', $d),
                'Missing latency or latency_ms field');
        } else {
            dm_assert('uptime-check: error has message', !empty($json2['error']), 'ok:false but no error field');
        }
    } else {
        dm_skip('uptime-check: response shape', 'TEST_SESSION_COOKIE not set');
    }
}

// ── 3. ssl-check.php ─────────────────────────────────────────────────────────
[$code, $json] = wp_fetch("{$base}/api/ssl-check.php?client_id=1");

if ($code === 404) {
    dm_skip('ssl-check: auth block',     'api/ssl-check.php not yet deployed — wait for Chat 1');
    dm_skip('ssl-check: response shape', 'api/ssl-check.php not yet deployed — wait for Chat 1');
} else {
    $blocked = ($code === 401 || $code === 302 || $code === 403)
            || ($json && isset($json['ok']) && $json['ok'] === false);
    dm_assert('ssl-check: auth block (no session)', $blocked,
        "Expected 401/302/403 or ok:false, got HTTP {$code}");

    if ($session) {
        [$code2, $json2] = wp_fetch("{$base}/api/ssl-check.php?client_id=1", $session);
        dm_assert('ssl-check: HTTP 200 with session', $code2 === 200, "Got HTTP {$code2}");
        dm_assert('ssl-check: has ok field',          array_key_exists('ok', $json2 ?? []), 'Missing ok field');
        if (!empty($json2['ok'])) {
            $d = $json2['data'] ?? [];
            dm_assert('ssl-check: has days_remaining', array_key_exists('days_remaining', $d),
                'Missing days_remaining');
            dm_assert('ssl-check: has status',         array_key_exists('status', $d), 'Missing status');
            dm_assert('ssl-check: days_remaining is int',
                is_int($d['days_remaining'] ?? null) || is_numeric($d['days_remaining'] ?? null),
                'days_remaining should be numeric');
        } else {
            dm_assert('ssl-check: error has message', !empty($json2['error']), 'ok:false but no error field');
        }
    } else {
        dm_skip('ssl-check: response shape', 'TEST_SESSION_COOKIE not set');
    }
}
