"""Run beside downloads/petrophysics_client.py after deploying the v24 backend.

Copy petrophysics_client.py beside this script, then run:
    python hosted_libraries.py

Set PIO_LIBRARY_URL to the deployed backend URL. Set PIO_API_KEY only for a
key-protected library; public demo functions need no consumer key.
"""

import os
from petrophysics_client import Client, RemoteError


def main():
    api = Client(
        base_url=os.getenv("PIO_LIBRARY_URL", "https://api.petrophysics.io"),
        api_key=os.getenv("PIO_API_KEY") or None,
        timeout=30,
    )
    print("Published libraries:", api.catalog())
    demo = api.library("demo", version=1)
    gr = [20, 40, 60, 80, 100]
    print("Vsh:", demo.vsh(gr, gr_min=20, gr_max=100))
    porosity = demo.density_porosity([2.30, 2.40, 2.50])
    print("Density porosity:", porosity)
    print("Archie Sw:", demo.archie_sw([15, 10, 5], porosity, rw=0.1))
    print("A single value:", api.call("demo", "vsh", 60, version=1))
    # Optional numpy arrays work too: numpy need not be installed for the client.
    # import numpy as np
    # vsh = demo.vsh(np.array(gr), gr_min=np.float64(20), gr_max=100)
    # Your colleague receives results, never the hosted implementation source.


if __name__ == "__main__":
    try:
        main()
    except RemoteError as exc:
        print("Remote call failed:", exc.error_code, "HTTP", exc.status, str(exc))
        raise SystemExit(1)
