File size: 2,754 Bytes
ee4ef53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
from typing import Dict, Any, List
from config import settings
from schema import PetProfile

logger = logging.getLogger(__name__)

class RulesEngine:

    @staticmethod
    def create_engine() -> "RulesEngine":
        return RulesEngine()

    def __init__(self):
        pass

    def apply_rules(self, profile: PetProfile, nutrition_targets: Dict[str, Any]) -> Dict[str, Any]:
        adjustments = {}

        for rule_name, rule_config in settings.NUTRITION_RULES.items():
            if self._evaluate_condition(profile, rule_config["condition"]):
                adjustments.update(rule_config["adjustments"])

        logger.info(f"Applied {len(adjustments)} rules for pet profile")
        return adjustments

    def _evaluate_condition(self, profile: PetProfile, condition: Dict[str, Any]) -> bool:
        for key, value in condition.items():
            if key == "life_stage":
                if profile.life_stage.value != value:
                    return False
            elif key == "neutered_spayed":
                if profile.neutered_spayed.value != value:
                    return False
            elif key == "activity_level":
                if profile.activity_level.value != value:
                    return False
            elif key == "pet_type":
                if profile.pet_type.value != value:
                    return False
            elif key == "bcs":
                if isinstance(value, dict):
                    if "lt" in value and profile.bcs >= value["lt"]:
                        return False
                    if "gt" in value and profile.bcs <= value["gt"]:
                        return False
                elif profile.bcs != value:
                    return False
        return True

    def validate_plan_safety(self, profile: PetProfile, targets: Dict[str, Any]) -> List[str]:
        warnings = []

        # Safety check 1: Energy limits
        if targets.get("mer", 0) > 5000:
            warnings.append("Very high energy requirement - consult veterinarian")

        # Safety check 2: Protein limits
        if targets.get("protein_g", 0) > 200:
            warnings.append("High protein recommendation - ensure adequate water intake")

        # Safety check 3: nutrition ratios
        ca = targets.get("calcium_mg", 0)
        p = targets.get("phosphorus_mg", 0)
        if p > 0 and (ca / p) < 1.0:
            warnings.append("Calcium:Phosphorus ratio may be suboptimal")

        # Safety check 4: BCS considerations
        if profile.bcs < 3:
            warnings.append("Very underweight pet - gradual refeeding required")
        elif profile.bcs > 7:
            warnings.append("Obese pet - weight management plan essential")

        return warnings