import requests
import json
from pathlib import Path

TOKEN_PATH = Path(__file__).resolve().parent.parent / "token.txt"
REQUEST_ID = "092a0c63-28be-4c6b-ab3b-204e1e2641d4"
CLINIC_SLUG = "mudr-buzalkova"

def read_token(p: Path) -> str:
    tok = p.read_text(encoding="utf-8").strip()
    if tok.startswith("Bearer "):
        tok = tok.split(" ", 1)[1]
    return tok

GRAPHQL_QUERY = r"""
query ClinicRequestDetail_GetPatientRequest2(
  $requestId: UUID!,
  
) {
  patientRequestMedicalRecords: listMedicalRecordsForPatientRequest(
    attachmentTypes: [ECRF_FILL_ATTACHMENT, MESSAGE_ATTACHMENT, PATIENT_REQUEST_ATTACHMENT]
    patientRequestId: $requestId
    pageInfo: {first: 100, offset: 0}
  ) {
    attachmentType
    id
    medicalRecord {
      contentType
      description
      downloadUrl
      id
      url
      visibleToPatient
    }
  }
}
"""


variables = {
    "requestId": REQUEST_ID,
    }

headers = {
    "Authorization": f"Bearer {read_token(TOKEN_PATH)}",
    "Content-Type": "application/json",
    "Accept": "application/json",
}

payload = {
    "operationName": "ClinicRequestDetail_GetPatientRequest2",
    "query": GRAPHQL_QUERY,
    "variables": variables,
}

print("📡 Querying Medevio API...\n")
r = requests.post("https://api.medevio.cz/graphql", json=payload, headers=headers)
print(f"HTTP status: {r.status_code}\n")
print(json.dumps(r.json(), indent=2, ensure_ascii=False))
