/**
 * LabManager: Handles test bookings, results, and AI explanations.
 */

class LabManager {
    constructor() {
        this.tests = [
            { id: 'T01', name: 'Complete Blood Count (CBC)', price: 450 },
            { id: 'T02', name: 'Lipid Profile', price: 800 },
            { id: 'T03', name: 'Blood Sugar (F/PP)', price: 150 },
            { id: 'T04', name: 'Thyroid (T3 T4 TSH)', price: 650 },
            { id: 'T05', name: 'Liver Function Test', price: 1200 }
        ];
        this.records = [
            { id: 'LAB-501', patientName: 'Suresh Kumar', testName: 'CBC + Blood Sugar', status: 'PROCESSING', urgency: 'URGENT' },
            { id: 'LAB-502', patientName: 'Anjali Singh', testName: 'Lipid Profile', status: 'COLLECTING', urgency: 'ROUTINE' }
        ];
    }

    getAvailableTests() {
        return this.tests;
    }

    createTestRequest(patientName, testId) {
        const test = this.tests.find(t => t.id === testId);
        const record = {
            id: `LAB-${Date.now()}`,
            patientName,
            testName: test.name,
            status: 'COLLECTING_SAMPLE',
            results: null,
            timestamp: new Date().toISOString()
        };
        this.records.push(record);
        return record;
    }

    updateResults(recordId, results) {
        const record = this.records.find(r => r.id === recordId);
        if (record) {
            record.results = results;
            record.status = 'COMPLETED';
            
            // AI Explainer logic (Simulation)
            const explanation = `Your ${record.testName} results show ${results.hemoglobin < 12 ? 'low hemoglobin (anemia)' : 'normal levels'}. Please discuss with Dr. Sharma.`;
            
            return {
                success: true,
                explanation,
                whatsappMessage: `🔬 *Lab Report Ready*\n\n*Test:* ${record.testName}\n\n*AI Summary:* ${explanation}\n\n_Full report PDF attached._`
            };
        }
        return { success: false };
    }
}

module.exports = new LabManager();
