/**
 * Patient Journey Simulation:
 * 1. OPD Payment
 * 2. Doctor Consultation (Prescription + Lab Request)
 * 3. Pharmacy Dispensing (Stock + GST Bill)
 * 4. Lab Forwarding
 */

const queueManager = require('./backend/queueManager');
const prescriptionManager = require('./backend/prescriptionManager');
const pharmacyManager = require('./backend/pharmacyManager');
const billingManager = require('./backend/billingManager');

async function startJourney() {
    console.log("=== 🏥 CURIO PATIENT JOURNEY START ===\n");
    const patientId = "P-8899";
    const patientName = "Rahul Verma";

    // 1. OPD Registration & Payment
    console.log("Step 1: Patient arrives at OPD and pays consultation fee.");
    const opdToken = queueManager.bookOnline("2026-05-14", "10:00");
    console.log(`[Billing] OPD Invoice Generated: ₹${opdToken.fee}. Status: PAID ✅`);
    console.log(`[Queue] Token Issued: ${opdToken.token}. Patient is in queue.\n`);

    // 2. Doctor Consultation
    console.log("Step 2: Doctor sees patient and writes prescription + lab tests.");
    const medicines = [
        { name: "Paracetamol", dosage: "500mg", frequency: "1-0-1", duration: "5 Days", qty: 10 },
        { name: "Cough Syrup", dosage: "10ml", frequency: "0-0-1", duration: "3 Days", qty: 1 }
    ];
    const labTests = [
        { testName: "Complete Blood Count (CBC)", instructions: "Fasting not required" }
    ];
    const rx = prescriptionManager.generate(patientId, "DR-SHARMA", medicines, "Viral Fever", labTests);
    console.log("[Doctor] Prescription Generated. Sent to patient via WhatsApp.");
    console.log(rx.whatsappMessage + "\n");

    // 3. Pharmacy Fulfillment
    console.log("Step 3: Patient goes to Pharmacy.");
    const fulfillment = pharmacyManager.fulfillPrescription(rx.data);
    
    console.log("\n--- 💳 Pharmacy GST Bill ---");
    fulfillment.billItems.forEach(item => {
        console.log(`${item.itemName} | HSN: ${item.hsn} | Qty: ${item.qty} | GST: ${item.gstRate}% | Total: ₹${item.total.toFixed(2)}`);
    });
    console.log(`GRAND TOTAL: ₹${fulfillment.totalBill.toFixed(2)}`);
    console.log("Status: PAID ✅\n");

    // 4. Lab Integration
    console.log("Step 4: Checking Lab Status...");
    if (fulfillment.labStatus === "FORWARDED") {
        console.log("✅ SUCCESS: Lab tests have been automatically synced with the Lab department.");
    }

    console.log("\n=== 🏁 PATIENT JOURNEY COMPLETE ===");
}

startJourney();
