MEDEVIO API – WORKING NOTES
============================

GENERAL ARCHITECTURE
--------------------
Frontend: React + Material-UI (MUI) + Apollo GraphQL
Backend: GraphQL API at https://api.medevio.cz/graphql
Authentication: via cookies / tokens stored in medevio_storage.json (Playwright session file)
Frontend base: https://my.medevio.cz/mudr-buzalkova/klinika/...
Session handling: token refreshed through AccessToken_AuthSelf calls

AUTHENTICATION AND SESSION
--------------------------
Reuse session cookies from medevio_storage.json.

Example:
    state = json.load(open("medevio_storage.json"))
    cookies = {c["name"]: c["value"] for c in state["cookies"] if "medevio" in c["domain"]}

Use these cookies in all requests.post(GRAPHQL_URL, headers, cookies, data=...) calls.

KEY API ENDPOINTS (GraphQL OPERATIONS)
--------------------------------------
Operation name                        Purpose
------------------------------------  ------------------------------------------
AccessToken_AuthSelf                  Verify or refresh current user session
ClinicNavigation_GetClinic            Get basic clinic info (name, ID)
ClinicAgenda_GetCalendarsForClinic    List available calendars within your clinic
ClinicAgenda_ListClinicReservations   List reservations (appointments) for date or range
UpdateReservation_GetReservation      Get full detail of one reservation
ClinicRequestDetail_GetPatientRequest2 Fetch detailed “Požadavek” (request card)
ClinicRequestNotes_Get                Load notes/comments on Požadavek
UseMessages_ListMessages              Load chat messages with patient
Communication_GetClinicFooter         UI info only

WORKFLOW FOR AUTOMATION
-----------------------
1. Extract reservations (agenda overview)
   operationName: "ClinicAgenda_ListClinicReservations"
   variables: {"clinicId": "<clinic-uuid>", "dateFrom": "YYYY-MM-DDT00:00:00Z", "dateTo": "YYYY-MM-DDT23:59:59Z"}

   Returns: id, startDateTime, endDateTime, patient {name, age}, reason, status

2. Get detail of a single reservation
   operationName: "UpdateReservation_GetReservation"
   variables: {"id": "<reservation-uuid>"}

   Returns: reason, time, doctor, patient contact, note, etc.

3. Get detail of Požadavek (request card)
   operationName: "ClinicRequestDetail_GetPatientRequest2"
   variables: {"id": "<request-uuid>"}

   Returns: text of request, attachments, communication, etc.

HTTP SETUP FOR ALL GRAPHQL CALLS
--------------------------------
GRAPHQL_URL = "https://api.medevio.cz/graphql"
headers = {
    "content-type": "application/json",
    "origin": "https://my.medevio.cz",
    "referer": "https://my.medevio.cz/",
}

response = requests.post(GRAPHQL_URL, headers=headers, cookies=cookies, data=json.dumps(payload))

DATA STRUCTURE HINTS (Reservation)
----------------------------------
{
  "id": "0e3a4f5c-1c15-40e6-b1b1-325de9269ef5",
  "reason": "Očkování - Chřipka",
  "startDateTime": "2025-10-17T09:10:00Z",
  "endDateTime": "2025-10-17T09:20:00Z",
  "status": "confirmed",
  "note": "přijde i s taťkou",
  "patient": {"id": "...", "name": "Černík Pavel", "age": 53},
  "doctor": {"id": "...", "name": "MUDr. Buzalková"},
  "location": {"name": "Ordinace Prosek"}
}

PLAYWRIGHT SELECTORS (REFERENCE)
--------------------------------
Purpose          Selector
---------------- -------------------------------------------
Agenda rows      div[data-testid='reservation-row']
Row ID           data-id
Agenda fields    div[data-field='Time'], div[data-field='Patient'], div[data-field='Reason']

RECOMMENDED AUTOMATION PIPELINE
--------------------------------
1. Step 1 – Agenda scrape (Playwright)
   Open agenda-dne → extract data-id, patient, time, reason.
2. Step 2 – GraphQL fetch (Python requests)
   Use ClinicAgenda_ListClinicReservations to get full dataset.
3. Step 3 – Detail enrichment
   For each appointment → call UpdateReservation_GetReservation.
   For linked Požadavek → call ClinicRequestDetail_GetPatientRequest2.
4. Step 4 – Storage / reporting
   Save to MySQL or Excel for vaccine inventory, reminders, analytics.

SECURITY / MAINTENANCE NOTES
----------------------------
- GraphQL introspection is disabled.
- Session cookies expire periodically; refresh via Playwright login.
- Keep data secure (GDPR compliance).

FUTURE TASKS
------------
- Automate login refresh and cookie renewal.
- Build function get_reservations(date) using ClinicAgenda_ListClinicReservations.
- Build function get_reservation_detail(reservation_id).
- Map vaccine keywords (Očkování – Chřipka, COVID, Hepatitida, …).
- Export to Excel/MySQL for vaccine order planning.
