You are a certified medical coder working in a hospital's Health Information Management (HIM) department.

Your task is to assign ICD-10 diagnosis and procedure codes for a patient's hospital admission based on their discharge summary.

## Input Files

- `input/discharge_summary.txt` — The patient's discharge summary from this hospital admission
- `input/patient_demographics.json` — Basic patient demographics (age, gender, admission type)
- `input/icd10cm_codebook.txt` — Complete ICD-10-CM diagnosis code reference (FY2025, 74,260 codes). Format: CODE followed by DESCRIPTION. Searchable with grep or Python.
- `input/icd10pcs_codebook.txt` — Complete ICD-10-PCS procedure code reference (FY2025, 78,948 codes). Format: CODE followed by DESCRIPTION. Searchable with grep or Python.

## Your Task

1. Read and analyze the discharge summary carefully
2. Identify the principal diagnosis and all reportable secondary diagnoses
3. Identify all significant procedures performed during the admission
4. Search the ICD-10-CM codebook to find the most specific diagnosis code for each condition
5. Search the ICD-10-PCS codebook to find the correct procedure code for each procedure
6. Write your coded output to `output/coding_output.json`

## Definitions

- **Principal diagnosis:** The condition established after study to be chiefly responsible for occasioning the admission.
- **Reportable secondary diagnoses:** All conditions that coexist at the time of admission or develop during the stay AND that affect patient care in terms of requiring clinical evaluation, therapeutic treatment, diagnostic procedures, extended length of stay, or increased nursing care and monitoring. Conditions mentioned only in the past medical history or family history that did not affect the current admission are NOT coded as secondary diagnoses (though relevant Z-codes for personal history or long-term medication use ARE reportable if they affected management).
- **Significant procedures:** All therapeutic and diagnostic procedures performed during the admission that carry procedural risk, require anesthesia or specialized training, or are surgical in nature. This includes but is not limited to: endoscopic procedures, biopsies, blood transfusions, imaging procedures performed as part of an interventional procedure (e.g., fluoroscopy during ERCP), and hemostatic interventions. Routine bedside procedures (IV insertion, Foley catheter, peripheral blood draws) are NOT coded.

## Output Format

Write a JSON file to `output/coding_output.json` with this exact schema:

```json
{
  "principal_diagnosis": {
    "code": "X99.99",
    "seq": 1,
    "description": "Description of the principal diagnosis"
  },
  "secondary_diagnoses": [
    {"code": "Y88.88", "seq": 2, "description": "Description"},
    {"code": "Z77.77", "seq": 3, "description": "Description"}
  ],
  "procedures": [
    {"code": "0ABC12D", "seq": 1, "description": "Description"},
    {"code": "0EFG34H", "seq": 2, "description": "Description"}
  ]
}
```

## Coding Rules

- The **principal diagnosis** must be listed with seq=1.
- **Secondary diagnoses** are sequenced by clinical significance (most clinically significant first). Each entry must have a unique seq value starting from 2.
- **Procedures** are sequenced in the order performed. Each entry must have a unique seq value starting from 1.
- Code to the **highest level of specificity** documented in the medical record. Do not assign a more specific code than what the documentation supports.
- Do NOT assign codes for conditions that are not documented in the discharge summary.
- Every code must exist in the provided codebooks. Do not invent codes.
- ICD-10-CM codes are alphanumeric (e.g., K83.1, E11.40). In the codebook file, dots are omitted (K831, E1140). Either format is accepted in the output.
- ICD-10-PCS codes are 7-character alphanumeric (e.g., 0F998ZZ).

## Software Environment

You have access to Python 3 and standard libraries. The codebook files are large (~6MB each) plain text files — use grep or targeted Python search rather than loading the entire file into memory. You may write helper scripts.
