import uuid
from datetime import datetime, timezone
from requests import Session
from requests_pkcs12 import Pkcs12Adapter

PFX_FILE = r"C:\Users\vlado\PycharmProjects\Recepty\AMBSUKL214235369G_31DEC2024.pfx"
PFX_PASSWORD = "Vlado7309208104++"
API_USER = "e08c89c6-2b1a-4eba-8ed9-4e3e63618379"
API_PASS = "Buzalka@Vladimir2025"

sess = Session()
sess.mount("https://", Pkcs12Adapter(pkcs12_filename=PFX_FILE, pkcs12_password=PFX_PASSWORD))
sess.auth = (API_USER, API_PASS)

id_zpravy = str(uuid.uuid4())
odeslano = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S+01:00")

# Přesně tělo z Medicusu
soap_body = (
    '<?xml version="1.0" encoding="UTF-8"?>'
    '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">'
    '<soapenv:Body>'
    '<NacistLekovyZaznamLekarDotaz xmlns="http://www.sukl.cz/erp/201912">'
    '<Doklad><Pristupujici>'
    '<Uzivatel>E08C89C6-2B1A-4EBA-8ED9-4E3E63618379</Uzivatel>'
    '<Pracoviste>00214235367</Pracoviste>'
    '</Pristupujici>'
    '<PocetZnakuATC>7</PocetZnakuATC>'
    '<PocetMesicu>60</PocetMesicu>'
    '<Pacient><Totoznost><Jmeno>'
    '<Prijmeni>Buzalka</Prijmeni>'
    '<Jmena>Vladim\u00edr</Jmena>'
    '</Jmeno>'
    '<DatumNarozeni>1973-09-20</DatumNarozeni>'
    '</Totoznost></Pacient></Doklad>'
    f'<Zprava><ID_Zpravy>{id_zpravy}</ID_Zpravy>'
    '<Verze>202501A</Verze>'
    f'<Odeslano>{odeslano}</Odeslano>'
    '<SW_Klienta>MEDICUS_____</SW_Klienta></Zprava>'
    '</NacistLekovyZaznamLekarDotaz>'
    '</soapenv:Body>'
    '</soapenv:Envelope>'
)

headers = {
    "Content-Type": 'text/xml; charset="UTF-8"',
    "SOAPAction": '"NacistLekovyZaznam"',
    "User-Agent": "Medicus"
}

endpoints = [
    "https://cuer-soap.erecept.sukl.cz/",
    "https://rlpo-soap.erecept.sukl.cz/",
    "https://common-soap.erecept.sukl.cz/",
    "https://lz-soap.erecept.sukl.cz/",
    "https://lekar-soap.erecept.sukl.cz/cuer/Lekar",
]

for ep in endpoints:
    print(f"\n--- {ep} ---")
    try:
        resp = sess.post(ep, data=soap_body.encode("utf-8"), headers=headers, timeout=10)
        print(f"HTTP {resp.status_code} | {len(resp.content)} bytů")
        print(resp.text[:300])
    except Exception as e:
        print(f"CHYBA: {e}")
