# 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
    return dt.strftime("%Y%m%d")


def date_format_204(dt: datetime) -> str:
    return dt.strftime("%Y%m%d%H%M%S")


def date_format_203(dt: datetime) -> str:
    return dt.strftime("%Y%m%d%H%M")


def get_attribute_value(at: str, te: TrackedEntity, defaultValue=None) -> Union[str, None]:
    av = te.attributes.get(at, defaultValue)

    if not av:
        return defaultValue

    if av.value:
        return av.value

    return defaultValue


def get_data_value(de: str, te: TrackedEntity, idx: int = 0, defaultValue=None) -> Union[str, None]:
    en: Enrollment = te.enrollments[idx]
    ev: Event = en.events["so8YZ9J3MeO"]  # AEFI stage

    if de not in ev.dataValues:
        return defaultValue


# 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

    return dt.strftime("%Y%m%d")


def date_format_204(dt: datetime) -> str:
    return dt.strftime("%Y%m%d%H%M%S")


def date_format_203(dt: datetime) -> str:
    return dt.strftime("%Y%m%d%H%M")


def get_attribute_value(at: str, te: TrackedEntity, defaultValue=None) -> Union[str, None]:
    av = te.attributes.get(at, defaultValue)

    if not av:
        return defaultValue

    if av.value:
        return av.value

    return defaultValue


def get_data_value(de: str, te: TrackedEntity, idx: int = 0, defaultValue=None) -> Union[str, None]:
    en: Enrollment = te.enrollments[idx]
    ev: Event = en.events["so8YZ9J3MeO"]  # AEFI stage

    if de not in ev.dataValues:
        return defaultValue


    dv: EventDataValue = ev.dataValues[de]