# 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

    if dilutent:
        drug.append(E.drugadditional(", ".join(dilutent)))


def build_safetyreport_patient_drugs(root: etree.Element, te: TrackedEntity):
    vaccine1_name = get_data_value("uSVcZzSM3zg", te)
    vaccine1_brand = get_data_value("JSd0HQOgJ8w", te)
    vaccine1_date = get_data_value("dOkuCjpD978", te)
    vaccine1_time = get_data_value("BSUncNBb20j", te, defaultValue="00:00")
    vaccine1_batch = get_data_value("LNqkAlvGplL", te)
    vaccine1_dose = get_data_value("LIyV4t7eCfZ", te)
    vaccine1_expiry = get_data_value("VFrc8SNFYm7", te)
    vaccine2_name = get_data_value("g9PjywVj2fs", te)
    vaccine2_brand = get_data_value("eRwc8Y0CNLh", te)
    vaccine2_date = get_data_value("VrzEutEnzSJ", te)
    vaccine2_time = get_data_value("fZFQVZFqu0q", te, defaultValue="00:00")
    vaccine2_batch = get_data_value("b1rSwGRcY5W", te)
    vaccine2_dose = get_data_value("E3F414izniN", te)
    vaccine2_expiry = get_data_value("rVUo2PBgwhr", te)
    vaccine3_name = get_data_value("OU5klvkk3SM", te)
    vaccine3_brand = get_data_value("wdZrkUvnuyr", te)
    vaccine3_date = get_data_value("f4WCAVwjHz0", te)
    vaccine3_time = get_data_value("VQKdZ1KeD7u", te, defaultValue="00:00")
    vaccine3_batch = get_data_value("YBnFoNouH6f", te)
    vaccine3_dose = get_data_value("WlE0K4xCc14", te)
    vaccine3_expiry = get_data_value("ffYfdSPmM1W", te)
    vaccine4_name = get_data_value("menOXwIFZh5", te)
    vaccine4_brand = get_data_value("Ptms0lmt4QX", te)
    vaccine4_date = get_data_value("H3TKHMFIN6V", te)

# 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


    if dilutent:
        drug.append(E.drugadditional(", ".join(dilutent)))


def build_safetyreport_patient_drugs(root: etree.Element, te: TrackedEntity):
    vaccine1_name = get_data_value("uSVcZzSM3zg", te)
    vaccine1_brand = get_data_value("JSd0HQOgJ8w", te)
    vaccine1_date = get_data_value("dOkuCjpD978", te)
    vaccine1_time = get_data_value("BSUncNBb20j", te, defaultValue="00:00")
    vaccine1_batch = get_data_value("LNqkAlvGplL", te)
    vaccine1_dose = get_data_value("LIyV4t7eCfZ", te)
    vaccine1_expiry = get_data_value("VFrc8SNFYm7", te)
    vaccine2_name = get_data_value("g9PjywVj2fs", te)
    vaccine2_brand = get_data_value("eRwc8Y0CNLh", te)
    vaccine2_date = get_data_value("VrzEutEnzSJ", te)
    vaccine2_time = get_data_value("fZFQVZFqu0q", te, defaultValue="00:00")
    vaccine2_batch = get_data_value("b1rSwGRcY5W", te)
    vaccine2_dose = get_data_value("E3F414izniN", te)
    vaccine2_expiry = get_data_value("rVUo2PBgwhr", te)
    vaccine3_name = get_data_value("OU5klvkk3SM", te)
    vaccine3_brand = get_data_value("wdZrkUvnuyr", te)
    vaccine3_date = get_data_value("f4WCAVwjHz0", te)
    vaccine3_time = get_data_value("VQKdZ1KeD7u", te, defaultValue="00:00")
    vaccine3_batch = get_data_value("YBnFoNouH6f", te)
    vaccine3_dose = get_data_value("WlE0K4xCc14", te)
    vaccine3_expiry = get_data_value("ffYfdSPmM1W", te)
    vaccine4_name = get_data_value("menOXwIFZh5", te)
    vaccine4_brand = get_data_value("Ptms0lmt4QX", te)
    vaccine4_date = get_data_value("H3TKHMFIN6V", te)

    vaccine4_time = get_data_value("S1PRFSk8Y9v", te, defaultValue="00:00")