# Athena Upload — App Implementation Spec

## Server endpoint is READY. Here's everything you need.

### Endpoint

`POST https://kinometric.com/back_athena_send.php`

### Request (JSON)

```json
{
  "user_id": 3,
  "encryption_key": "abc123...",
  "athena_patient_id": "7681",
  "patient": "John Doe",
  "pdf": "JVBERi0xLjQK...",
  "department_id": "1"
}
```

| Field | Required | Source in App | Notes |
|-------|----------|--------------|-------|
| `user_id` | YES | `FFAppState().userId` | Same as all other API calls |
| `encryption_key` | YES | `FFAppState().encryptionKey` | Also accepts `encryptionkey` |
| `athena_patient_id` | YES | `FFAppState().realPatientid` | The `realpatientid` field -- this IS the athena link |
| `patient` | YES | First + Last name | Patient display name for the document note |
| `pdf` | YES | `Uint8List` from `pdf.save()`, base64 encoded | Raw base64 OR `data:application/pdf;base64,...` prefix -- both work |
| `department_id` | NO | Hardcode `"1"` or omit | Server defaults to configured department if missing |

### Success Response

```json
{
  "success": true,
  "clinicaldocumentid": 212361,
  "message": "Document uploaded to athenaOne successfully.",
  "athena_patient_id": "7681",
  "department_id": "1"
}
```

### Error Response

```json
{
  "success": false,
  "error": "Missing required parameters: athena_patient_id"
}
```

### Implementation -- Follow the Email PDF Pattern

The Athena upload is almost identical to the existing Email PDF flow:

1. Generate PDF in memory (already done -- `pdf.save()` gives `Uint8List`)
2. Base64 encode the bytes: `base64Encode(pdfBytes)`
3. POST to `back_athena_send.php` instead of `back_mail_pdf.php`
4. Show success/error snackbar

### What to implement in pdf_gen_widget.dart

The Athena button handler at line ~504-506 (currently empty) should:

1. **Check that `realpatientid` is not empty** -- if it is, show error: "No athenaOne patient ID linked. Set the patient's Real Patient ID first."
2. **Show confirmation dialog**: "Upload balance test results to athenaOne for {patient name}?"
3. **On confirm**:
   - Generate PDF (reuse existing generatePdf or use already-generated bytes)
   - Base64 encode: `base64Encode(pdfBytes)`
   - POST to server (same HTTP pattern as email)
   - Show loading spinner during upload
4. **On success**: Show snackbar "Uploaded to athenaOne (Document ID: {id})"
5. **On error**: Show snackbar with error message

### Key guard rail

If `FFAppState().realPatientid` is empty (no athena patient ID linked), the button should either:
- Be disabled/grayed out with tooltip "No athenaOne ID"
- Or show an error dialog when tapped

### That's it

No department picker needed (server uses default). No patient verification needed (that's the web dashboard's job). Just: generate PDF, base64, POST, show result.
