# Path: dhis2_core/src/dhis2/e2b/common.py
# def date_format_102(dt: datetime) -> str:
#     return dt.strftime("%Y%m%d")
# 
# def date_format_203(dt: datetime) -> str:
#     return dt.strftime("%Y%m%d%H%M")
# 
# def date_format_204(dt: datetime) -> str:
#     return dt.strftime("%Y%m%d%H%M%S")
# 
# 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]
# 
#     if dv:
#         return dv.value
# 
#     return defaultValue
# 
# def get_patient_sex(te: TrackedEntity) -> str:
#     value = get_attribute_value("CklPZdOd6H1", te)
# 
#     if not value:
#         value = get_attribute_value("oindugucx72", te)
# 
#     if "MALE" == value:
#         return "1"
#     elif "FEMALE" == value:
#         return "2"
# 
#     return ""
# 
# def get_reaction_outcome(te: TrackedEntity):
#     value = get_data_value("yRrSDiR5v1M", te)
# 
#     if "Recovered/resolved" == value:
#         return "1"
#     elif "Recovering/resolving" == value:
#         return "2"
#     elif "Not recovered/not resolved" == value:
#         return "3"
#     elif "Recovered/resolved with sequelae" == value:
#         return "4"
#     elif "Died" == value or "Autopsy done" == value:
#         return "5"
#     elif "Unknown" == value:
#         return "6"
# 
#     return value
# 
# def get_yes_no(de: str, te: TrackedEntity, idx: int = 0):
#     dv: EventDataValue = get_data_value(de, te, idx)
# 
#     if "true" == dv:
#         return "1"
# 
#     return "2"
# 
# 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 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]] = []


import logging
from datetime import datetime
from uuid import uuid4
from lxml import etree
from lxml.builder import E
from .common import (
    date_format_102,
    date_format_203,
    date_format_204,
    get_attribute_value,
    get_data_value,
    get_patient_sex,
    get_reaction_outcome,
    get_yes_no,
)
from .models.e2b import Enrollment, TrackedEntity
    family_name = get_attribute_value("aW66s2QSosT", te)

    if given_name:
        name = given_name

        if family_name:
            name = f"{name} {family_name}"

        p.append(E.patientinitial(name))  # should we use name here or not?

    dt = get_attribute_value("BiTsLcJQ95V", te)

    if not dt:
        dt = get_attribute_value("NI0QRzJvQ0k", te)  # MU

    if dt:
        dt = datetime.fromisoformat(dt)
        dob = dt.strftime("%Y%m%d")
        p.append(E.patientbirthdateformat("102"))
        p.append(E.patientbirthdate(dob))

    p.append(E.patientsex(get_patient_sex(te)))

    if get_yes_no("VXdRoWQOBxG", te):
        p.append(
            E.medicalhistoryepisode(
                E.patientmedicalcomment(get_data_value("AfrWB2ofm7l", te) or ""),
            )
        )


# Path: dhis2_core/src/dhis2/e2b/common.py
# def date_format_102(dt: datetime) -> str:
#     return dt.strftime("%Y%m%d")
# 
# def date_format_203(dt: datetime) -> str:
#     return dt.strftime("%Y%m%d%H%M")
# 
# def date_format_204(dt: datetime) -> str:
#     return dt.strftime("%Y%m%d%H%M%S")
# 
# 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]
# 
#     if dv:
#         return dv.value
# 
#     return defaultValue
# 
# def get_patient_sex(te: TrackedEntity) -> str:
#     value = get_attribute_value("CklPZdOd6H1", te)
# 
#     if not value:
#         value = get_attribute_value("oindugucx72", te)
# 
#     if "MALE" == value:
#         return "1"
#     elif "FEMALE" == value:
#         return "2"
# 
#     return ""
# 
# def get_reaction_outcome(te: TrackedEntity):
#     value = get_data_value("yRrSDiR5v1M", te)
# 
#     if "Recovered/resolved" == value:
#         return "1"
#     elif "Recovering/resolving" == value:
#         return "2"
#     elif "Not recovered/not resolved" == value:
#         return "3"
#     elif "Recovered/resolved with sequelae" == value:
#         return "4"
#     elif "Died" == value or "Autopsy done" == value:
#         return "5"
#     elif "Unknown" == value:
#         return "6"
# 
#     return value
# 
# def get_yes_no(de: str, te: TrackedEntity, idx: int = 0):
#     dv: EventDataValue = get_data_value(de, te, idx)
# 
#     if "true" == dv:
#         return "1"
# 
#     return "2"
# 
# 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 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/e2b_resources.py
import logging
from datetime import datetime
from uuid import uuid4
from lxml import etree
from lxml.builder import E
from .common import (
    date_format_102,
    date_format_203,
    date_format_204,
    get_attribute_value,
    get_data_value,
    get_patient_sex,
    get_reaction_outcome,
    get_yes_no,
)
from .models.e2b import Enrollment, TrackedEntity

    family_name = get_attribute_value("aW66s2QSosT", te)

    if given_name:
        name = given_name

        if family_name:
            name = f"{name} {family_name}"

        p.append(E.patientinitial(name))  # should we use name here or not?

    dt = get_attribute_value("BiTsLcJQ95V", te)

    if not dt:
        dt = get_attribute_value("NI0QRzJvQ0k", te)  # MU

    if dt:
        dt = datetime.fromisoformat(dt)
        dob = dt.strftime("%Y%m%d")
        p.append(E.patientbirthdateformat("102"))
        p.append(E.patientbirthdate(dob))

    p.append(E.patientsex(get_patient_sex(te)))

    if get_yes_no("VXdRoWQOBxG", te):
        p.append(
            E.medicalhistoryepisode(
                E.patientmedicalcomment(get_data_value("AfrWB2ofm7l", te) or ""),
            )
        )


    dead = get_yes_no("DOA6ZFMro84", te) == "1"