#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Quick check: fetch one request from Medevio API and print all fields."""

import json
import requests
from pathlib import Path

TOKEN_PATH = Path(__file__).resolve().parent.parent / "token.txt"
GRAPHQL_URL = "https://api.medevio.cz/graphql"
CLINIC_SLUG = "mudr-buzalkova"
REQUEST_ID = "6b46b5a8-b080-4821-86b0-39adabeec86b"

token = TOKEN_PATH.read_text(encoding="utf-8").strip()
headers = {
    "content-type": "application/json",
    "authorization": f"Bearer {token}",
    "origin": "https://my.medevio.cz",
    "referer": "https://my.medevio.cz/",
}

# Query with as many fields as possible
QUERY = """
query GetPatientRequest2($requestId: UUID!, $clinicSlug: String!, $locale: Locale!) {
  request: getPatientRequest2(patientRequestId: $requestId, clinicSlug: $clinicSlug) {
    id
    displayTitle(locale: $locale)
    createdAt
    updatedAt
    doneAt
    removedAt
    userNote
    eventType
    extendedPatient(clinicSlug: $clinicSlug) {
      name
      surname
      dob
      identificationNumber
      insuranceCompanyObject { shortName }
    }
    ecrfFilledData(locale: $locale) {
      name
      groups {
        label
        fields { name label type value }
      }
    }
  }
}
"""

payload = {
    "operationName": "GetPatientRequest2",
    "query": QUERY,
    "variables": {
        "requestId": REQUEST_ID,
        "clinicSlug": CLINIC_SLUG,
        "locale": "cs",
    },
}

r = requests.post(GRAPHQL_URL, json=payload, headers=headers, timeout=30)
print(json.dumps(r.json(), indent=2, ensure_ascii=False))
