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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
"""
COMPREHENSIVE MEDICAL ANALYSIS PROMPT ENGINEERING FRAMEWORK
Creates meaningful clinical insights across ALL medical categories

This file provides enhanced prompt templates and structured output schemas for:
- Cardiology/ECG Analysis
- Radiology (X-ray, CT, MRI, Ultrasound)
- Laboratory Medicine
- Pathology (Biopsies, Cytology)
- Clinical Documentation
- General Medical Analysis

Each prompt includes:
1. Domain-specific clinical context
2. Structured diagnostic framework
3. Professional medical terminology
4. Evidence-based recommendations
5. Clinical correlation requirements
"""

import json
from typing import Dict, Any, List
from datetime import datetime

class ComprehensiveMedicalPromptEngine:
    """
    Enhanced medical prompt engineering for meaningful clinical insights
    across all medical categories
    """
    
    def __init__(self):
        self.medical_domains = self._initialize_medical_domains()
        self.output_schemas = self._initialize_output_schemas()
    
    def _initialize_medical_domains(self) -> Dict[str, Dict[str, Any]]:
        """Initialize comprehensive medical analysis domains with enhanced prompts"""
        return {
            # ===== CARDIOLOGY/ECG ANALYSIS =====
            "cardiology_ecg": {
                "clinical_context": """You are a board-certified cardiologist specializing in electrocardiography and cardiac electrophysiology. 
Provide comprehensive ECG analysis with clinical expertise.""",
                
                "prompt_template": """CLINICAL SCENARIO: Comprehensive Electrocardiogram Analysis
Patient Context: {patient_info}
ECG Data: {ecg_data}

Provide detailed cardiac electrophysiology assessment including:

1. **RHYTHM ANALYSIS & CARDIAC RATE**
   - Primary rhythm identification with confidence assessment
   - Heart rate analysis with normal/abnormal range determination
   - Sinus rhythm characteristics and any arrhythmic patterns

2. **CONDUCTION SYSTEM ASSESSMENT**
   - P wave morphology, duration, and timing analysis
   - PR interval interpretation (normal, prolonged, shortened)
   - QRS complex analysis (duration, morphology, axis determination)
   - QT/QTc interval measurement with clinical significance

3. **MYOCARDIAL ISCHEMIA/INFARCTION DETECTION**
   - ST-segment elevation/depression analysis with lead distribution
   - T wave abnormalities and their clinical significance
   - Q wave presence indicating prior infarction territory
   - Pathological Q waves vs physiological variants

4. **CLINICAL CORRELATION & RECOMMENDATIONS**
   - Risk stratification based on ECG findings
   - Correlation with clinical presentation and cardiac biomarkers
   - Evidence-based management recommendations
   - Follow-up testing requirements (stress test, echo, catheterization)

Provide professional cardiac electrophysiology interpretation.""",
                
                "domain_expertise": "Cardiology, Electrophysiology, Clinical Medicine",
                "critical_elements": ["rhythm", "intervals", "ischemia", "axis", "recommendations"]
            },
            
            # ===== RADIOLOGY ANALYSIS =====
            "radiology_xray": {
                "clinical_context": """You are a board-certified radiologist specializing in diagnostic imaging interpretation.
Provide comprehensive radiological analysis with clinical expertise.""",
                
                "prompt_template": """CLINICAL SCENARIO: Comprehensive Radiological Analysis
Imaging Study: {imaging_study}
Patient Context: {patient_context}
Radiological Data: {image_data}

Provide detailed radiological interpretation including:

1. **TECHNICAL ASSESSMENT**
   - Imaging modality and acquisition parameters
   - Image quality and technical adequacy
   - Anatomical coverage and positioning

2. **ANATOMICAL & PATHOLOGICAL FINDINGS**
   - Systematic review of anatomical structures
   - Primary pathological findings with specific descriptions
   - Differential diagnosis considerations based on imaging patterns
   - Critical findings requiring urgent attention

3. **CLINICAL SIGNIFICANCE & INTERPRETATION**
   - Correlation with clinical presentation and symptoms
   - Severity assessment and prognostic implications
   - Disease progression or treatment response indicators
   - Comparison with prior imaging when available

4. **MANAGEMENT RECOMMENDATIONS**
   - Additional imaging studies if indicated
   - Clinical correlation requirements
   - Treatment implications and monitoring needs
   - Follow-up imaging recommendations

Provide expert radiological consultation with clinical correlation.""",
                
                "domain_expertise": "Radiology, Diagnostic Imaging, Clinical Medicine",
                "critical_elements": ["findings", "differential", "severity", "recommendations"]
            },
            
            "radiology_ct": {
                "clinical_context": """You are a board-certified radiologist specializing in CT imaging and cross-sectional anatomy.
Provide comprehensive CT analysis with clinical expertise.""",
                
                "prompt_template": """CLINICAL SCENARIO: Comprehensive CT Scan Analysis
CT Study: {ct_study}
Clinical Indication: {indication}
Patient Context: {patient_context}

Provide detailed CT interpretation including:

1. **TECHNICAL ASSESSMENT**
   - CT protocol and imaging parameters
   - Contrast enhancement status
   - Image quality and diagnostic adequacy

2. **SYSTEMATIC ORGAN REVIEW**
   - Brain/nervous system (if head CT)
   - Chest (lung parenchyma, mediastinum, pleural spaces)
   - Abdomen/pelvis (solid organs, bowel, vasculature)
   - Musculoskeletal structures as applicable

3. **PATHOLOGICAL FINDINGS & INTERPRETATION**
   - Primary lesion characterization (size, location, enhancement)
   - Secondary findings and metastatic assessment
   - Inflammatory, infectious, or neoplastic processes
   - Vascular abnormalities and perfusion deficits

4. **CLINICAL CORRELATION & RECOMMENDATIONS**
   - Findings correlation with clinical presentation
   - Differential diagnosis with probability assessment
   - Tissue sampling recommendations if indicated
   - Treatment planning and monitoring protocols

Provide expert CT interpretation with clinical management guidance.""",
                
                "domain_expertise": "Radiology, Cross-sectional Imaging, Clinical Medicine",
                "critical_elements": ["systematic_review", "pathology", "differential", "management"]
            },
            
            # ===== LABORATORY MEDICINE =====
            "laboratory_chemistry": {
                "clinical_context": """You are a board-certified clinical pathologist specializing in laboratory medicine.
Provide comprehensive laboratory interpretation with clinical expertise.""",
                
                "prompt_template": """CLINICAL SCENARIO: Comprehensive Laboratory Analysis
Laboratory Data: {lab_data}
Patient Context: {patient_context}
Clinical Indication: {indication}

Provide detailed laboratory interpretation including:

1. **NORMAL & ABNORMAL VALUE ASSESSMENT**
   - Reference range comparison with flagging of abnormal values
   - Critical value identification requiring immediate attention
   - Trending analysis for serial measurements
   - Hemolysis, lipemia, or other specimen quality issues

2. **CLINICAL SIGNIFICANCE & CORRELATION**
   - Clinical interpretation of abnormal findings
   - Correlation with patient symptoms and medical history
   - Drug-induced laboratory abnormalities
   - Organ-specific findings (hepatic, renal, cardiac markers)

3. **DIAGNOSTIC & PROGNOSTIC IMPLICATIONS**
   - Disease diagnosis support based on laboratory patterns
   - Prognostic indicators and risk stratification
   - Treatment monitoring and therapeutic drug levels
   - Baseline and follow-up testing requirements

4. **RECOMMENDATIONS & CLINICAL ACTION**
   - Repeat testing requirements or confirmatory studies
   - Additional laboratory testing for diagnosis/monitoring
   - Clinical correlation with other diagnostic modalities
   - Specialist consultation recommendations

Provide expert clinical pathology interpretation.""",
                
                "domain_expertise": "Laboratory Medicine, Clinical Pathology, Clinical Medicine",
                "critical_elements": ["reference_ranges", "abnormalities", "significance", "recommendations"]
            },
            
            # ===== PATHOLOGY ANALYSIS =====
            "pathology_biopsy": {
                "clinical_context": """You are a board-certified pathologist specializing in histopathology and molecular pathology.
Provide comprehensive pathological analysis with clinical expertise.""",
                
                "prompt_template": """CLINICAL SCENARIO: Comprehensive Pathology Analysis
Specimen Type: {specimen_type}
Anatomical Site: {site}
Clinical Information: {clinical_info}
Histopathological Data: {path_data}

Provide detailed pathological interpretation including:

1. **MORPHOLOGICAL ASSESSMENT**
   - Specimen adequacy and diagnostic quality
   - Histological pattern analysis and architectural features
   - Cellular morphology, nuclear characteristics, and cytoplasmic features
   - Staining characteristics and immunohistochemical profile

2. **DIAGNOSTIC INTERPRETATION**
   - Primary diagnosis with confidence level
   - Differential diagnosis considerations
   - Grading and staging information when applicable
   - Molecular/immunohistochemical markers and their significance

3. **CLINICAL CORRELATION & PROGNOSIS**
   - Correlation with clinical presentation and imaging findings
   - Prognostic factors and risk stratification
   - Treatment response prediction and therapeutic targets
   - Genetic/molecular alterations with clinical implications

4. **CLINICAL MANAGEMENT RECOMMENDATIONS**
   - Surgical margin assessment and adequacy
   - Additional staining or molecular testing recommendations
   - Treatment planning implications
   - Follow-up protocols and surveillance recommendations

Provide expert pathological consultation with clinical management guidance.""",
                
                "domain_expertise": "Pathology, Histopathology, Molecular Pathology, Clinical Medicine",
                "critical_elements": ["morphology", "diagnosis", "prognosis", "management"]
            },
            
            # ===== CLINICAL DOCUMENTATION =====
            "clinical_notes": {
                "clinical_context": """You are an experienced clinical physician reviewing medical documentation.
Provide comprehensive clinical assessment with medical expertise.""",
                
                "prompt_template": """CLINICAL SCENARIO: Clinical Documentation Review
Medical Document: {document_data}
Document Type: {doc_type}
Patient Context: {patient_context}

Provide comprehensive clinical analysis including:

1. **DOCUMENT STRUCTURE & CONTENT ASSESSMENT**
   - Chief complaint and presenting problem analysis
   - History of present illness extraction and analysis
   - Past medical, surgical, and social history review
   - Physical examination findings and documentation quality

2. **CLINICAL ASSESSMENT & DIAGNOSTIC REASONING**
   - Primary assessment and clinical impression extraction
   - Differential diagnosis considerations and reasoning
   - Problem prioritization and clinical decision-making
   - Evidence-based diagnostic approach assessment

3. **TREATMENT PLAN & MANAGEMENT**
   - Therapeutic interventions and medication orders
   - Diagnostic testing recommendations and rationale
   - Follow-up care and monitoring protocols
   - Patient education and discharge planning

4. **CLINICAL QUALITY ASSURANCE**
   - Documentation completeness and accuracy
   - Clinical reasoning adequacy and decision-making quality
   - Standard of care compliance and best practice adherence
   - Areas for improvement and education recommendations

Provide professional clinical assessment and quality review.""",
                
                "domain_expertise": "Clinical Medicine, Internal Medicine, Medical Documentation",
                "critical_elements": ["assessment", "reasoning", "management", "quality"]
            },
            
            # ===== EMERGENCY MEDICINE =====
            "emergency_medicine": {
                "clinical_context": """You are an emergency medicine physician specializing in acute care assessment.
Provide comprehensive emergency medicine evaluation with clinical expertise.""",
                
                "prompt_template": """CLINICAL SCENARIO: Emergency Medicine Assessment
Emergency Presentation: {emergency_data}
Patient Context: {patient_context}
Clinical Scenario: {scenario}

Provide comprehensive emergency medicine evaluation including:

1. **ACUTE PRESENTATION ASSESSMENT**
   - Chief complaint and triage priority assessment
   - Vital signs analysis and stability determination
   - Acute symptom progression and severity
   - Risk stratification and immediate threats

2. **EMERGENCY CLINICAL FINDINGS**
   - Critical diagnostic findings requiring immediate attention
   - Organ system dysfunction assessment
   - Pain assessment and management needs
   - Environmental and trauma considerations

3. **EMERGENCY MANAGEMENT PROTOCOL**
   - Immediate life-saving interventions required
   - Diagnostic testing priorities (CT, labs, ECG)
   - Specialist consultation requirements
   - Admission vs discharge decisions

4. **DISPOSITION & FOLLOW-UP**
   - Admission criteria and level of care determination
   - Outpatient follow-up requirements
   - Patient education and discharge instructions
   - Emergency re-evaluation triggers

Provide expert emergency medicine consultation with acute care protocols.""",
                
                "domain_expertise": "Emergency Medicine, Acute Care, Critical Care",
                "critical_elements": ["triage", "critical_findings", "management", "disposition"]
            }
        }
    
    def _initialize_output_schemas(self) -> Dict[str, Dict[str, Any]]:
        """Initialize structured output schemas for each medical domain"""
        return {
            "cardiology_ecg": {
                "required_fields": [
                    "rhythm_analysis", "heart_rate", "conduction_intervals", 
                    "ischemia_findings", "clinical_significance", "recommendations"
                ],
                "output_structure": {
                    "rhythm_analysis": "Primary rhythm identification and characteristics",
                    "heart_rate": "Rate analysis with clinical interpretation",
                    "conduction_intervals": "PR, QRS, QT intervals with significance",
                    "ischemia_findings": "ST-T changes, Q waves, infarct location",
                    "clinical_significance": "Risk assessment and correlation",
                    "recommendations": "Evidence-based management and follow-up"
                }
            },
            
            "radiology_xray": {
                "required_fields": [
                    "technical_assessment", "anatomical_findings", "pathological_lesions",
                    "differential_diagnosis", "clinical_correlation", "recommendations"
                ],
                "output_structure": {
                    "technical_assessment": "Image quality, positioning, adequacy",
                    "anatomical_findings": "Systematic review of structures",
                    "pathological_lesions": "Primary findings with descriptions",
                    "differential_diagnosis": "List of possibilities with rationale",
                    "clinical_correlation": "Symptom correlation and significance",
                    "recommendations": "Additional imaging, treatment, follow-up"
                }
            },
            
            "laboratory_chemistry": {
                "required_fields": [
                    "normal_values", "abnormal_values", "critical_values",
                    "clinical_significance", "trending_analysis", "recommendations"
                ],
                "output_structure": {
                    "normal_values": "Results within reference range",
                    "abnormal_values": "Out of range results with interpretation",
                    "critical_values": "Life-threatening values requiring action",
                    "clinical_significance": "Interpretation of abnormalities",
                    "trending_analysis": "Pattern recognition and changes",
                    "recommendations": "Repeat testing, additional studies"
                }
            },
            
            "pathology_biopsy": {
                "required_fields": [
                    "morphological_assessment", "diagnostic_interpretation",
                    "grading_staging", "prognosis_factors", "treatment_implications"
                ],
                "output_structure": {
                    "morphological_assessment": "Histological pattern and cellular features",
                    "diagnostic_interpretation": "Primary diagnosis and differential",
                    "grading_staging": "Severity assessment and classification",
                    "prognosis_factors": "Risk factors and outcome prediction",
                    "treatment_implications": "Therapeutic targets and monitoring"
                }
            },
            
            "clinical_notes": {
                "required_fields": [
                    "chief_complaint", "assessment_plan", "clinical_reasoning",
                    "diagnostic_approach", "treatment_recommendations", "documentation_quality"
                ],
                "output_structure": {
                    "chief_complaint": "Primary problem and presentation",
                    "assessment_plan": "Clinical impression and assessment",
                    "clinical_reasoning": "Diagnostic logic and decision-making",
                    "diagnostic_approach": "Testing and evaluation strategy",
                    "treatment_recommendations": "Therapeutic interventions",
                    "documentation_quality": "Completeness and accuracy assessment"
                }
            }
        }
    
    def generate_enhanced_prompt(self, domain: str, data: Dict[str, Any]) -> str:
        """Generate enhanced medical prompt for specific domain"""
        if domain not in self.medical_domains:
            return self._generate_general_medical_prompt(data)
        
        domain_config = self.medical_domains[domain]
        prompt_template = domain_config["prompt_template"]
        
        # Format the prompt with available data
        formatted_prompt = prompt_template.format(**data)
        
        # Add structured output requirements
        if domain in self.output_schemas:
            schema = self.output_schemas[domain]
            formatted_prompt += f"""

STRUCTURED OUTPUT REQUIREMENTS:
Provide your analysis in the following structured format:

"""
            for field in schema["required_fields"]:
                formatted_prompt += f"• **{field.replace('_', ' ').title()}**: [Detailed analysis]\n"
        
        # Add clinical correlation requirement
        formatted_prompt += """

CLINICAL CORRELATION REQUIREMENTS:
1. Correlate all findings with patient presentation and medical context
2. Use professional medical terminology and clinical expertise
3. Provide evidence-based recommendations with clinical justification
4. Include risk stratification and management priorities
5. Suggest appropriate follow-up and monitoring protocols

Professional medical analysis with clinical correlation required."""
        
        return formatted_prompt
    
    def _generate_general_medical_prompt(self, data: Dict[str, Any]) -> str:
        """Generate general medical prompt for unspecified domains"""
        return f"""CLINICAL SCENARIO: Comprehensive Medical Document Analysis
Medical Information: {data.get('text', 'N/A')}

Provide comprehensive medical analysis including:

1. **CLINICAL FINDINGS**
   - Key medical findings and interpretations
   - Diagnostic considerations with clinical significance
   - Correlation with medical standards and guidelines

2. **CLINICAL ASSESSMENT**
   - Overall clinical impression and interpretation
   - Risk factors and prognostic indicators
   - Clinical decision-making guidance

3. **MANAGEMENT RECOMMENDATIONS**
   - Evidence-based treatment considerations
   - Follow-up protocols and monitoring requirements
   - Specialist consultation recommendations

4. **CLINICAL CORRELATION**
   - Integration with patient presentation and medical history
   - Quality assessment and clinical standards compliance

Provide expert medical consultation with clinical correlation and professional medical interpretation."""

    def get_domain_expertise(self, domain: str) -> str:
        """Get domain-specific medical expertise context"""
        return self.medical_domains.get(domain, {}).get("domain_expertise", "Clinical Medicine")
    
    def get_critical_elements(self, domain: str) -> List[str]:
        """Get critical analysis elements for domain"""
        return self.medical_domains.get(domain, {}).get("critical_elements", ["findings", "significance", "recommendations"])

# Global instance for medical analysis
medical_prompt_engine = ComprehensiveMedicalPromptEngine()

def generate_medical_analysis_prompt(domain: str, data: Dict[str, Any]) -> str:
    """Generate enhanced medical analysis prompt"""
    return medical_prompt_engine.generate_enhanced_prompt(domain, data)

def get_medical_domain_expertise(domain: str) -> str:
    """Get medical domain expertise context"""
    return medical_prompt_engine.get_domain_expertise(domain)