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

class RxVisionAgent extends BaseAgent {
    constructor() {
        super('RxVisionAgent');
        this.responseMocks = {
            'default': JSON.stringify({
                medications: [
                    { name: 'Amoxicillin', dosage: '500mg', frequency: '1-0-1', duration: '5 days' },
                    { name: 'Paracetamol', dosage: '650mg', frequency: '1-1-1', duration: '3 days' }
                ],
                doctorNotes: 'Drink plenty of fluids. Rest for 3 days.',
                nextFollowUp: '2026-06-02'
            })
        };
    }

    async execute(config, payload) {
        const { imageUrl } = payload;
        
        console.log(`[RxVisionAgent] Processing prescription image: ${imageUrl}`);
        
        const prompt = `
            Act as an expert medical OCR system. Analyze the provided image of a handwritten prescription.
            Extract the medications, dosages, frequencies, and duration. 
            Also extract any general doctor notes and the next follow-up date.
            Return a JSON object: { "medications": [{ "name", "dosage", "frequency", "duration" }], "doctorNotes": "...", "nextFollowUp": "YYYY-MM-DD" }
        `;

        // Simulate sending image to Vision API
        const llmResponse = await this.mockLLMCall(prompt, this.responseMocks);
        
        try {
            const result = JSON.parse(llmResponse);
            console.log(`[RxVisionAgent] Successfully extracted ${result.medications.length} medications.`);
            return result;
        } catch (e) {
            console.error('[RxVisionAgent] Failed to parse Vision LLM response', e);
            return { medications: [], doctorNotes: '', nextFollowUp: null };
        }
    }
}

module.exports = new RxVisionAgent();
