File size: 30,673 Bytes
a6b8ecc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
"""
Convert Synthea synthetic patient data to realistic medical PDFs with PHI.
This module generates various medical document types from Synthea JSON/CSV output.
"""

import json
import os
import random
from datetime import datetime, timedelta
from pathlib import Path
from typing import Dict, List, Any, Tuple, Optional

import pandas as pd
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer, PageBreak
from reportlab.platypus import Image as RLImage
from PIL import Image, ImageDraw, ImageFont
import numpy as np
from faker import Faker


class MedicalPDFGenerator:
    """Generate realistic medical PDFs from Synthea data with full PHI."""

    def __init__(self, synthea_dir: str, output_dir: str, annotations_dir: str):
        """
        Initialize the PDF generator.

        Args:
            synthea_dir: Directory containing Synthea output
            output_dir: Directory to save generated PDFs
            annotations_dir: Directory to save PHI annotations
        """
        self.synthea_dir = Path(synthea_dir)
        self.output_dir = Path(output_dir)
        self.annotations_dir = Path(annotations_dir)

        # Create output directories
        self.output_dir.mkdir(parents=True, exist_ok=True)
        self.annotations_dir.mkdir(parents=True, exist_ok=True)

        # Initialize faker for additional realistic data
        self.faker = Faker()
        Faker.seed(42)

        # PHI tracking for annotations
        self.phi_annotations = []

        # Document templates
        self.document_types = [
            'prescription',
            'lab_report',
            'discharge_summary',
            'insurance_claim',
            'medical_record',
            'referral_letter',
            'consultation_note',
            'radiology_report',
        ]

    def load_synthea_patient(self, patient_file: Path) -> Dict:
        """Load a Synthea patient from FHIR JSON."""
        with open(patient_file, 'r') as f:
            return json.load(f)

    def extract_patient_info(self, bundle: Dict) -> Dict[str, Any]:
        """Extract relevant patient information from FHIR bundle."""
        patient_info = {
            'name': '',
            'birth_date': '',
            'ssn': '',
            'phone': '',
            'email': '',
            'address': '',
            'mrn': '',
            'insurance_id': '',
            'conditions': [],
            'medications': [],
            'procedures': [],
            'encounters': [],
            'allergies': [],
            'immunizations': [],
            'observations': [],
        }

        for entry in bundle.get('entry', []):
            resource = entry.get('resource', {})
            resource_type = resource.get('resourceType', '')

            if resource_type == 'Patient':
                # Extract patient demographics
                name_data = resource.get('name', [{}])[0]
                patient_info['name'] = f"{name_data.get('given', [''])[0]} {name_data.get('family', '')}"
                patient_info['birth_date'] = resource.get('birthDate', '')

                # Extract SSN from identifier
                for identifier in resource.get('identifier', []):
                    if identifier.get('system') == 'http://hl7.org/fhir/sid/us-ssn':
                        patient_info['ssn'] = identifier.get('value', self.faker.ssn())

                # Extract contact information
                telecom = resource.get('telecom', [])
                for contact in telecom:
                    if contact.get('system') == 'phone':
                        patient_info['phone'] = contact.get('value', self.faker.phone_number())
                    elif contact.get('system') == 'email':
                        patient_info['email'] = contact.get('value', self.faker.email())

                # Extract address
                address_data = resource.get('address', [{}])[0]
                patient_info['address'] = self._format_address(address_data)

                # Generate MRN
                patient_info['mrn'] = f"MRN-{self.faker.random_number(digits=8, fix_len=True)}"

            elif resource_type == 'Condition':
                condition = {
                    'code': resource.get('code', {}).get('text', ''),
                    'onset': resource.get('onsetDateTime', ''),
                    'status': resource.get('clinicalStatus', {}).get('text', ''),
                }
                patient_info['conditions'].append(condition)

            elif resource_type == 'MedicationRequest':
                medication = {
                    'name': resource.get('medicationCodeableConcept', {}).get('text', ''),
                    'dosage': self._extract_dosage(resource.get('dosageInstruction', [])),
                    'prescriber': self._extract_prescriber(resource),
                }
                patient_info['medications'].append(medication)

            elif resource_type == 'Procedure':
                procedure = {
                    'name': resource.get('code', {}).get('text', ''),
                    'date': resource.get('performedDateTime', resource.get('performedPeriod', {}).get('start', '')),
                    'performer': self._extract_performer(resource),
                }
                patient_info['procedures'].append(procedure)

            elif resource_type == 'Encounter':
                encounter = {
                    'type': resource.get('class', {}).get('display', ''),
                    'period': resource.get('period', {}),
                    'reason': self._extract_reason(resource),
                }
                patient_info['encounters'].append(encounter)

            elif resource_type == 'AllergyIntolerance':
                allergy = {
                    'substance': resource.get('code', {}).get('text', ''),
                    'severity': resource.get('criticality', ''),
                }
                patient_info['allergies'].append(allergy)

        # Generate insurance ID
        patient_info['insurance_id'] = f"INS-{self.faker.random_number(digits=10, fix_len=True)}"

        return patient_info

    def _format_address(self, address_data: Dict) -> str:
        """Format address from FHIR address structure."""
        lines = address_data.get('line', [])
        city = address_data.get('city', self.faker.city())
        state = address_data.get('state', self.faker.state_abbr())
        postal = address_data.get('postalCode', self.faker.postcode())

        if not lines:
            lines = [self.faker.street_address()]

        return f"{', '.join(lines)}, {city}, {state} {postal}"

    def _extract_dosage(self, dosage_instructions: List) -> str:
        """Extract dosage information."""
        if not dosage_instructions:
            return "Take as directed"

        dosage = dosage_instructions[0]
        text = dosage.get('text', '')
        if text:
            return text

        # Build dosage string from components
        timing = dosage.get('timing', {}).get('repeat', {})
        frequency = timing.get('frequency', 1)
        period = timing.get('period', 1)
        period_unit = timing.get('periodUnit', 'day')

        return f"{frequency} time(s) per {period} {period_unit}"

    def _extract_prescriber(self, resource: Dict) -> str:
        """Extract prescriber name."""
        requester = resource.get('requester', {})
        display = requester.get('display', '')
        if display:
            return display
        return f"Dr. {self.faker.last_name()}"

    def _extract_performer(self, resource: Dict) -> str:
        """Extract performer name."""
        performers = resource.get('performer', [])
        if performers:
            return performers[0].get('actor', {}).get('display', f"Dr. {self.faker.last_name()}")
        return f"Dr. {self.faker.last_name()}"

    def _extract_reason(self, resource: Dict) -> str:
        """Extract encounter reason."""
        reasons = resource.get('reasonCode', [])
        if reasons:
            return reasons[0].get('text', 'Routine checkup')
        return 'Routine checkup'

    def create_prescription(self, patient_info: Dict, output_path: Path) -> List[Dict]:
        """Create a prescription PDF."""
        doc = SimpleDocTemplate(str(output_path), pagesize=letter)
        story = []
        styles = getSampleStyleSheet()
        phi_annotations = []

        # Header
        header_style = ParagraphStyle(
            'CustomHeader',
            parent=styles['Heading1'],
            fontSize=24,
            textColor=colors.HexColor('#003366'),
            alignment=1,  # Center
        )

        story.append(Paragraph("PRESCRIPTION", header_style))
        story.append(Spacer(1, 0.2 * inch))

        # Clinic information
        clinic_name = f"{self.faker.company()} Medical Center"
        clinic_address = self.faker.address().replace('\n', ', ')
        clinic_phone = self.faker.phone_number()
        dea_number = f"DEA: B{self.faker.random_number(digits=7, fix_len=True)}"
        npi_number = f"NPI: {self.faker.random_number(digits=10, fix_len=True)}"

        clinic_info = f"""
        <para align=center>
        <b>{clinic_name}</b><br/>
        {clinic_address}<br/>
        Phone: {clinic_phone}<br/>
        {dea_number} | {npi_number}
        </para>
        """
        story.append(Paragraph(clinic_info, styles['Normal']))
        story.append(Spacer(1, 0.3 * inch))

        # Patient information (PHI)
        patient_data = [
            ['Patient Name:', patient_info['name']],  # PHI: Name
            ['Date of Birth:', patient_info['birth_date']],  # PHI: Date
            ['MRN:', patient_info['mrn']],  # PHI: MRN
            ['Address:', patient_info['address']],  # PHI: Address
            ['Phone:', patient_info['phone']],  # PHI: Phone
            ['Date:', datetime.now().strftime('%Y-%m-%d')],  # PHI: Date
        ]

        # Track PHI locations for annotations
        phi_annotations.append({'type': 'name', 'value': patient_info['name'], 'page': 1})
        phi_annotations.append({'type': 'date', 'value': patient_info['birth_date'], 'page': 1})
        phi_annotations.append({'type': 'mrn', 'value': patient_info['mrn'], 'page': 1})
        phi_annotations.append({'type': 'address', 'value': patient_info['address'], 'page': 1})
        phi_annotations.append({'type': 'phone', 'value': patient_info['phone'], 'page': 1})

        patient_table = Table(patient_data, colWidths=[2 * inch, 4 * inch])
        patient_table.setStyle(TableStyle([
            ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
            ('FONTNAME', (1, 0), (1, -1), 'Helvetica'),
            ('FONTSIZE', (0, 0), (-1, -1), 10),
            ('ALIGN', (0, 0), (0, -1), 'RIGHT'),
            ('ALIGN', (1, 0), (1, -1), 'LEFT'),
        ]))
        story.append(patient_table)
        story.append(Spacer(1, 0.3 * inch))

        # Prescription details
        story.append(Paragraph("℞", ParagraphStyle('Rx', fontSize=20, alignment=0)))
        story.append(Spacer(1, 0.1 * inch))

        # Add medications
        if patient_info['medications']:
            for i, med in enumerate(patient_info['medications'][:3]):  # Limit to 3 medications
                med_text = f"""
                <para leftIndent=20>
                <b>{med['name']}</b><br/>
                Sig: {med['dosage']}<br/>
                Quantity: {random.randint(30, 90)}<br/>
                Refills: {random.randint(0, 3)}
                </para>
                """
                story.append(Paragraph(med_text, styles['Normal']))
                story.append(Spacer(1, 0.2 * inch))
        else:
            # Generate synthetic medication if none available
            med_name = self.faker.word().capitalize() + "mycin 500mg"
            med_text = f"""
            <para leftIndent=20>
            <b>{med_name}</b><br/>
            Sig: Take 1 tablet by mouth twice daily<br/>
            Quantity: 60<br/>
            Refills: 2
            </para>
            """
            story.append(Paragraph(med_text, styles['Normal']))
            story.append(Spacer(1, 0.2 * inch))

        # Prescriber signature
        prescriber_name = f"Dr. {self.faker.name()}"
        license_number = f"License: {self.faker.random_number(digits=8, fix_len=True)}"

        phi_annotations.append({'type': 'name', 'value': prescriber_name, 'page': 1})

        signature_text = f"""
        <para>
        <br/><br/>
        _________________________________<br/>
        {prescriber_name}, MD<br/>
        {license_number}
        </para>
        """
        story.append(Paragraph(signature_text, styles['Normal']))

        # Build PDF
        doc.build(story)

        return phi_annotations

    def create_lab_report(self, patient_info: Dict, output_path: Path) -> List[Dict]:
        """Create a laboratory report PDF."""
        doc = SimpleDocTemplate(str(output_path), pagesize=letter)
        story = []
        styles = getSampleStyleSheet()
        phi_annotations = []

        # Header
        header_style = ParagraphStyle(
            'LabHeader',
            parent=styles['Heading1'],
            fontSize=20,
            textColor=colors.HexColor('#800020'),
            alignment=1,
        )

        story.append(Paragraph("LABORATORY REPORT", header_style))
        story.append(Spacer(1, 0.2 * inch))

        # Lab information
        lab_name = f"{self.faker.company()} Diagnostics"
        lab_address = self.faker.address().replace('\n', ', ')
        lab_phone = self.faker.phone_number()
        clia_number = f"CLIA: {self.faker.random_number(digits=10, fix_len=True)}"

        lab_info = f"""
        <para align=center>
        <b>{lab_name}</b><br/>
        {lab_address}<br/>
        Phone: {lab_phone} | {clia_number}
        </para>
        """
        story.append(Paragraph(lab_info, styles['Normal']))
        story.append(Spacer(1, 0.3 * inch))

        # Patient and specimen information
        specimen_id = f"SPEC-{self.faker.random_number(digits=10, fix_len=True)}"
        accession_number = f"ACC-{self.faker.random_number(digits=8, fix_len=True)}"
        collection_date = datetime.now() - timedelta(days=random.randint(1, 7))
        report_date = datetime.now()

        patient_data = [
            ['Patient Name:', patient_info['name']],  # PHI
            ['Date of Birth:', patient_info['birth_date']],  # PHI
            ['SSN:', patient_info['ssn']],  # PHI
            ['MRN:', patient_info['mrn']],  # PHI
            ['Collection Date:', collection_date.strftime('%Y-%m-%d %H:%M')],  # PHI
            ['Report Date:', report_date.strftime('%Y-%m-%d %H:%M')],  # PHI
            ['Specimen ID:', specimen_id],
            ['Accession #:', accession_number],
        ]

        # Track PHI
        phi_annotations.extend([
            {'type': 'name', 'value': patient_info['name'], 'page': 1},
            {'type': 'date', 'value': patient_info['birth_date'], 'page': 1},
            {'type': 'ssn', 'value': patient_info['ssn'], 'page': 1},
            {'type': 'mrn', 'value': patient_info['mrn'], 'page': 1},
            {'type': 'date', 'value': collection_date.strftime('%Y-%m-%d'), 'page': 1},
            {'type': 'date', 'value': report_date.strftime('%Y-%m-%d'), 'page': 1},
        ])

        info_table = Table(patient_data, colWidths=[2 * inch, 4 * inch])
        info_table.setStyle(TableStyle([
            ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
            ('FONTNAME', (1, 0), (1, -1), 'Helvetica'),
            ('FONTSIZE', (0, 0), (-1, -1), 9),
            ('GRID', (0, 0), (-1, -1), 0.5, colors.grey),
            ('BACKGROUND', (0, 0), (0, -1), colors.lightgrey),
        ]))
        story.append(info_table)
        story.append(Spacer(1, 0.3 * inch))

        # Test results
        story.append(Paragraph("TEST RESULTS", styles['Heading2']))
        story.append(Spacer(1, 0.1 * inch))

        # Create test results table
        test_data = [
            ['Test Name', 'Result', 'Units', 'Reference Range', 'Flag'],
        ]

        # Generate realistic lab values
        tests = [
            ('Glucose', random.randint(70, 120), 'mg/dL', '70-99', ''),
            ('Creatinine', round(random.uniform(0.6, 1.2), 1), 'mg/dL', '0.7-1.3', ''),
            ('BUN', random.randint(7, 25), 'mg/dL', '7-20', ''),
            ('Sodium', random.randint(135, 145), 'mEq/L', '136-144', ''),
            ('Potassium', round(random.uniform(3.5, 5.0), 1), 'mEq/L', '3.5-5.0', ''),
            ('Chloride', random.randint(96, 106), 'mEq/L', '96-106', ''),
            ('CO2', random.randint(22, 28), 'mEq/L', '22-28', ''),
            ('Hemoglobin', round(random.uniform(12.0, 17.0), 1), 'g/dL', '12.0-16.0', ''),
            ('Hematocrit', round(random.uniform(36.0, 48.0), 1), '%', '36.0-46.0', ''),
            ('WBC Count', round(random.uniform(4.5, 11.0), 1), 'K/uL', '4.5-11.0', ''),
        ]

        for test_name, value, units, ref_range, flag in tests:
            # Randomly flag some abnormal values
            if random.random() < 0.2:
                if random.choice([True, False]):
                    flag = 'H'  # High
                else:
                    flag = 'L'  # Low

            test_data.append([test_name, str(value), units, ref_range, flag])

        test_table = Table(test_data, colWidths=[2 * inch, 1 * inch, 1 * inch, 1.5 * inch, 0.5 * inch])
        test_table.setStyle(TableStyle([
            ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
            ('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
            ('FONTSIZE', (0, 0), (-1, -1), 9),
            ('GRID', (0, 0), (-1, -1), 0.5, colors.grey),
            ('BACKGROUND', (0, 0), (-1, 0), colors.lightgrey),
            ('ALIGN', (1, 1), (-1, -1), 'CENTER'),
            # Highlight abnormal flags
            ('TEXTCOLOR', (4, 1), (4, -1), colors.red),
            ('FONTNAME', (4, 1), (4, -1), 'Helvetica-Bold'),
        ]))
        story.append(test_table)
        story.append(Spacer(1, 0.3 * inch))

        # Pathologist signature
        pathologist_name = f"Dr. {self.faker.name()}"
        pathologist_title = "Clinical Pathologist"
        license_number = f"License: {self.faker.random_number(digits=8, fix_len=True)}"

        phi_annotations.append({'type': 'name', 'value': pathologist_name, 'page': 1})

        signature_text = f"""
        <para>
        Reviewed and Approved by:<br/><br/>
        _________________________________<br/>
        {pathologist_name}, MD<br/>
        {pathologist_title}<br/>
        {license_number}
        </para>
        """
        story.append(Paragraph(signature_text, styles['Normal']))

        # Build PDF
        doc.build(story)

        return phi_annotations

    def create_insurance_claim(self, patient_info: Dict, output_path: Path) -> List[Dict]:
        """Create an insurance claim form PDF."""
        doc = SimpleDocTemplate(str(output_path), pagesize=letter)
        story = []
        styles = getSampleStyleSheet()
        phi_annotations = []

        # Header
        header_style = ParagraphStyle(
            'ClaimHeader',
            parent=styles['Heading1'],
            fontSize=18,
            textColor=colors.HexColor('#000080'),
            alignment=1,
        )

        story.append(Paragraph("HEALTH INSURANCE CLAIM FORM", header_style))
        story.append(Paragraph("CMS-1500 (02-12)", styles['Normal']))
        story.append(Spacer(1, 0.2 * inch))

        # Insurance information
        insurance_company = f"{self.faker.company()} Health Insurance"
        policy_number = patient_info['insurance_id']
        group_number = f"GRP-{self.faker.random_number(digits=6, fix_len=True)}"

        # Patient information section
        story.append(Paragraph("<b>PATIENT INFORMATION</b>", styles['Heading3']))

        patient_data = [
            ['1. Patient Name:', patient_info['name']],  # PHI
            ['2. Date of Birth:', patient_info['birth_date']],  # PHI
            ['3. SSN:', patient_info['ssn']],  # PHI
            ['4. Patient Address:', patient_info['address']],  # PHI
            ['5. Phone:', patient_info['phone']],  # PHI
            ['6. Email:', patient_info['email']],  # PHI
            ['7. Policy Number:', policy_number],  # PHI
            ['8. Group Number:', group_number],
        ]

        # Track PHI
        phi_annotations.extend([
            {'type': 'name', 'value': patient_info['name'], 'page': 1},
            {'type': 'date', 'value': patient_info['birth_date'], 'page': 1},
            {'type': 'ssn', 'value': patient_info['ssn'], 'page': 1},
            {'type': 'address', 'value': patient_info['address'], 'page': 1},
            {'type': 'phone', 'value': patient_info['phone'], 'page': 1},
            {'type': 'email', 'value': patient_info['email'], 'page': 1},
            {'type': 'insurance_id', 'value': policy_number, 'page': 1},
        ])

        patient_table = Table(patient_data, colWidths=[2 * inch, 4 * inch])
        patient_table.setStyle(TableStyle([
            ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
            ('FONTSIZE', (0, 0), (-1, -1), 9),
            ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
            ('VALIGN', (0, 0), (-1, -1), 'TOP'),
        ]))
        story.append(patient_table)
        story.append(Spacer(1, 0.2 * inch))

        # Provider information
        story.append(Paragraph("<b>PROVIDER INFORMATION</b>", styles['Heading3']))

        provider_name = f"Dr. {self.faker.name()}"
        provider_npi = f"{self.faker.random_number(digits=10, fix_len=True)}"
        provider_tax_id = f"{self.faker.random_number(digits=2, fix_len=True)}-{self.faker.random_number(digits=7, fix_len=True)}"
        facility_name = f"{self.faker.company()} Medical Center"
        facility_address = self.faker.address().replace('\n', ', ')

        provider_data = [
            ['Provider Name:', provider_name],  # PHI
            ['NPI:', provider_npi],
            ['Tax ID:', provider_tax_id],
            ['Facility:', facility_name],
            ['Facility Address:', facility_address],  # PHI
        ]

        phi_annotations.extend([
            {'type': 'name', 'value': provider_name, 'page': 1},
            {'type': 'address', 'value': facility_address, 'page': 1},
        ])

        provider_table = Table(provider_data, colWidths=[2 * inch, 4 * inch])
        provider_table.setStyle(TableStyle([
            ('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'),
            ('FONTSIZE', (0, 0), (-1, -1), 9),
            ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
        ]))
        story.append(provider_table)
        story.append(Spacer(1, 0.2 * inch))

        # Diagnosis and procedures
        story.append(Paragraph("<b>DIAGNOSIS AND TREATMENT</b>", styles['Heading3']))

        # Add diagnosis codes
        diagnosis_data = [
            ['ICD-10 Code', 'Description'],
        ]

        # Use actual conditions or generate synthetic ones
        if patient_info['conditions']:
            for condition in patient_info['conditions'][:4]:
                icd_code = f"{chr(random.randint(65, 90))}{random.randint(10, 99)}.{random.randint(0, 9)}"
                diagnosis_data.append([icd_code, condition['code']])
        else:
            diagnosis_data.extend([
                ['J06.9', 'Acute upper respiratory infection'],
                ['R50.9', 'Fever, unspecified'],
            ])

        diagnosis_table = Table(diagnosis_data, colWidths=[1.5 * inch, 4.5 * inch])
        diagnosis_table.setStyle(TableStyle([
            ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
            ('FONTSIZE', (0, 0), (-1, -1), 9),
            ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
            ('BACKGROUND', (0, 0), (-1, 0), colors.lightgrey),
        ]))
        story.append(diagnosis_table)
        story.append(Spacer(1, 0.2 * inch))

        # Service lines
        story.append(Paragraph("<b>SERVICES PROVIDED</b>", styles['Heading3']))

        service_data = [
            ['Date', 'CPT Code', 'Description', 'Units', 'Charge'],
        ]

        # Generate service lines
        service_date = datetime.now() - timedelta(days=random.randint(1, 30))
        services = [
            ('99213', 'Office visit, established patient', 1, 150.00),
            ('80053', 'Comprehensive metabolic panel', 1, 85.00),
            ('85025', 'Complete blood count', 1, 45.00),
        ]

        total_charges = 0
        for cpt, desc, units, charge in services:
            service_data.append([
                service_date.strftime('%m/%d/%Y'),  # PHI: Date
                cpt,
                desc,
                str(units),
                f"${charge:.2f}",
            ])
            total_charges += charge
            phi_annotations.append({'type': 'date', 'value': service_date.strftime('%Y-%m-%d'), 'page': 1})

        service_data.append(['', '', '', 'TOTAL:', f"${total_charges:.2f}"])

        service_table = Table(service_data, colWidths=[1 * inch, 1 * inch, 2.5 * inch, 0.75 * inch, 0.75 * inch])
        service_table.setStyle(TableStyle([
            ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
            ('FONTNAME', (3, -1), (-1, -1), 'Helvetica-Bold'),
            ('FONTSIZE', (0, 0), (-1, -1), 9),
            ('GRID', (0, 0), (-1, -2), 0.5, colors.black),
            ('BACKGROUND', (0, 0), (-1, 0), colors.lightgrey),
            ('ALIGN', (3, 0), (-1, -1), 'RIGHT'),
        ]))
        story.append(service_table)
        story.append(Spacer(1, 0.3 * inch))

        # Signature section
        signature_date = datetime.now().strftime('%Y-%m-%d')
        phi_annotations.append({'type': 'date', 'value': signature_date, 'page': 1})

        signature_text = f"""
        <para>
        I authorize payment of medical benefits to the provider.<br/><br/>
        Patient Signature: _______________________________ Date: {signature_date}<br/><br/>
        Provider Signature: ______________________________ Date: {signature_date}
        </para>
        """
        story.append(Paragraph(signature_text, styles['Normal']))

        # Build PDF
        doc.build(story)

        return phi_annotations

    def add_realistic_noise(self, pdf_path: Path) -> None:
        """Add realistic scanning artifacts to make PDFs look more authentic."""
        # This would add noise, slight rotation, blur, etc.
        # For now, we'll keep the PDFs clean for better initial training
        pass

    def save_annotations(self, pdf_name: str, annotations: List[Dict]) -> None:
        """Save PHI annotations for a PDF."""
        annotation_file = self.annotations_dir / f"{pdf_name}.json"

        with open(annotation_file, 'w') as f:
            json.dump({
                'document': pdf_name,
                'annotations': annotations,
                'timestamp': datetime.now().isoformat(),
            }, f, indent=2)

    def generate_pdfs_from_synthea(self, num_documents: int = 100) -> None:
        """Generate multiple PDFs from Synthea data."""
        print(f"Generating {num_documents} medical PDFs with PHI...")

        # Find Synthea FHIR bundles
        fhir_dir = self.synthea_dir / 'fhir'
        if not fhir_dir.exists():
            print(f"Error: FHIR directory not found at {fhir_dir}")
            print("Please run Synthea first to generate patient data.")
            return

        patient_files = list(fhir_dir.glob('*.json'))
        if not patient_files:
            print(f"No patient files found in {fhir_dir}")
            return

        print(f"Found {len(patient_files)} patient files")

        # Generate PDFs
        for i in range(min(num_documents, len(patient_files) * len(self.document_types))):
            patient_file = random.choice(patient_files)
            doc_type = random.choice(self.document_types)

            try:
                # Load patient data
                bundle = self.load_synthea_patient(patient_file)
                patient_info = self.extract_patient_info(bundle)

                # Generate filename
                timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
                pdf_name = f"{doc_type}_{i:04d}_{timestamp}.pdf"
                pdf_path = self.output_dir / pdf_name

                # Generate PDF based on type
                if doc_type == 'prescription':
                    annotations = self.create_prescription(patient_info, pdf_path)
                elif doc_type == 'lab_report':
                    annotations = self.create_lab_report(patient_info, pdf_path)
                elif doc_type == 'insurance_claim':
                    annotations = self.create_insurance_claim(patient_info, pdf_path)
                else:
                    # For other types, use a default template
                    annotations = self.create_prescription(patient_info, pdf_path)

                # Save annotations
                self.save_annotations(pdf_name, annotations)

                print(f"✓ Generated {pdf_name} with {len(annotations)} PHI annotations")

            except Exception as e:
                print(f"✗ Error generating PDF {i}: {e}")
                continue

        print(f"\nGeneration complete!")
        print(f"PDFs saved to: {self.output_dir}")
        print(f"Annotations saved to: {self.annotations_dir}")


def main():
    """Main function to run PDF generation."""
    import argparse

    parser = argparse.ArgumentParser(description='Generate medical PDFs from Synthea data')
    parser.add_argument('--synthea-output', type=str, default='./data/synthetic/synthea',
                        help='Path to Synthea output directory')
    parser.add_argument('--pdf-output', type=str, default='./data/pdfs',
                        help='Directory to save generated PDFs')
    parser.add_argument('--annotations-output', type=str, default='./data/annotations',
                        help='Directory to save PHI annotations')
    parser.add_argument('--num-documents', type=int, default=100,
                        help='Number of PDFs to generate')

    args = parser.parse_args()

    # Create generator
    generator = MedicalPDFGenerator(
        synthea_dir=args.synthea_output,
        output_dir=args.pdf_output,
        annotations_dir=args.annotations_output
    )

    # Generate PDFs
    generator.generate_pdfs_from_synthea(args.num_documents)


if __name__ == '__main__':
    main()