# Path: dhis2_core/src/dhis2/e2b/models/e2b.py
# class Enrollment(BaseModel):
#     created: datetime
#     lastUpdated: datetime
#     enrollment: str
#     trackedEntityInstance: str
#     orgUnit: str
#     storedBy: Optional[str]
#     status: str
#     completedDate: Optional[str]
#     events: List[Event] = []
#     attributes: Union[Dict[str, AttributeValue], List[AttributeValue]] = []
# 
# class Event(BaseModel):
#     created: datetime
#     lastUpdated: datetime
#     event: str
#     program: str
#     programStage: str
#     trackedEntityInstance: str
#     orgUnit: str
#     status: str
#     dueDate: datetime
#     eventDate: datetime
#     completedDate: Optional[str]
#     storedBy: Optional[str]
#     dataValues: Union[Dict[str, EventDataValue], List[EventDataValue]] = []
# 
# class EventDataValue(BaseModel):
#     created: datetime
#     lastUpdated: datetime
#     dataElement: str
#     value: str
#     providedElsewhere: bool
#     storedBy: Optional[str]
# 
# class TrackedEntity(BaseModel):
#     created: datetime
#     lastUpdated: datetime
#     trackedEntityInstance: str
#     trackedEntityType: str
#     orgUnit: str
#     storedBy: Optional[str]
#     enrollments: List[Enrollment] = []
#     attributes: Union[Dict[str, AttributeValue], List[AttributeValue]] = []


from datetime import datetime
from typing import Union
from .models.e2b import Enrollment, Event, EventDataValue, TrackedEntity
    if de not in ev.dataValues:
        return defaultValue

    dv: EventDataValue = ev.dataValues[de]

    if dv:
        return dv.value

    return defaultValue


def get_patient_age(te: TrackedEntity):
    value = get_attribute_value("BiTsLcJQ95V", te)
    dt = datetime.fromisoformat(value)
    now = datetime.now()

    year = now.year - dt.year

    if year > 0:
        return ("801", str(year))

    months = now.month - dt.month

    if months > 0:
        return ("802", str(months))

    return ("804", str(now.day - dt.day))


def get_yes_no(de: str, te: TrackedEntity, idx: int = 0):

# Path: dhis2_core/src/dhis2/e2b/models/e2b.py
# class Enrollment(BaseModel):
#     created: datetime
#     lastUpdated: datetime
#     enrollment: str
#     trackedEntityInstance: str
#     orgUnit: str
#     storedBy: Optional[str]
#     status: str
#     completedDate: Optional[str]
#     events: List[Event] = []
#     attributes: Union[Dict[str, AttributeValue], List[AttributeValue]] = []
# 
# class Event(BaseModel):
#     created: datetime
#     lastUpdated: datetime
#     event: str
#     program: str
#     programStage: str
#     trackedEntityInstance: str
#     orgUnit: str
#     status: str
#     dueDate: datetime
#     eventDate: datetime
#     completedDate: Optional[str]
#     storedBy: Optional[str]
#     dataValues: Union[Dict[str, EventDataValue], List[EventDataValue]] = []
# 
# class EventDataValue(BaseModel):
#     created: datetime
#     lastUpdated: datetime
#     dataElement: str
#     value: str
#     providedElsewhere: bool
#     storedBy: Optional[str]
# 
# class TrackedEntity(BaseModel):
#     created: datetime
#     lastUpdated: datetime
#     trackedEntityInstance: str
#     trackedEntityType: str
#     orgUnit: str
#     storedBy: Optional[str]
#     enrollments: List[Enrollment] = []
#     attributes: Union[Dict[str, AttributeValue], List[AttributeValue]] = []


# Path: dhis2_core/src/dhis2/e2b/common.py
from datetime import datetime
from typing import Union
from .models.e2b import Enrollment, Event, EventDataValue, TrackedEntity

    if de not in ev.dataValues:
        return defaultValue

    dv: EventDataValue = ev.dataValues[de]

    if dv:
        return dv.value

    return defaultValue


def get_patient_age(te: TrackedEntity):
    value = get_attribute_value("BiTsLcJQ95V", te)
    dt = datetime.fromisoformat(value)
    now = datetime.now()

    year = now.year - dt.year

    if year > 0:
        return ("801", str(year))

    months = now.month - dt.month

    if months > 0:
        return ("802", str(months))

    return ("804", str(now.day - dt.day))


def get_yes_no(de: str, te: TrackedEntity, idx: int = 0):

    dv: EventDataValue = get_data_value(de, te, idx)