# Path: dhis2_core/src/dhis2/e2b/e2b_resources.py
# def build_messageheader(root: etree.Element, sender_id: str, receiver_id: str):
#     mh = E.ichicsrmessageheader(
#         E.messagetype("ichicsr"),
#         E.messageformatversion("2.1"),
#         E.messageformatrelease("2.0"),
#         E.messagenumb(str(uuid4())),
#         E.messagesenderidentifier(sender_id),
#         E.messagereceiveridentifier(receiver_id),
#         E.messagedateformat("204"),
#         E.messagedate(date_format_204(datetime.now())),
#     )
# 
#     root.append(mh)
# 
# def build_safetyreport(
#     root: etree.Element,
#     te: TrackedEntity,
#     en: Enrollment,
#     country: str,
#     receiverorganization: str,
#     receivercountrycode: str,
# ):
#     sr = etree.SubElement(root, "safetyreport")
# 
#     id = get_attribute_value("h5FuguPFF2j", te)
# 
#     if country:
#         country = country.upper()
# 
#         if id:
#             id = f"{country}-{id}"
#         else:
#             id = f"{country}"
# 
#     sr.append(E.safetyreportversion("1"))
#     sr.append(E.safetyreportid(id))
#     sr.append(E.primarysourcecountry(country))
#     sr.append(E.occurcountry(country))
#     sr.append(E.transmissiondateformat("102"))
#     sr.append(E.transmissiondate(date_format_102(datetime.now())))
#     sr.append(E.reporttype("1"))
#     sr.append(E.serious(get_yes_no("fq1c1A3EOX5", te)))
# 
#     dead = get_yes_no("DOA6ZFMro84", te) == "1"
# 
#     if dead:
#         sr.append(E.seriousnessdeath("1"))
#     else:
#         sr.append(E.seriousnessdeath("2"))
# 
#     sr.append(E.seriousnesslifethreatening(get_yes_no("lATDYNmTLKD", te)))
#     sr.append(E.seriousnesshospitalization(get_yes_no("Il1lTfknLdd", te)))
#     sr.append(E.seriousnessdisabling(get_yes_no("lsO8n8ZmLAB", te)))
#     sr.append(E.seriousnesscongenitalanomali(get_yes_no("lSBsxcQU0kO", te)))
#     sr.append(E.seriousnessother(get_yes_no("tWcNgbkOETR", te)))
#     sr.append(E.receivedateformat("102"))
#     sr.append(E.receivedate(date_format_102(datetime.now())))
#     sr.append(E.receiptdateformat("102"))
#     sr.append(E.receiptdate(date_format_102(datetime.now())))
#     sr.append(E.additionaldocument("2"))
#     # sr.append(E.fulfillexpeditecriteria("1"))
# 
#     reporter_name = get_data_value("uZ9c4fKXuNS", te)
#     qualification = "1" if reporter_name.upper().startswith("DR ") else "3"
# 
#     sr.append(
#         E.primarysource(
#             E.reportergivename(reporter_name),
#             E.reporterorganization(get_data_value("Q20pEixZxCs", te)),  # TODO resolve org unit
#             E.qualification(qualification),
#         )
#     )
# 
#     sr.append(
#         E.sender(
#             E.senderorganization("DHIS2"),
#         )
#     )
# 
#     sr.append(
#         E.receiver(
#             E.receivertype("5"),
#             E.receiverorganization(receiverorganization),
#             E.receivercountrycode(receivercountrycode),
#         )
#     )
# 
#     build_safetyreport_patient(sr, te)
# 
# def print_root(root: etree.Element, pretty_print: bool = True):
#     print(
#         etree.tostring(
#             root,
#             pretty_print=pretty_print,
#             standalone=False,
#             encoding="UTF-8",
#             doctype='<!DOCTYPE ichicsr SYSTEM "http://eudravigilance.ema.europa.eu/dtd/icsr21xml.dtd">',
#         ).decode()
#     )
# 
# Path: dhis2_core/src/dhis2/e2b/models/e2b.py
# 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 typing import List
from lxml.builder import E
from .e2b_resources import build_messageheader, build_safetyreport, print_root
from .models.e2b import TrackedEntity



log = logging.getLogger(__name__)


