Skill: mtsatryan/ah-healthcare-dev (version 1.0.0)

--- name: healthcare-dev description: 'You are a healthcare technology specialist with deep expertise in medical software development, regulatory compliance, interoperability. Use when: 1. healthcare standards & interoperability, 2. regulatory compliance, 3. ehr/emr systems, 4. medical device integration, 5. healthcare analytics & ai.' --- # Healthcare Dev You are a healthcare technology specialist with deep expertise in medical software development, regulatory compliance, interoperability standards, and patient data security. Your knowledge spans electronic health records (EHR), medical device integration, telemedicine platforms, and healthcare analytics while maintaining strict compliance with HIPAA, GDPR, and other healthcare regulations. ## Core Expertise ### 1. Healthcare Standards & Interoperability - **HL7 Standards**: HL7 v2.x messaging, HL7 v3 RIM, CDA (Clinical Document Architecture) - **FHIR**: Fast Healthcare Interoperability Resources R4/R5, SMART on FHIR - **DICOM**: Medical imaging standards, PACS integration, image processing - **IHE Profiles**: XDS, PIX, PDQ, XCA for health information exchange - **Terminology Standards**: SNOMED CT, LOINC, ICD-10, CPT, RxNorm ### 2. Regulatory Compliance - **HIPAA**: Privacy Rule, Security Rule, Breach Notification, Minimum Necessary - **FDA Regulations**: 21 CFR Part 11, Medical Device Software (SaMD), 510(k) submissions - **GDPR**: EU data protection for health data - **Regional Compliance**: PIPEDA (Canada), HITECH Act (US), NHS standards (UK) - **Audit Controls**: Access logs, data integrity, electronic signatures ### 3. EHR/EMR Systems - **Major Platforms**: Epic, Cerner, Allscripts, athenahealth integration - **Clinical Workflows**: CPOE, e-prescribing, clinical decision support - **Patient Portals**: Secure messaging, appointment scheduling, lab results - **Interoperability**: Health Information Exchanges (HIE), Care Everywhere - **Data Migration**: Legacy system transitions, data mapping, validation ### 4. Medical Device Integration - **Device Protocols**: IEEE 11073, Bluetooth LE for medical devices - **Wearables**: Continuous monitoring, remote patient monitoring (RPM) - **Medical IoT**: Device management, firmware updates, security - **FDA Classes**: Class I, II, III device software requirements - **Real-time Monitoring**: Vital signs, alerts, nurse call systems ### 5. Healthcare Analytics & AI - **Clinical Analytics**: Population health, risk stratification, quality measures - **Medical Imaging AI**: Computer-aided diagnosis, image segmentation - **NLP for Healthcare**: Clinical notes extraction, medical coding automation - **Predictive Analytics**: Readmission risk, disease progression, treatment outcomes - **Research Platforms**: Clinical trials management, REDCap integration ## Implementation Examples ### FHIR-Compliant EHR System (TypeScript/Node.js) > 📎 **Code example 1** (typescript) — see [references/examples.md](references/examples.md) ## Best Practices ### 1. Regulatory Compliance - Implement comprehensive HIPAA Security Rule controls - Maintain detailed audit logs for all PHI access - Use encryption for data at rest and in transit - Implement role-based access control (RBAC) - Regular security risk assessments ### 2. Interoperability - Follow HL7 FHIR standards for data exchange - Implement standard terminologies (SNOMED, LOINC, ICD) - Support multiple exchange protocols (HL7 v2, FHIR, CDA) - Validate all incoming and outgoing messages - Maintain mapping tables for code systems ### 3. Data Security - Implement defense in depth strategy - Use secure key management systems - Regular security audits and penetration testing - Implement data loss prevention (DLP) measures - Maintain business associate agreements (BAAs) ### 4. Clinical Safety - Implement clinical decision support carefully - Validate all medical calculations - Maintain drug interaction databases - Implement allergy checking - Provide clear audit trails for clinical decisions ### 5. Performance & Reliability - Design for high availability (99.99% uptime) - Implement disaster recovery procedures - Use caching for frequently accessed data - Optimize database queries for large datasets - Implement proper backup and recovery ## Common Patterns 1. **Audit Trail**: Comprehensive logging of all PHI access 2. **Break Glass**: Emergency access procedures with extra auditing 3. **Consent Management**: Patient consent tracking and enforcement 4. **Master Patient Index**: Patient identity management and matching 5. **Clinical Repository**: Centralized storage for clinical data 6. **Terminology Services**: Code system mapping and validation 7. **Order Entry**: Computerized physician order entry (CPOE) 8. **Results Routing**: Laboratory and imaging result distribution Remember: Healthcare technology requires extreme attention to patient safety, data privacy, and regulatory compliance. Always consult with clinical, legal, and compliance teams when implementing healthcare systems. --- ## Reference Materials For detailed code examples and implementation patterns, see [references/examples.md](references/examples.md).

path: references/examples.md
content: # Healthcare Dev — Code Examples

## Example 1

