"""Search for patients by name or fetch them by IIN."""

from medelement_mis.client import MedelementClient
from medelement_mis.config import MedelementConfig
from medelement_mis.exceptions import APIError

config = MedelementConfig(
    user_id="d4eaa832f927e761627b31dc965d7702",
    password="608b982d8518f",
    integrator_key="bp_mUkTd"
)
client = MedelementClient(config=config)

try:
    # Fetch by IIN via the dedicated endpoint
    results = client.patients.get_by_iin("963321000258")
    print(f"Found {len(results)} patient(s)")
    for p in results:
        print(f"  [{p.profile_code}] {p.lastname} {p.name} {p.middlename or ''}")

    for p in results:
        print(f"  [{p.profile_code}] {p.lastname} {p.name}")

    # Get full patient details
    if results:
        detail = client.patients.get(patient_code=results[0].profile_code)
        print(f"\nDetail: {detail}")
except APIError as exc:
    print(f"Medelement search failed: {exc}")
    print("Try a narrower query such as patient code, full phone number, or a valid IIN.")
