const patientId = "PAT-TEST-001";

async function test() {
    console.log("1. Starting Lab Watcher...");
    let res = await fetch("http://localhost:3000/api/ai/lab/watch/start", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
            patientId,
            baselineData: {
                baseline: "Patient is on Warfarin (blood thinner) and has hypertension.",
                medications: ["Warfarin", "Lisinopril"]
            }
        })
    });
    console.log("Start Response:", await res.json());

    console.log("\n2. Submitting dangerous lab result...");
    res = await fetch(`http://localhost:3000/api/ai/lab/result/${patientId}`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
            result: "Latest INR level is 6.5 (Dangerously High)"
        })
    });
    console.log("Signal Response:", await res.json());

    console.log("\n3. Polling for AI Alerts (waiting 15 seconds)...");
    for (let i = 0; i < 4; i++) {
        await new Promise(r => setTimeout(r, 5000));
        res = await fetch(`http://localhost:3000/api/ai/lab/alerts/${patientId}`);
        const data = await res.json();
        console.log(`Poll ${i+1}:`, data);
        if (data.alerts && data.alerts.length > 0) {
            console.log("🎉 SUCCESS! Alert received:");
            console.log(data.alerts[0]);
            break;
        }
    }
}

test();
