const BaseAgent = require('./BaseAgent');

class CDSSAgent extends BaseAgent {
    constructor() {
        super('CDSSAgent');
        this.responseMocks = {
            'warfarin': JSON.stringify({ hasWarning: true, warningLevel: 'HIGH', message: 'Potential drug interaction: Aspirin and Warfarin increase bleeding risk.' }),
            'penicillin': JSON.stringify({ hasWarning: true, warningLevel: 'CRITICAL', message: 'Patient has a documented allergy to Penicillin!' }),
            'default': JSON.stringify({ hasWarning: false })
        };
    }

    async execute(config, payload) {
        const { currentPrescription, patientHistory } = payload;
        
        console.log(`[CDSSAgent] Checking prescription against patient history for interactions...`);
        
        const prompt = `
            Check this new prescription: "${currentPrescription}"
            Against patient history: "${patientHistory}"
            Return a JSON object: { "hasWarning": boolean, "warningLevel": "LOW/MEDIUM/HIGH/CRITICAL", "message": "..." }
        `;

        const llmResponse = await this.mockLLMCall(prompt, this.responseMocks);
        
        try {
            const result = JSON.parse(llmResponse);
            if (result.hasWarning) {
                console.log(`[CDSSAgent] ⚠️ ${result.warningLevel} WARNING: ${result.message}`);
            } else {
                console.log(`[CDSSAgent] Safe to prescribe.`);
            }
            return result;
        } catch (e) {
            console.error('[CDSSAgent] Failed to parse LLM response', e);
            return { hasWarning: false };
        }
    }
}

module.exports = new CDSSAgent();
