#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, requests
sys.stdout.reconfigure(encoding='utf-8')
from requests_pkcs12 import Pkcs12Adapter
from datetime import date
import xml.etree.ElementTree as ET

ENDPOINT = "https://prod.b2b.vzp.cz/B2BProxy/HttpProxy/RegistracePojistencePZSB2B"
PFX_PATH = r"/Insurance/Certificates/picka.pfx"
PFX_PASS = "Vlado7309208104+"
NS = {
    "soap": "http://schemas.xmlsoap.org/soap/envelope/",
    "rp":   "http://xmlns.gemsystem.cz/B2B/RegistracePojistencePZSB2B/1",
}

envelope = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns1:registracePojistencePZSB2B xmlns:ns1="http://xmlns.gemsystem.cz/B2B/RegistracePojistencePZSB2B/1">
      <ns1:cisloPojistence>7309208104</ns1:cisloPojistence>
      <ns1:kDatu>2026-04-29</ns1:kDatu>
    </ns1:registracePojistencePZSB2B>
  </soap:Body>
</soap:Envelope>"""

session = requests.Session()
session.mount("https://", Pkcs12Adapter(pkcs12_filename=PFX_PATH, pkcs12_password=PFX_PASS))
resp = session.post(ENDPOINT, data=envelope.encode("utf-8"),
                    headers={"Content-Type": "text/xml; charset=utf-8", "SOAPAction": "process"},
                    timeout=30, verify=True)

print(f"HTTP: {resp.status_code}")
root = ET.fromstring(resp.text)
items = root.findall(".//rp:odbornost", NS)
print(f"Pocet odbornosti: {len(items)}")
for i, it in enumerate(items):
    print(f"\n--- odbornost #{i+1} ---")
    print(ET.tostring(it, encoding="unicode"))

st = root.find(".//rp:stavVyrizeniPozadavku", NS)
print(f"stav: {st.text if st is not None else '?'}")
