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
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
"""Clinical Guidelines Provider for USPSTF, CDC, and AHRQ guidelines."""

import logging
from typing import List, Dict, Any, Callable
import httpx

from core.base_provider import BaseProvider
from core.decorators import safe_json_return
from providers import register_provider

logger = logging.getLogger(__name__)

# Evidence-based preventive care guidelines
PREVENTIVE_GUIDELINES = {
    "diabetes_screening": [
        {
            "title": "Type 2 Diabetes Screening",
            "recommendation": "Screen for prediabetes and type 2 diabetes in adults aged 35 to 70 years who have overweight or obesity",
            "grade": "B",
            "age_range": "35-70 years",
            "frequency": "Every 3 years if normal",
            "source": "USPSTF",
            "conditions": ["overweight", "obesity"],
            "citation": "USPSTF 2021"
        }
    ],
    "colorectal_screening": [
        {
            "title": "Colorectal Cancer Screening",
            "recommendation": "Screen for colorectal cancer starting at age 45 years",
            "grade": "A",
            "age_range": "45-75 years",
            "frequency": "Every 10 years (colonoscopy) or annually (FIT)",
            "source": "USPSTF",
            "methods": ["Colonoscopy every 10 years", "FIT annually", "CT colonography every 5 years"],
            "citation": "USPSTF 2021"
        }
    ],
    "hypertension_screening": [
        {
            "title": "Hypertension Screening",
            "recommendation": "Screen for high blood pressure in adults 18 years or older",
            "grade": "A",
            "age_range": "18+ years",
            "frequency": "Annually for adults 40+ or at increased risk; every 3-5 years for adults 18-39 with normal BP",
            "source": "USPSTF",
            "citation": "USPSTF 2021"
        }
    ],
    "cholesterol_screening": [
        {
            "title": "Statin Use for Primary Prevention of CVD",
            "recommendation": "Screen for cardiovascular disease risk factors beginning at age 40",
            "grade": "B",
            "age_range": "40-75 years",
            "frequency": "Every 4-6 years",
            "source": "USPSTF",
            "conditions": ["1+ CVD risk factors"],
            "citation": "USPSTF 2016"
        }
    ]
}

# CDC vaccine schedule recommendations
VACCINE_SCHEDULE = {
    "influenza": {
        "vaccine": "Influenza (Flu)",
        "recommendation": "Annual vaccination for all persons aged 6 months and older",
        "age_groups": ["6 months+"],
        "frequency": "Annually",
        "source": "CDC",
        "priority_groups": ["65+", "Pregnant", "Chronic conditions"]
    },
    "covid19": {
        "vaccine": "COVID-19",
        "recommendation": "Vaccination recommended for all persons 6 months and older",
        "age_groups": ["6 months+"],
        "frequency": "Updated annually",
        "source": "CDC",
        "priority_groups": ["65+", "Immunocompromised", "Healthcare workers"]
    },
    "pneumococcal": {
        "vaccine": "Pneumococcal (PCV/PPSV)",
        "recommendation": "Vaccination for adults 65 years or older and younger adults with certain conditions",
        "age_groups": ["65+", "19-64 with risk factors"],
        "frequency": "One-time or based on risk",
        "source": "CDC",
        "conditions": ["Chronic heart/lung disease", "Diabetes", "Immunocompromised"]
    },
    "shingles": {
        "vaccine": "Shingles (Zoster)",
        "recommendation": "2-dose series for adults 50 years and older",
        "age_groups": ["50+"],
        "frequency": "2 doses, 2-6 months apart",
        "source": "CDC"
    },
    "tdap": {
        "vaccine": "Tdap (Tetanus, Diphtheria, Pertussis)",
        "recommendation": "One dose of Tdap, then Td booster every 10 years",
        "age_groups": ["All adults"],
        "frequency": "Every 10 years",
        "source": "CDC"
    },
    "hepatitis_b": {
        "vaccine": "Hepatitis B",
        "recommendation": "Vaccination for adults at increased risk",
        "age_groups": ["19-59 years (universal)", "60+ with risk factors"],
        "frequency": "2 or 3-dose series",
        "source": "CDC",
        "risk_factors": ["Healthcare workers", "Diabetes", "Chronic liver disease"]
    }
}