def run(
    tracked_entities: List[TrackedEntity],
    *,
    sender_id: str,
    receiver_id: str,

# Path: dhis2_core/src/dhis2/e2b/e2b_resources.py
# def build_messageheader(root: etree.Element, sender_id: str, receiver_id: str):
#     mh = E.ichicsrmessageheader(
#         E.messagetype("ichicsr"),
#         E.messageformatversion("2.1"),
#         E.messageformatrelease("2.0"),
#         E.messagenumb(str(uuid4())),
#         E.messagesenderidentifier(sender_id),
#         E.messagereceiveridentifier(receiver_id),
#         E.messagedateformat("204"),
#         E.messagedate(date_format_204(datetime.now())),
#     )
# 
#     root.append(mh)
# 
# def build_safetyreport(
#     root: etree.Element,
#     te: TrackedEntity,
#     en: Enrollment,
#     country: str,
#     receiverorganization: str,
#     receivercountrycode: str,
# ):
#     sr = etree.SubElement(root, "safetyreport")
# 
#     id = get_attribute_value("h5FuguPFF2j", te)
# 
#     if country:
#         country = country.upper()
# 
#         if id:
#             id = f"{country}-{id}"
#         else:
#             id = f"{country}"
# 
#     sr.append(E.safetyreportversion("1"))
#     sr.append(E.safetyreportid(id))
#     sr.append(E.primarysourcecountry(country))
#     sr.append(E.occurcountry(country))
#     sr.append(E.transmissiondateformat("102"))
#     sr.append(E.transmissiondate(date_format_102(datetime.now())))
#     sr.append(E.reporttype("1"))
#     sr.append(E.serious(get_yes_no("fq1c1A3EOX5", te)))
# 
#     dead = get_yes_no("DOA6ZFMro84", te) == "1"
# 
#     if dead:
#         sr.append(E.seriousnessdeath("1"))
#     else:
#         sr.append(E.seriousnessdeath("2"))
# 
#     sr.append(E.seriousnesslifethreatening(get_yes_no("lATDYNmTLKD", te)))
#     sr.append(E.seriousnesshospitalization(get_yes_no("Il1lTfknLdd", te)))
#     sr.append(E.seriousnessdisabling(get_yes_no("lsO8n8ZmLAB", te)))
#     sr.append(E.seriousnesscongenitalanomali(get_yes_no("lSBsxcQU0kO", te)))
#     sr.append(E.seriousnessother(get_yes_no("tWcNgbkOETR", te)))
#     sr.append(E.receivedateformat("102"))
#     sr.append(E.receivedate(date_format_102(datetime.now())))
#     sr.append(E.receiptdateformat("102"))
#     sr.append(E.receiptdate(date_format_102(datetime.now())))
#     sr.append(E.additionaldocument("2"))
#     # sr.append(E.fulfillexpeditecriteria("1"))
# 
#     reporter_name = get_data_value("uZ9c4fKXuNS", te)
#     qualification = "1" if reporter_name.upper().startswith("DR ") else "3"
# 
#     sr.append(
#         E.primarysource(
#             E.reportergivename(reporter_name),
#             E.reporterorganization(get_data_value("Q20pEixZxCs", te)),  # TODO resolve org unit
#             E.qualification(qualification),
#         )
#     )
# 
#     sr.append(
#         E.sender(
#             E.senderorganization("DHIS2"),
#         )
#     )
# 
#     sr.append(
#         E.receiver(
#             E.receivertype("5"),
#             E.receiverorganization(receiverorganization),
#             E.receivercountrycode(receivercountrycode),
#         )
#     )
# 
#     build_safetyreport_patient(sr, te)
# 
# def print_root(root: etree.Element, pretty_print: bool = True):
#     print(
#         etree.tostring(
#             root,
#             pretty_print=pretty_print,
#             standalone=False,
#             encoding="UTF-8",
#             doctype='<!DOCTYPE ichicsr SYSTEM "http://eudravigilance.ema.europa.eu/dtd/icsr21xml.dtd">',
#         ).decode()
#     )
# 
# Path: dhis2_core/src/dhis2/e2b/models/e2b.py
# 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/r2.py
import logging
from typing import List
from lxml.builder import E
from .e2b_resources import build_messageheader, build_safetyreport, print_root
from .models.e2b import TrackedEntity




log = logging.getLogger(__name__)


def run(
    tracked_entities: List[TrackedEntity],
    *,
    sender_id: str,
    receiver_id: str,

    country: str,