# 🧭 MEDEVIO API – Working Notes

## General Architecture
- **Frontend:** React + Material‑UI (MUI) + Apollo GraphQL  
- **Backend:** GraphQL API → `https://api.medevio.cz/graphql`  
- **Authentication:** cookies/tokens stored in `medevio_storage.json` (Playwright session)  
- **Frontend base:** `https://my.medevio.cz/mudr-buzalkova/klinika/...`  
- **Session handling:** token refreshed through `AccessToken_AuthSelf` calls  

---

## Authentication and Session
Reuse session cookies from Playwright:

```python
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 every `requests.post(GRAPHQL_URL, headers, cookies, data=...)` call.

---

## Key GraphQL Operations

| Operation name | Purpose |
|----------------|----------|
| `AccessToken_AuthSelf` | Verify or refresh session |
| `ClinicNavigation_GetClinic` | Get basic clinic info |
| `ClinicAgenda_GetCalendarsForClinic` | List calendars within clinic |
| **`ClinicAgenda_ListClinicReservations`** | **List reservations (appointments)** |
| **`UpdateReservation_GetReservation`** | **Get full detail of one reservation** |
| **`ClinicRequestDetail_GetPatientRequest2`** | **Fetch detailed “Požadavek” (request card)** |
| `ClinicRequestNotes_Get` | Load Požadavek notes |
| `UseMessages_ListMessages` | Load chat messages |
| `Communication_GetClinicFooter` | UI footer text |

---

## Workflow for Automation

### 1️⃣ Extract reservations (agenda overview)
```graphql
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
```graphql
operationName: "UpdateReservation_GetReservation"
variables: {"id": "<reservation-uuid>"}
```
→ returns reason, time, doctor, patient contact, note, etc.

### 3️⃣ Get detail of Požadavek
```graphql
operationName: "ClinicRequestDetail_GetPatientRequest2"
variables: {"id": "<request-uuid>"}
```
→ returns text, attachments, communication, etc.

---

## HTTP Setup (Python)

```python
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))
```

---

## Reservation Data Example

```json
{
  "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

| 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. **Agenda scrape (Playwright)** → extract `data-id`, patient, time, reason.  
2. **GraphQL fetch (Python)** → use `ClinicAgenda_ListClinicReservations`.  
3. **Detail enrichment** → call `UpdateReservation_GetReservation` and `ClinicRequestDetail_GetPatientRequest2`.  
4. **Storage/reporting** → save to MySQL/Excel for vaccine planning.

---

## Security / Maintenance Notes

- GraphQL introspection disabled.  
- Refresh session cookies periodically.  
- Keep data GDPR-compliant.

---

## Future Tasks

- Automate cookie renewal.  
- Build helper functions:  
  - `get_reservations(date)` using `ClinicAgenda_ListClinicReservations`  
  - `get_reservation_detail(reservation_id)`  
- Map vaccine keywords (`Očkování – Chřipka`, `COVID`, `Hepatitida`, …).  
- Export to Excel/MySQL for vaccine order planning.