@register_provider("clinical_guidelines")
class ClinicalGuidelinesProvider(BaseProvider):
    """Provider for clinical guidelines and preventive care recommendations."""

    def __init__(self, client: httpx.AsyncClient):
        super().__init__("clinical_guidelines", client)

    async def initialize(self) -> None:
        """Initialize clinical guidelines provider."""
        logger.info("Clinical Guidelines provider initialized")

    def get_tools(self) -> List[Callable]:
        """Return all clinical guideline tools."""
        return [
            self.clinical_get_preventive_recommendations,
            self.clinical_get_vaccine_schedule,
            self.clinical_search_guidelines,
            self.clinical_get_lifestyle_recommendations,
            self.clinical_generate_care_plan,
        ]

    @safe_json_return
    async def clinical_get_preventive_recommendations(
        self,
        age: int,
        sex: str,
        conditions: List[str] = None
    ) -> Dict[str, Any]:
        """
        Get personalized preventive care recommendations based on USPSTF guidelines.

        Args:
            age: Patient age in years
            sex: Patient sex (male/female)
            conditions: List of existing conditions (e.g., ["diabetes", "overweight"])

        Returns:
            Evidence-based screening and prevention recommendations with grades
        """
        if conditions is None:
            conditions = []

        recommendations = []
        condition_lower = [c.lower() for c in conditions]

        # Diabetes screening
        if 35 <= age <= 70 and any(c in condition_lower for c in ["overweight", "obesity", "prediabetes"]):
            recommendations.append({
                **PREVENTIVE_GUIDELINES["diabetes_screening"][0],
                "applicable": True,
                "rationale": "Age and BMI criteria met"
            })

        # Colorectal cancer screening
        if 45 <= age <= 75:
            recommendations.append({
                **PREVENTIVE_GUIDELINES["colorectal_screening"][0],
                "applicable": True,
                "rationale": "Age-based screening recommendation"
            })

        # Hypertension screening
        if age >= 18:
            freq = "Annually" if age >= 40 else "Every 3-5 years"
            rec = PREVENTIVE_GUIDELINES["hypertension_screening"][0].copy()
            rec["frequency"] = freq
            rec["applicable"] = True
            rec["rationale"] = "Universal screening for adults"
            recommendations.append(rec)

        # Cholesterol/CVD screening
        if 40 <= age <= 75:
            recommendations.append({
                **PREVENTIVE_GUIDELINES["cholesterol_screening"][0],
                "applicable": True,
                "rationale": "Age-based cardiovascular risk screening"
            })

        # Sex-specific screenings
        if sex.lower() == "female":
            if 40 <= age <= 74:
                recommendations.append({
                    "title": "Breast Cancer Screening",
                    "recommendation": "Biennial screening mammography for women aged 40-74",
                    "grade": "B",
                    "age_range": "40-74 years",
                    "frequency": "Every 2 years",
                    "source": "USPSTF",
                    "applicable": True,
                    "citation": "USPSTF 2024"
                })
            if 21 <= age <= 65:
                recommendations.append({
                    "title": "Cervical Cancer Screening",
                    "recommendation": "Screening every 3 years with cytology (21-29) or every 5 years with HPV testing (30-65)",
                    "grade": "A",
                    "age_range": "21-65 years",
                    "frequency": "Every 3-5 years based on method",
                    "source": "USPSTF",
                    "applicable": True,
                    "citation": "USPSTF 2018"
                })
        elif sex.lower() == "male":
            if 55 <= age <= 80 and "smoking history" in condition_lower:
                recommendations.append({
                    "title": "Lung Cancer Screening",
                    "recommendation": "Annual screening with low-dose CT for adults 50-80 with 20 pack-year smoking history",
                    "grade": "B",
                    "age_range": "50-80 years",
                    "frequency": "Annually",
                    "source": "USPSTF",
                    "applicable": True,
                    "conditions": ["20 pack-year smoking history"],
                    "citation": "USPSTF 2021"
                })

        return {
            "patient_age": age,
            "patient_sex": sex,
            "conditions": conditions,
            "recommendations": recommendations,
            "total_recommendations": len(recommendations)
        }

    @safe_json_return
    async def clinical_get_vaccine_schedule(
        self,
        age: int,
        conditions: List[str] = None
    ) -> Dict[str, Any]:
        """
        Get personalized vaccine recommendations based on CDC guidelines.

        Args:
            age: Patient age in years
            conditions: List of health conditions

        Returns:
            Recommended vaccines with timing and priority
        """
        if conditions is None:
            conditions = []

        recommended_vaccines = []
        condition_lower = [c.lower() for c in conditions]

        # Universal vaccines
        recommended_vaccines.append({
            **VACCINE_SCHEDULE["influenza"],
            "due": "Annually, preferably before flu season (Sept-Oct)",
            "priority": "High" if age >= 65 or len(conditions) > 0 else "Standard"
        })

        recommended_vaccines.append({
            **VACCINE_SCHEDULE["covid19"],
            "due": "Check for updated formulation annually",
            "priority": "High" if age >= 65 else "Standard"
        })

        # Age-specific vaccines
        if age >= 65:
            recommended_vaccines.append({
                **VACCINE_SCHEDULE["pneumococcal"],
                "due": "If not previously vaccinated",
                "priority": "High"
            })

        if age >= 50:
            recommended_vaccines.append({
                **VACCINE_SCHEDULE["shingles"],
                "due": "2-dose series if not previously vaccinated",
                "priority": "High" if age >= 60 else "Standard"
            })

        # Condition-specific
        if any(c in condition_lower for c in ["diabetes", "heart disease", "lung disease", "immunocompromised"]):
            if age >= 19 and age < 65:
                recommended_vaccines.append({
                    **VACCINE_SCHEDULE["pneumococcal"],
                    "due": "Based on risk factors",
                    "priority": "High",
                    "rationale": "Chronic medical condition"
                })

        # Tdap (universal adult)
        recommended_vaccines.append({
            **VACCINE_SCHEDULE["tdap"],
            "due": "If not received in last 10 years",
            "priority": "Standard"
        })

        # Hepatitis B
        if 19 <= age <= 59:
            recommended_vaccines.append({
                **VACCINE_SCHEDULE["hepatitis_b"],
                "due": "If not previously vaccinated",
                "priority": "Standard"
            })

        return {
            "patient_age": age,
            "conditions": conditions,
            "recommended_vaccines": recommended_vaccines,
            "total_vaccines": len(recommended_vaccines)
        }

    @safe_json_return
    async def clinical_search_guidelines(
        self,
        query: str,
        source: str = "All"
    ) -> Dict[str, Any]:
        """
        Search clinical guidelines by condition or screening type.

        Args:
            query: Search term (condition, screening type, etc.)
            source: Filter by source (USPSTF, CDC, AHRQ, All)

        Returns:
            Matching guidelines with recommendations
        """
        query_lower = query.lower()
        results = []

        # Search preventive guidelines
        for key, guidelines in PREVENTIVE_GUIDELINES.items():
            if query_lower in key:
                results.extend(guidelines)
            else:
                for g in guidelines:
                    if query_lower in g.get("title", "").lower():
                        results.append(g)

        # Search vaccines
        for key, vaccine in VACCINE_SCHEDULE.items():
            if query_lower in key or query_lower in vaccine.get("vaccine", "").lower():
                results.append({
                    "title": vaccine["vaccine"],
                    "recommendation": vaccine["recommendation"],
                    "source": vaccine["source"],
                    "type": "Immunization",
                    "age_groups": vaccine.get("age_groups", []),
                    "frequency": vaccine.get("frequency", "")
                })

        # Filter by source if specified
        if source != "All":
            results = [r for r in results if r.get("source", "").upper() == source.upper()]

        return {
            "query": query,
            "source_filter": source,
            "results": results,
            "total": len(results)
        }

    @safe_json_return
    async def clinical_get_lifestyle_recommendations(
        self,
        conditions: List[str],
        age: int
    ) -> Dict[str, Any]:
        """
        Get evidence-based lifestyle and wellness recommendations.

        Args:
            conditions: List of health conditions
            age: Patient age

        Returns:
            Lifestyle modification recommendations
        """
        if conditions is None:
            conditions = []

        recommendations = []
        condition_lower = [c.lower() for c in conditions]

        # Universal recommendations
        recommendations.append({
            "category": "Physical Activity",
            "recommendation": "At least 150 minutes of moderate-intensity aerobic activity per week",
            "source": "CDC Physical Activity Guidelines",
            "evidence_grade": "A"
        })

        recommendations.append({
            "category": "Nutrition",
            "recommendation": "Mediterranean-style diet rich in fruits, vegetables, whole grains, and healthy fats",
            "source": "AHA Dietary Guidelines",
            "evidence_grade": "A"
        })

        recommendations.append({
            "category": "Sleep",
            "recommendation": "7-9 hours of sleep per night for adults",
            "source": "CDC Sleep Guidelines",
            "evidence_grade": "A"
        })

        # Condition-specific
        if "diabetes" in condition_lower:
            recommendations.append({
                "category": "Diabetes Management",
                "recommendation": "Monitor blood glucose regularly, aim for HbA1c <7% for most adults",
                "source": "ADA Standards of Care",
                "evidence_grade": "A",
                "specifics": ["Carbohydrate counting", "Regular foot examinations", "Annual eye exams"]
            })

        if "hypertension" in condition_lower:
            recommendations.append({
                "category": "Blood Pressure Management",
                "recommendation": "DASH diet, sodium restriction (<2300mg/day), regular BP monitoring",
                "source": "ACC/AHA Hypertension Guidelines",
                "evidence_grade": "A",
                "target": "BP <130/80 mmHg"
            })

        if any("heart" in c or "cardiac" in c or "cardiovascular" in c for c in condition_lower):
            recommendations.append({
                "category": "Cardiovascular Health",
                "recommendation": "Cardiac rehabilitation if eligible, cholesterol management, smoking cessation",
                "source": "AHA/ACC Guidelines",
                "evidence_grade": "A"
            })

        if "obesity" in condition_lower or "overweight" in condition_lower:
            recommendations.append({
                "category": "Weight Management",
                "recommendation": "5-10% weight loss through caloric reduction and increased physical activity",
                "source": "USPSTF Obesity Guidelines",
                "evidence_grade": "B"
            })

        return {
            "conditions": conditions,
            "patient_age": age,
            "recommendations": recommendations,
            "total": len(recommendations)
        }

    @safe_json_return
    async def clinical_generate_care_plan(
        self,
        age: int,
        sex: str,
        conditions: List[str] = None,
        current_medications: List[str] = None
    ) -> Dict[str, Any]:
        """
        Generate comprehensive 6-month care plan with screening, vaccines, and lifestyle.

        Args:
            age: Patient age
            sex: Patient sex
            conditions: Existing conditions
            current_medications: Current medication list

        Returns:
            Structured 6-month care plan
        """
        if conditions is None:
            conditions = []
        if current_medications is None:
            current_medications = []

        # Get all recommendation types
        preventive = await self.clinical_get_preventive_recommendations(age, sex, conditions)
        vaccines = await self.clinical_get_vaccine_schedule(age, conditions)
        lifestyle = await self.clinical_get_lifestyle_recommendations(conditions, age)

        # Build timeline
        care_plan = {
            "patient_profile": {
                "age": age,
                "sex": sex,
                "conditions": conditions,
                "current_medications": current_medications
            },
            "preventive_screenings": preventive.get("recommendations", []),
            "immunizations": vaccines.get("recommended_vaccines", []),
            "lifestyle_modifications": lifestyle.get("recommendations", []),
            "timeline": {
                "month_1": ["Schedule overdue screenings", "Update immunizations"],
                "month_2": ["Follow up on screening results", "Begin lifestyle modifications"],
                "month_3": ["Medication review with provider", "Assess lifestyle progress"],
                "month_6": ["Comprehensive health review", "Update care plan"]
            },
            "follow_up_schedule": {
                "primary_care": "Every 3-6 months" if conditions else "Annually",
                "specialist": "As indicated by conditions" if conditions else "Not required",
                "lab_work": "Per screening recommendations"
            }
        }

        return care_plan