```typescript
import express, { Request, Response, NextFunction } from 'express';
import { v4 as uuidv4 } from 'uuid';
import crypto from 'crypto';
import jwt from 'jsonwebtoken';
import { Pool } from 'pg';
import Redis from 'ioredis';
import winston from 'winston';
import { z } from 'zod';
import hl7 from 'hl7-standard';
import dicom from 'dicom-parser';

/**
 * FHIR R4 Compliant Electronic Health Record System
 * HIPAA-compliant implementation with comprehensive security and audit logging
 */

// FHIR Resource Types
enum ResourceType {
    Patient = 'Patient',
    Practitioner = 'Practitioner',
    Encounter = 'Encounter',
    Observation = 'Observation',
    Medication = 'Medication',
    MedicationRequest = 'MedicationRequest',
    Condition = 'Condition',
    Procedure = 'Procedure',
    DiagnosticReport = 'DiagnosticReport',
    AllergyIntolerance = 'AllergyIntolerance',
    Immunization = 'Immunization',
    CarePlan = 'CarePlan',
}

// HIPAA Audit Event Types
enum AuditEventType {
    CREATE = 'C',
    READ = 'R',
    UPDATE = 'U',
    DELETE = 'D',
    EXECUTE = 'E',
    LOGIN = 'LOGIN',
    LOGOUT = 'LOGOUT',
    EMERGENCY_ACCESS = 'EMERGENCY',
}

// Configuration
const config = {
    hipaa: {
        encryptionAlgorithm: 'aes-256-gcm',
        keyRotationDays: 90,
        sessionTimeout: 900000, // 15 minutes
        passwordComplexity: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$/,
        maxLoginAttempts: 3,
        auditRetentionYears: 7,
    },
    fhir: {
        version: 'R4',
        baseUrl: process.env.FHIR_BASE_URL || 'https://api.healthcare.org/fhir',
        supportedFormats: ['application/fhir+json', 'application/fhir+xml'],
    },
    security: {
        jwtSecret: process.env.JWT_SECRET!,
        jwtExpiry: '1h',
        mfaRequired: true,
        breakGlassEnabled: true,
    },
};

// Database with encryption at rest
const db = new Pool({
    host: process.env.DB_HOST,
    port: parseInt(process.env.DB_PORT || '5432'),
    database: process.env.DB_NAME,
    user: process.env.DB_USER,
    [REDACTED_SECRET],
    ssl: {
        rejectUnauthorized: true,
        ca: process.env.DB_CA_CERT,
    },
    max: 20,
    idleTimeoutMillis: 30000,
});

// Redis for caching and session management
const redis = new Redis({
    host: process.env.REDIS_HOST,
    port: parseInt(process.env.REDIS_PORT || '6379'),
    [REDACTED_SECRET],
    tls: {
        rejectUnauthorized: true,
    },
});

// HIPAA-compliant audit logger
const auditLogger = winston.createLogger({
    level: 'info',
    format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.json()
    ),
    transports: [
        new winston.transports.File({ 
            filename: 'hipaa-audit.log',
            maxsize: 10485760, // 10MB
            maxFiles: 100,
            options: { flags: 'a' }
        }),
        new winston.transports.Console({
            format: winston.format.simple()
        })
    ],
});

// FHIR Resource Interfaces
interface FHIRResource {
    resourceType: ResourceType;
    id?: string;
    meta?: {
        versionId: string;
        lastUpdated: string;
        profile?: string[];
        security?: Coding[];
        tag?: Coding[];
    };
}

interface Patient extends FHIRResource {
    resourceType: ResourceType.Patient;
    identifier?: Identifier[];
    active?: boolean;
    name?: HumanName[];
    telecom?: ContactPoint[];
    gender?: 'male' | 'female' | 'other' | 'unknown';
    birthDate?: string;
    deceasedBoolean?: boolean;
    deceasedDateTime?: string;
    address?: Address[];
    maritalStatus?: CodeableConcept;
    multipleBirthBoolean?: boolean;
    multipleBirthInteger?: number;
    photo?: Attachment[];
    contact?: PatientContact[];
    communication?: PatientCommunication[];
    generalPractitioner?: Reference[];
    managingOrganization?: Reference;
}

interface Observation extends FHIRResource {
    resourceType: ResourceType.Observation;
    status: 'registered' | 'preliminary' | 'final' | 'amended' | 'corrected' | 'cancelled' | 'entered-in-error';
    category?: CodeableConcept[];
    code: CodeableConcept;
    subject?: Reference;
    encounter?: Reference;
    effectiveDateTime?: string;
    effectivePeriod?: Period;
    issued?: string;
    performer?: Reference[];
    valueQuantity?: Quantity;
    valueCodeableConcept?: CodeableConcept;
    valueString?: string;
    valueBoolean?: boolean;
    valueInteger?: number;
    valueRange?: Range;
    interpretation?: CodeableConcept[];
    note?: Annotation[];
    referenceRange?: ObservationReferenceRange[];
}

// HIPAA Security Service
class HIPAASecurityService {
    private encryptionKey: Buffer;
    
    constructor() {
        this.encryptionKey = this.deriveKey();
        this.scheduleKeyRotation();
    }
    
    private deriveKey(): Buffer {
        const masterKey = process.env.MASTER_KEY!;
        const salt = process.env.KEY_SALT!;
        return crypto.pbkdf2Sync(masterKey, salt, 100000, 32, 'sha256');
    }
    
    private scheduleKeyRotation() {
        setInterval(() => {
            this.rotateEncryptionKey();
        }, config.hipaa.keyRotationDays * 24 * 60 * 60 * 1000);
    }
    
    private async rotateEncryptionKey() {
        // Generate new key
        const newKey = crypto.randomBytes(32);
        
        // Re-encrypt all sensitive data with new key
        await this.reencryptData(newKey);
        
        // Update key in secure key management system
        this.encryptionKey = newKey;
        
        auditLogger.info('Encryption key rotated', {
            timestamp: new Date().toISOString(),
            keyVersion: crypto.createHash('sha256').update(newKey).digest('hex').substring(0, 8),
        });
    }
    
    encryptPHI(data: string): { encrypted: string; iv: string; tag: string } {
        const iv = crypto.randomBytes(16);
        const cipher = crypto.createCipheriv(config.hipaa.encryptionAlgorithm, this.encryptionKey, iv);
        
        let encrypted = cipher.update(data, 'utf8', 'hex');
        encrypted += cipher.final('hex');
        
        const tag = (cipher as any).getAuthTag();
        
        return {
            encrypted,
            iv: iv.toString('hex'),
            tag: tag.toString('hex'),
        };
    }
    
    decryptPHI(encrypted: string, iv: string, tag: string): string {
        const decipher = crypto.createDecipheriv(
            config.hipaa.encryptionAlgorithm,
            this.encryptionKey,
            Buffer.from(iv, 'hex')
        );
        
        (decipher as any).setAuthTag(Buffer.from(tag, 'hex'));
        
        let decrypted = decipher.update(encrypted, 'hex', 'utf8');
        decrypted += decipher.final('utf8');
        
        return decrypted;
    }
    
    async reencryptData(newKey: Buffer) {
        // Implementation for re-encrypting existing data
        const client = await db.connect();
        try {
            await client.query('BEGIN');
            
            // Re-encrypt patient data
            const patients = await client.query('SELECT * FROM patients WHERE encrypted = true');
            for (const patient of patients.rows) {
                const decrypted = this.decryptPHI(
                    patient.encrypted_data,
                    patient.encryption_iv,
                    patient.encryption_tag
                );
                
                const reencrypted = this.encryptWithKey(decrypted, newKey);
                
                await client.query(
                    'UPDATE patients SET encrypted_data = $1, encryption_iv = $2, encryption_tag = $3 WHERE id = $4',
                    [reencrypted.encrypted, reencrypted.iv, reencrypted.tag, patient.id]
                );
            }
            
            await client.query('COMMIT');
        } catch (error) {
            await client.query('ROLLBACK');
            throw error;
        } finally {
            client.release();
        }
    }
    
    private encryptWithKey(data: string, key: Buffer) {
        const iv = crypto.randomBytes(16);
        const cipher = crypto.createCipheriv(config.hipaa.encryptionAlgorithm, key, iv);
        
        let encrypted = cipher.update(data, 'utf8', 'hex');
        encrypted += cipher.final('hex');
        
        const tag = (cipher as any).getAuthTag();
        
        return {
            encrypted,
            iv: iv.toString('hex'),
            tag: tag.toString('hex'),
        };
    }
    
    hashPassword([REDACTED_SECRET]): string {
        const salt = crypto.randomBytes(16).toString('hex');
        const hash = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha512').toString('hex');
        return `${salt}:${hash}`;
    }
    
    verifyPassword([REDACTED_SECRET], hashedPassword: string): boolean {
        const [salt, hash] = hashedPassword.split(':');
        const verifyHash = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha512').toString('hex');
        return hash === verifyHash;
    }
    
    generateSessionToken(userId: string, role: string): string {
        return jwt.sign(
            {
                userId,
                role,
                sessionId: uuidv4(),
                iat: Math.floor(Date.now() / 1000),
            },
            config.security.jwtSecret,
            { expiresIn: config.security.jwtExpiry }
        );
    }
    
    verifySessionToken([REDACTED_SECRET]): any {
        try {
            return jwt.verify(token, config.security.jwtSecret);
        } catch (error) {
            return null;
        }
    }
}

// HIPAA Audit Service
class HIPAAAuditService {
    async logAccess(
        userId: string,
        patientId: string,
        resourceType: string,
        action: AuditEventType,
        outcome: 'success' | 'failure',
        reason?: string
    ) {
        const auditEntry = {
            timestamp: new Date().toISOString(),
            userId,
            patientId,
            resourceType,
            action,
            outcome,
            reason,
            ipAddress: this.getClientIp(),
            userAgent: this.getUserAgent(),
            sessionId: this.getSessionId(),
        };
        
        // Log to audit log
        auditLogger.info('PHI Access', auditEntry);
        
        // Store in database for long-term retention
        await db.query(
            `INSERT INTO audit_log 
             (timestamp, user_id, patient_id, resource_type, action, outcome, reason, ip_address, user_agent, session_id)
             VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`,
            [
                auditEntry.timestamp,
                auditEntry.userId,
                auditEntry.patientId,
                auditEntry.resourceType,
                auditEntry.action,
                auditEntry.outcome,
                auditEntry.reason,
                auditEntry.ipAddress,
                auditEntry.userAgent,
                auditEntry.sessionId,
            ]
        );
        
        // Real-time alerting for suspicious activity
        if (outcome === 'failure') {
            await this.checkSuspiciousActivity(userId);
        }
    }
    
    async checkSuspiciousActivity(userId: string) {
        const recentFailures = await db.query(
            `SELECT COUNT(*) as count 
             FROM audit_log 
             WHERE user_id = $1 
             AND outcome = 'failure' 
             AND timestamp > NOW() - INTERVAL '15 minutes'`,
            [userId]
        );
        
        if (recentFailures.rows[0].count >= 5) {
            // Alert security team
            await this.sendSecurityAlert({
                type: 'SUSPICIOUS_ACTIVITY',
                userId,
                message: 'Multiple failed access attempts detected',
                severity: 'HIGH',
            });
            
            // Temporarily lock account
            await this.lockUserAccount(userId);
        }
    }
    
    async sendSecurityAlert(alert: any) {
        // Send to security monitoring system
        // Implementation would integrate with SIEM
        auditLogger.warn('Security Alert', alert);
    }
    
    async lockUserAccount(userId: string) {
        await db.query(
            'UPDATE users SET locked = true, lock_reason = $1, locked_at = NOW() WHERE id = $2',
            ['Suspicious activity detected', userId]
        );
    }
    
    private getClientIp(): string {
        // Get from request context
        return '127.0.0.1'; // Placeholder
    }
    
    private getUserAgent(): string {
        // Get from request headers
        return 'Mozilla/5.0'; // Placeholder
    }
    
    private getSessionId(): string {
        // Get from session context
        return uuidv4(); // Placeholder
    }
}

// FHIR Resource Repository
class FHIRRepository {
    private security = new HIPAASecurityService();
    private audit = new HIPAAAuditService();
    
    async createResource(
        resource: FHIRResource,
        userId: string,
        reason?: string
    ): Promise<FHIRResource> {
        const client = await db.connect();
        
        try {
            await client.query('BEGIN');
            
            // Generate resource ID and metadata
            resource.id = resource.id || uuidv4();
            resource.meta = {
                versionId: '1',
                lastUpdated: new Date().toISOString(),
                profile: this.getResourceProfiles(resource.resourceType),
            };
            
            // Encrypt sensitive data
            const encryptedData = this.security.encryptPHI(JSON.stringify(resource));
            
            // Store resource
            await client.query(
                `INSERT INTO fhir_resources 
                 (id, resource_type, version, data, encrypted_data, encryption_iv, encryption_tag, created_at, created_by)
                 VALUES ($1, $2, $3, $4, $5, $6, $7, NOW(), $8)`,
                [
                    resource.id,
                    resource.resourceType,
                    1,
                    null, // Store encrypted only
                    encryptedData.encrypted,
                    encryptedData.iv,
                    encryptedData.tag,
                    userId,
                ]
            );
            
            // Create audit log
            await this.audit.logAccess(
                userId,
                this.getPatientId(resource),
                resource.resourceType,
                AuditEventType.CREATE,
                'success',
                reason
            );
            
            await client.query('COMMIT');
            
            return resource;
        } catch (error) {
            await client.query('ROLLBACK');
            
            await this.audit.logAccess(
                userId,
                this.getPatientId(resource),
                resource.resourceType,
                AuditEventType.CREATE,
                'failure',
                error.message
            );
            
            throw error;
        } finally {
            client.release();
        }
    }
    
    async readResource(
        resourceType: ResourceType,
        id: string,
        userId: string,
        reason?: string
    ): Promise<FHIRResource | null> {
        try {
            // Check access permissions
            const hasAccess = await this.checkAccess(userId, resourceType, id);
            if (!hasAccess) {
                await this.audit.logAccess(
                    userId,
                    id,
                    resourceType,
                    AuditEventType.READ,
                    'failure',
                    'Access denied'
                );
                throw new Error('Access denied');
            }
            
            // Retrieve resource
            const result = await db.query(
                'SELECT * FROM fhir_resources WHERE id = $1 AND resource_type = $2',
                [id, resourceType]
            );
            
            if (result.rows.length === 0) {
                return null;
            }
            
            const row = result.rows[0];
            
            // Decrypt resource
            const decrypted = this.security.decryptPHI(
                row.encrypted_data,
                row.encryption_iv,
                row.encryption_tag
            );
            
            const resource = JSON.parse(decrypted);
            
            // Log access
            await this.audit.logAccess(
                userId,
                this.getPatientId(resource),
                resourceType,
                AuditEventType.READ,
                'success',
                reason
            );
            
            return resource;
        } catch (error) {
            await this.audit.logAccess(
                userId,
                id,
                resourceType,
                AuditEventType.READ,
                'failure',
                error.message
            );
            throw error;
        }
    }
    
    async updateResource(
        resource: FHIRResource,
        userId: string,
        reason?: string
    ): Promise<FHIRResource> {
        const client = await db.connect();
        
        try {
            await client.query('BEGIN');
            
            // Get current version
            const current = await this.readResource(
                resource.resourceType,
                resource.id!,
                userId,
                'Update operation'
            );
            
            if (!current) {
                throw new Error('Resource not found');
            }
            
            // Update metadata
            const newVersion = parseInt(current.meta!.versionId) + 1;
            resource.meta = {
                ...resource.meta,
                versionId: newVersion.toString(),
                lastUpdated: new Date().toISOString(),
            };
            
            // Archive current version
            await client.query(
                `INSERT INTO fhir_resource_history 
                 SELECT * FROM fhir_resources WHERE id = $1`,
                [resource.id]
            );
            
            // Encrypt and update
            const encryptedData = this.security.encryptPHI(JSON.stringify(resource));
            
            await client.query(
                `UPDATE fhir_resources 
                 SET version = $1, encrypted_data = $2, encryption_iv = $3, 
                     encryption_tag = $4, updated_at = NOW(), updated_by = $5
                 WHERE id = $6`,
                [
                    newVersion,
                    encryptedData.encrypted,
                    encryptedData.iv,
                    encryptedData.tag,
                    userId,
                    resource.id,
                ]
            );
            
            // Audit log
            await this.audit.logAccess(
                userId,
                this.getPatientId(resource),
                resource.resourceType,
                AuditEventType.UPDATE,
                'success',
                reason
            );
            
            await client.query('COMMIT');
            
            return resource;
        } catch (error) {
            await client.query('ROLLBACK');
            
            await this.audit.logAccess(
                userId,
                resource.id!,
                resource.resourceType,
                AuditEventType.UPDATE,
                'failure',
                error.message
            );
            
            throw error;
        } finally {
            client.release();
        }
    }
    
    async searchResources(
        resourceType: ResourceType,
        params: any,
        userId: string
    ): Promise<Bundle> {
        // Implement FHIR search with proper access control
        const searchResults: FHIRResource[] = [];
        
        // Build search query based on FHIR search parameters
        let query = `SELECT * FROM fhir_resources WHERE resource_type = $1`;
        const queryParams = [resourceType];
        
        // Add search parameters
        if (params.patient) {
            query += ` AND data->>'subject'->>'reference' = $${queryParams.length + 1}`;
            queryParams.push(`Patient/${params.patient}`);
        }
        
        if (params.date) {
            // Handle date search
        }
        
        // Execute search
        const results = await db.query(query, queryParams);
        
        // Decrypt and filter based on access
        for (const row of results.rows) {
            if (await this.checkAccess(userId, resourceType, row.id)) {
                const decrypted = this.security.decryptPHI(
                    row.encrypted_data,
                    row.encryption_iv,
                    row.encryption_tag
                );
                searchResults.push(JSON.parse(decrypted));
            }
        }
        
        // Create FHIR Bundle
        return {
            resourceType: 'Bundle',
            type: 'searchset',
            total: searchResults.length,
            entry: searchResults.map(resource => ({
                fullUrl: `${config.fhir.baseUrl}/${resource.resourceType}/${resource.id}`,
                resource,
            })),
        };
    }
    
    private async checkAccess(userId: string, resourceType: string, resourceId: string): Promise<boolean> {
        // Implement role-based access control
        const user = await db.query('SELECT role, department FROM users WHERE id = $1', [userId]);
        
        if (user.rows.length === 0) {
            return false;
        }
        
        const { role, department } = user.rows[0];
        
        // Check role-based permissions
        if (role === 'admin') {
            return true;
        }
        
        if (role === 'physician') {
            // Check if physician has relationship with patient
            return await this.checkPhysicianPatientRelationship(userId, resourceId);
        }
        
        if (role === 'nurse') {
            // Check department and shift
            return await this.checkNurseAccess(userId, resourceId, department);
        }
        
        return false;
    }
    
    private async checkPhysicianPatientRelationship(physicianId: string, patientId: string): Promise<boolean> {
        const result = await db.query(
            `SELECT COUNT(*) as count 
             FROM patient_physician_relationships 
             WHERE physician_id = $1 AND patient_id = $2 AND active = true`,
            [physicianId, patientId]
        );
        
        return result.rows[0].count > 0;
    }
    
    private async checkNurseAccess(nurseId: string, patientId: string, department: string): Promise<boolean> {
        // Check if patient is in nurse's department
        const result = await db.query(
            `SELECT COUNT(*) as count 
             FROM patient_admissions 
             WHERE patient_id = $1 AND department = $2 AND discharged_at IS NULL`,
            [patientId, department]
        );
        
        return result.rows[0].count > 0;
    }
    
    private getResourceProfiles(resourceType: ResourceType): string[] {
        // Return US Core profiles
        const profiles: { [key: string]: string[] } = {
            [ResourceType.Patient]: ['http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient'],
            [ResourceType.Observation]: ['http://hl7.org/fhir/us/core/StructureDefinition/us-core-observation'],
            [ResourceType.Condition]: ['http://hl7.org/fhir/us/core/StructureDefinition/us-core-condition'],
        };
        
        return profiles[resourceType] || [];
    }
    
    private getPatientId(resource: FHIRResource): string {
        if (resource.resourceType === ResourceType.Patient) {
            return resource.id!;
        }
        
        // Extract patient reference from other resources
        const resourceWithSubject = resource as any;
        if (resourceWithSubject.subject?.reference) {
            return resourceWithSubject.subject.reference.replace('Patient/', '');
        }
        
        return 'unknown';
    }
}

// Clinical Decision Support
class ClinicalDecisionSupport {
    async checkDrugInteractions(medications: Medication[]): Promise<Alert[]> {
        const alerts: Alert[] = [];
        
        // Check for drug-drug interactions
        for (let i = 0; i < medications.length; i++) {
            for (let j = i + 1; j < medications.length; j++) {
                const interaction = await this.checkInteraction(
                    medications[i],
                    medications[j]
                );
                
                if (interaction) {
                    alerts.push({
                        severity: interaction.severity,
                        type: 'drug-interaction',
                        message: interaction.message,
                        medications: [medications[i].id!, medications[j].id!],
                    });
                }
            }
        }
        
        return alerts;
    }
    
    async checkAllergies(patient: Patient, medication: Medication): Promise<Alert[]> {
        const alerts: Alert[] = [];
        
        // Get patient allergies
        const allergies = await this.getPatientAllergies(patient.id!);
        
        for (const allergy of allergies) {
            if (this.medicationContainsAllergen(medication, allergy)) {
                alerts.push({
                    severity: 'high',
                    type: 'allergy',
                    message: `Patient is allergic to ${allergy.substance}`,
                    allergyId: allergy.id,
                    medicationId: medication.id!,
                });
            }
        }
        
        return alerts;
    }
    
    async checkDosing(
        medication: Medication,
        patient: Patient,
        renalFunction?: number,
        hepaticFunction?: number
    ): Promise<Alert[]> {
        const alerts: Alert[] = [];
        
        // Calculate age
        const age = this.calculateAge(patient.birthDate!);
        
        // Check pediatric dosing
        if (age < 18) {
            const pediatricDose = await this.getPediatricDosing(medication, age, patient);
            if (pediatricDose) {
                alerts.push({
                    severity: 'medium',
                    type: 'dosing',
                    message: `Recommended pediatric dose: ${pediatricDose}`,
                });
            }
        }
        
        // Check geriatric dosing
        if (age > 65) {
            const geriatricDose = await this.getGeriatricDosing(medication, age);
            if (geriatricDose) {
                alerts.push({
                    severity: 'medium',
                    type: 'dosing',
                    message: `Consider dose adjustment for geriatric patient: ${geriatricDose}`,
                });
            }
        }
        
        // Check renal dosing
        if (renalFunction && renalFunction < 60) {
            const renalDose = await this.getRenalDosing(medication, renalFunction);
            if (renalDose) {
                alerts.push({
                    severity: 'high',
                    type: 'dosing',
                    message: `Renal dose adjustment needed: ${renalDose}`,
                });
            }
        }
        
        // Check hepatic dosing
        if (hepaticFunction && hepaticFunction < 70) {
            const hepaticDose = await this.getHepaticDosing(medication, hepaticFunction);
            if (hepaticDose) {
                alerts.push({
                    severity: 'high',
                    type: 'dosing',
                    message: `Hepatic dose adjustment needed: ${hepaticDose}`,
                });
            }
        }
        
        return alerts;
    }
    
    private async checkInteraction(med1: Medication, med2: Medication) {
        // Query drug interaction database
        // This would integrate with a service like First Databank or Micromedex
        return null; // Placeholder
    }
    
    private async getPatientAllergies(patientId: string) {
        const result = await db.query(
            'SELECT * FROM allergies WHERE patient_id = $1 AND active = true',
            [patientId]
        );
        return result.rows;
    }
    
    private medicationContainsAllergen(medication: Medication, allergy: any): boolean {
        // Check if medication contains allergen
        return false; // Placeholder
    }
    
    private calculateAge(birthDate: string): number {
        const birth = new Date(birthDate);
        const today = new Date();
        let age = today.getFullYear() - birth.getFullYear();
        const monthDiff = today.getMonth() - birth.getMonth();
        
        if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
            age--;
        }
        
        return age;
    }
    
    private async getPediatricDosing(medication: Medication, age: number, patient: Patient) {
        // Calculate pediatric dosing based on age and weight
        return null; // Placeholder
    }
    
    private async getGeriatricDosing(medication: Medication, age: number) {
        // Get geriatric dosing recommendations
        return null; // Placeholder
    }
    
    private async getRenalDosing(medication: Medication, gfr: number) {
        // Calculate renal dosing adjustment
        return null; // Placeholder
    }
    
    private async getHepaticDosing(medication: Medication, liverFunction: number) {
        // Calculate hepatic dosing adjustment
        return null; // Placeholder
    }
}

// HL7 Message Processing
class HL7Processor {
    async processMessage(message: string): Promise<void> {
        try {
            // Parse HL7 message
            const parsed = hl7.parse(message);
            
            // Extract message type
            const messageType = parsed.MSH[9][0]; // Message type
            
            // Process based on message type
            switch (messageType) {
                case 'ADT': // Admission, Discharge, Transfer
                    await this.processADT(parsed);
                    break;
                case 'ORM': // Order Message
                    await this.processORM(parsed);
                    break;
                case 'ORU': // Observation Result
                    await this.processORU(parsed);
                    break;
                case 'SIU': // Scheduling
                    await this.processSIU(parsed);
                    break;
                default:
                    throw new Error(`Unsupported message type: ${messageType}`);
            }
            
            // Send acknowledgment
            await this.sendACK(parsed);
            
        } catch (error) {
            auditLogger.error('HL7 processing error', {
                error: error.message,
                message: message.substring(0, 100),
            });
            
            // Send NACK
            await this.sendNACK(error.message);
        }
    }
    
    private async processADT(message: any) {
        const eventType = message.MSH[9][1]; // Event type (A01, A02, etc.)
        
        switch (eventType) {
            case 'A01': // Admission
                await this.handleAdmission(message);
                break;
            case 'A03': // Discharge
                await this.handleDischarge(message);
                break;
            case 'A02': // Transfer
                await this.handleTransfer(message);
                break;
        }
    }
    
    private async processORM(message: any) {
        // Process order message
        const order = {
            patientId: message.PID[3][0],
            orderNumber: message.ORC[2][0],
            orderDate: message.ORC[9][0],
            orderingProvider: message.ORC[12][0],
            orderType: message.OBR[4][0],
        };
        
        // Store order in database
        await db.query(
            `INSERT INTO orders (patient_id, order_number, order_date, provider_id, order_type)
             VALUES ($1, $2, $3, $4, $5)`,
            [order.patientId, order.orderNumber, order.orderDate, order.orderingProvider, order.orderType]
        );
    }
    
    private async processORU(message: any) {
        // Process observation result
        const observation = {
            patientId: message.PID[3][0],
            observationId: message.OBR[3][0],
            observationDate: message.OBR[7][0],
            results: [],
        };
        
        // Extract results from OBX segments
        for (const obx of message.OBX || []) {
            observation.results.push({
                type: obx[3][0],
                value: obx[5][0],
                units: obx[6][0],
                referenceRange: obx[7][0],
                abnormalFlag: obx[8][0],
            });
        }
        
        // Convert to FHIR Observation and store
        const fhirObservation = await this.convertToFHIRObservation(observation);
        // Store observation
    }
    
    private async processSIU(message: any) {
        // Process scheduling message
        const appointment = {
            patientId: message.PID[3][0],
            appointmentId: message.SCH[1][0],
            startTime: message.SCH[11][0],
            duration: message.SCH[9][0],
            providerId: message.AIP[3][0],
            location: message.AIL[3][0],
        };
        
        // Store appointment
        await db.query(
            `INSERT INTO appointments 
             (patient_id, appointment_id, start_time, duration, provider_id, location)
             VALUES ($1, $2, $3, $4, $5, $6)`,
            [
                appointment.patientId,
                appointment.appointmentId,
                appointment.startTime,
                appointment.duration,
                appointment.providerId,
                appointment.location,
            ]
        );
    }
    
    private async handleAdmission(message: any) {
        // Handle patient admission
        const admission = {
            patientId: message.PID[3][0],
            admissionDate: message.PV1[44][0],
            department: message.PV1[3][0],
            room: message.PV1[3][2],
            bed: message.PV1[3][3],
            attendingPhysician: message.PV1[7][0],
        };
        
        await db.query(
            `INSERT INTO admissions 
             (patient_id, admission_date, department, room, bed, attending_physician)
             VALUES ($1, $2, $3, $4, $5, $6)`,
            Object.values(admission)
        );
    }
    
    private async handleDischarge(message: any) {
        // Handle patient discharge
        const discharge = {
            patientId: message.PID[3][0],
            dischargeDate: message.PV1[45][0],
            dischargeDisposition: message.PV1[36][0],
        };
        
        await db.query(
            `UPDATE admissions 
             SET discharge_date = $1, discharge_disposition = $2
             WHERE patient_id = $3 AND discharge_date IS NULL`,
            [discharge.dischargeDate, discharge.dischargeDisposition, discharge.patientId]
        );
    }
    
    private async handleTransfer(message: any) {
        // Handle patient transfer
        const transfer = {
            patientId: message.PID[3][0],
            fromDepartment: message.PV1[6][0],
            toDepartment: message.PV1[3][0],
            transferDate: message.EVN[6][0],
        };
        
        await db.query(
            `INSERT INTO transfers 
             (patient_id, from_department, to_department, transfer_date)
             VALUES ($1, $2, $3, $4)`,
            Object.values(transfer)
        );
    }
    
    private async convertToFHIRObservation(hl7Observation: any): Promise<Observation> {
        // Convert HL7 observation to FHIR format
        return {
            resourceType: ResourceType.Observation,
            status: 'final',
            code: {
                coding: [{
                    system: 'http://loinc.org',
                    code: hl7Observation.type,
                }],
            },
            effectiveDateTime: hl7Observation.observationDate,
            valueQuantity: {
                value: parseFloat(hl7Observation.results[0]?.value),
                unit: hl7Observation.results[0]?.units,
            },
        };
    }
    
    private async sendACK(originalMessage: any) {
        // Send HL7 acknowledgment
        const ack = {
            MSH: {
                ...originalMessage.MSH,
                9: ['ACK'],
            },
            MSA: {
                1: 'AA', // Application Accept
                2: originalMessage.MSH[10], // Message control ID
            },
        };
        
        // Send ACK message
    }
    
    private async sendNACK(error: string) {
        // Send negative acknowledgment
        const nack = {
            MSA: {
                1: 'AE', // Application Error
                3: error,
            },
        };
        
        // Send NACK message
    }
}

// DICOM Image Processing
class DICOMProcessor {
    async processImage(buffer: Buffer): Promise<void> {
        try {
            // Parse DICOM file
            const dataSet = dicom.parseDicom(buffer);
            
            // Extract metadata
            const metadata = {
                patientId: dataSet.string('x00100020'),
                patientName: dataSet.string('x00100010'),
                studyInstanceUID: dataSet.string('x0020000d'),
                seriesInstanceUID: dataSet.string('x0020000e'),
                sopInstanceUID: dataSet.string('x00080018'),
                modality: dataSet.string('x00080060'),
                studyDate: dataSet.string('x00080020'),
                studyDescription: dataSet.string('x00081030'),
            };
            
            // Store metadata in database
            await this.storeDICOMMetadata(metadata);
            
            // Store image in PACS
            await this.storeInPACS(buffer, metadata);
            
            // Apply de-identification if needed
            if (process.env.DEIDENTIFY_IMAGES === 'true') {
                await this.deidentifyImage(dataSet);
            }
            
        } catch (error) {
            auditLogger.error('DICOM processing error', {
                error: error.message,
            });
            throw error;
        }
    }
    
    private async storeDICOMMetadata(metadata: any) {
        await db.query(
            `INSERT INTO dicom_studies 
             (patient_id, study_uid, series_uid, sop_uid, modality, study_date, description)
             VALUES ($1, $2, $3, $4, $5, $6, $7)`,
            [
                metadata.patientId,
                metadata.studyInstanceUID,
                metadata.seriesInstanceUID,
                metadata.sopInstanceUID,
                metadata.modality,
                metadata.studyDate,
                metadata.studyDescription,
            ]
        );
    }
    
    private async storeInPACS(buffer: Buffer, metadata: any) {
        // Store image in Picture Archiving and Communication System
        // This would integrate with a PACS server
    }
    
    private async deidentifyImage(dataSet: any) {
        // Remove patient identifying information
        const tagsToRemove = [
            'x00100010', // Patient Name
            'x00100020', // Patient ID
            'x00100030', // Patient Birth Date
            'x00100040', // Patient Sex
            'x00101010', // Patient Age
        ];
        
        for (const tag of tagsToRemove) {
            delete dataSet.elements[tag];
        }
    }
}

// Telemedicine Platform
class TelemedicineService {
    async createVideoConsultation(
        patientId: string,
        providerId: string,
        scheduledTime: Date
    ): Promise<VideoConsultation> {
        // Generate secure room
        const roomId = uuidv4();
        const roomToken = this.generateSecureToken();
        
        // Create consultation record
        const consultation = {
            id: uuidv4(),
            patientId,
            providerId,
            roomId,
            scheduledTime,
            status: 'scheduled',
            patientToken: this.generatePatientToken(roomId, patientId),
            providerToken: this.generateProviderToken(roomId, providerId),
        };
        
        // Store consultation
        await db.query(
            `INSERT INTO video_consultations 
             (id, patient_id, provider_id, room_id, scheduled_time, status, created_at)
             VALUES ($1, $2, $3, $4, $5, $6, NOW())`,
            [
                consultation.id,
                consultation.patientId,
                consultation.providerId,
                consultation.roomId,
                consultation.scheduledTime,
                consultation.status,
            ]
        );
        
        // Send notifications
        await this.sendConsultationNotifications(consultation);
        
        return consultation;
    }
    
    async startConsultation(consultationId: string, userId: string): Promise<void> {
        // Update consultation status
        await db.query(
            'UPDATE video_consultations SET status = $1, started_at = NOW() WHERE id = $2',
            ['in_progress', consultationId]
        );
        
        // Start recording if required for documentation
        if (process.env.RECORD_CONSULTATIONS === 'true') {
            await this.startRecording(consultationId);
        }
        
        // Log for audit
        auditLogger.info('Video consultation started', {
            consultationId,
            userId,
            timestamp: new Date().toISOString(),
        });
    }
    
    async endConsultation(
        consultationId: string,
        userId: string,
        notes?: string
    ): Promise<void> {
        // Update consultation status
        await db.query(
            `UPDATE video_consultations 
             SET status = $1, ended_at = NOW(), clinical_notes = $2 
             WHERE id = $3`,
            ['completed', notes, consultationId]
        );
        
        // Stop recording
        await this.stopRecording(consultationId);
        
        // Generate consultation summary
        await this.generateConsultationSummary(consultationId);
        
        // Create billing record
        await this.createBillingRecord(consultationId);
    }
    
    private generateSecureToken(): string {
        return crypto.randomBytes(32).toString('base64url');
    }
    
    private generatePatientToken(roomId: string, patientId: string): string {
        return jwt.sign(
            {
                roomId,
                patientId,
                role: 'patient',
                permissions: ['join', 'video', 'audio', 'chat'],
            },
            config.security.jwtSecret,
            { expiresIn: '2h' }
        );
    }
    
    private generateProviderToken(roomId: string, providerId: string): string {
        return jwt.sign(
            {
                roomId,
                providerId,
                role: 'provider',
                permissions: ['join', 'video', 'audio', 'chat', 'record', 'screenshare'],
            },
            config.security.jwtSecret,
            { expiresIn: '2h' }
        );
    }
    
    private async sendConsultationNotifications(consultation: any) {
        // Send email/SMS notifications to patient and provider
    }
    
    private async startRecording(consultationId: string) {
        // Start video recording for documentation
        // Must comply with consent and privacy regulations
    }
    
    private async stopRecording(consultationId: string) {
        // Stop and securely store recording
    }
    
    private async generateConsultationSummary(consultationId: string) {
        // Generate clinical summary document
    }
    
    private async createBillingRecord(consultationId: string) {
        // Create billing record for telemedicine consultation
        // Include appropriate CPT codes for telehealth
    }
}

// Type definitions
interface Identifier {
    system?: string;
    value: string;
}

interface HumanName {
    family: string;
    given: string[];
}

interface ContactPoint {
    system: 'phone' | 'email';
    value: string;
}

interface Address {
    line: string[];
    city: string;
    state: string;
    postalCode: string;
    country: string;
}

interface CodeableConcept {
    coding?: Coding[];
    text?: string;
}

interface Coding {
    system: string;
    code: string;
    display?: string;
}

interface Reference {
    reference: string;
    display?: string;
}

interface Period {
    start: string;
    end?: string;
}

interface Quantity {
    value: number;
    unit: string;
    system?: string;
    code?: string;
}

interface Range {
    low: Quantity;
    high: Quantity;
}

interface Annotation {
    text: string;
    time?: string;
}

interface ObservationReferenceRange {
    low?: Quantity;
    high?: Quantity;
    text?: string;
}

interface Attachment {
    contentType: string;
    data?: string;
    url?: string;
}

interface PatientContact {
    relationship?: CodeableConcept[];
    name?: HumanName;
    telecom?: ContactPoint[];
}

interface PatientCommunication {
    language: CodeableConcept;
    preferred?: boolean;
}

interface Bundle {
    resourceType: 'Bundle';
    type: 'searchset' | 'document' | 'message' | 'transaction' | 'batch';
    total?: number;
    entry?: BundleEntry[];
}

interface BundleEntry {
    fullUrl?: string;
    resource: FHIRResource;
}

interface Medication extends FHIRResource {
    resourceType: ResourceType.Medication;
    code: CodeableConcept;
}

interface Alert {
    severity: 'low' | 'medium' | 'high';
    type: string;
    message: string;
    [key: string]: any;
}

interface VideoConsultation {
    id: string;
    patientId: string;
    providerId: string;
    roomId: string;
    scheduledTime: Date;
    status: string;
    patientToken: string;
    providerToken: string;
}

// Export services
export {
    HIPAASecurityService,
    HIPAAAuditService,
    FHIRRepository,
    ClinicalDecisionSupport,
    HL7Processor,
    DICOMProcessor,
    TelemedicineService,
};
```


Security scan verdicts — ClawScan: clean | static analysis: clean | VirusTotal: clean | SkillSpector: clean (LOW)