import jsPDF from 'jspdf'; import { Superbill } from '@/types/clinical'; export function generateSuperbillPdf(superbill: Superbill) { const doc = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'letter', }); const margin = 40; let y = margin; // Header Banner doc.setFillColor(15, 23, 42); // Slate 900 doc.rect(0, 0, doc.internal.pageSize.getWidth(), 80, 'F'); doc.setFont('helvetica', 'bold'); doc.setTextColor(255, 255, 255); doc.setFontSize(20); doc.text('MEDICAL SUPERBILL & STATEMENT', margin, 42); doc.setFont('helvetica', 'normal'); doc.setFontSize(10); doc.setTextColor(148, 163, 184); // Slate 400 doc.text('Comprehensive Out-of-Network Insurance Reimbursement Form', margin, 60); doc.setTextColor(255, 255, 255); doc.text(`Invoice #: ${superbill.invoiceNumber}`, doc.internal.pageSize.getWidth() - margin - 150, 42); doc.text(`Date of Service: ${superbill.dateOfService}`, doc.internal.pageSize.getWidth() - margin - 150, 60); y = 110; // Two columns: Clinic (Billing Provider) & Patient Details doc.setFont('helvetica', 'bold'); doc.setFontSize(11); doc.setTextColor(15, 23, 42); doc.text('BILLING PROVIDER & CLINIC', margin, y); doc.text('PATIENT INFORMATION', 320, y); doc.setDrawColor(226, 232, 240); doc.line(margin, y + 6, 280, y + 6); doc.line(320, y + 6, doc.internal.pageSize.getWidth() - margin, y + 6); y += 22; doc.setFont('helvetica', 'normal'); doc.setFontSize(9); doc.setTextColor(51, 65, 85); // Clinic Info doc.text(superbill.clinicName, margin, y); doc.text(superbill.clinicAddress, margin, y + 14); doc.text(`Billing Provider: ${superbill.providerName}`, margin, y + 28); doc.text(`Provider NPI: ${superbill.providerNpi} | Tax ID: ${superbill.clinicTaxId}`, margin, y + 42); doc.text(`Place of Service (POS): ${superbill.posCode}`, margin, y + 56); // Patient Info doc.text(`Name: ${superbill.patientName}`, 320, y); doc.text(`DOB: ${superbill.patientDob}`, 320, y + 14); doc.text(`Address: ${superbill.patientAddress}`, 320, y + 28); doc.text(`Payment Status: ${superbill.paymentMethod} (PAID IN FULL)`, 320, y + 42); y += 85; // Diagnosis (ICD-10) Section doc.setFont('helvetica', 'bold'); doc.setFontSize(11); doc.setTextColor(15, 23, 42); doc.text('DIAGNOSIS CODES (ICD-10-CM)', margin, y); doc.line(margin, y + 6, doc.internal.pageSize.getWidth() - margin, y + 6); y += 20; doc.setFont('helvetica', 'normal'); doc.setFontSize(9); doc.setTextColor(51, 65, 85); superbill.icd10Codes.forEach((icd, idx) => { doc.setFont('helvetica', 'bold'); doc.text(`(${idx + 1}) ${icd.code}`, margin, y); doc.setFont('helvetica', 'normal'); doc.text(` - ${icd.description}`, margin + 65, y); y += 15; }); y += 15; // Procedure Line Items Table doc.setFont('helvetica', 'bold'); doc.setFontSize(11); doc.setTextColor(15, 23, 42); doc.text('PROCEDURES & SERVICES (CPT CODES)', margin, y); doc.line(margin, y + 6, doc.internal.pageSize.getWidth() - margin, y + 6); y += 18; // Table Header doc.setFillColor(241, 245, 249); doc.rect(margin, y, doc.internal.pageSize.getWidth() - (margin * 2), 22, 'F'); doc.setFont('helvetica', 'bold'); doc.setFontSize(9); doc.setTextColor(71, 85, 105); doc.text('CPT CODE', margin + 8, y + 15); doc.text('DESCRIPTION', margin + 80, y + 15); doc.text('UNITS', 400, y + 15); doc.text('RATE', 450, y + 15); doc.text('TOTAL', 505, y + 15); y += 24; doc.setFont('helvetica', 'normal'); doc.setFontSize(9); doc.setTextColor(30, 41, 59); superbill.items.forEach((item) => { doc.text(item.cptCode, margin + 8, y + 14); doc.text(item.description, margin + 80, y + 14); doc.text(String(item.units), 405, y + 14); doc.text(`$${item.rate.toFixed(2)}`, 450, y + 14); doc.text(`$${item.total.toFixed(2)}`, 505, y + 14); doc.setDrawColor(241, 245, 249); doc.line(margin, y + 20, doc.internal.pageSize.getWidth() - margin, y + 20); y += 22; }); y += 20; // Summary Totals Box const summaryX = 350; doc.setFillColor(248, 250, 252); doc.rect(summaryX, y, doc.internal.pageSize.getWidth() - margin - summaryX, 70, 'F'); doc.rect(summaryX, y, doc.internal.pageSize.getWidth() - margin - summaryX, 70, 'S'); doc.setFont('helvetica', 'normal'); doc.text('Total Charges:', summaryX + 15, y + 20); doc.text(`$${superbill.totalAmount.toFixed(2)}`, 505, y + 20); doc.text('Patient Amount Paid:', summaryX + 15, y + 38); doc.text(`$${superbill.patientPaid.toFixed(2)}`, 505, y + 38); doc.setFont('helvetica', 'bold'); doc.text('Balance Due:', summaryX + 15, y + 56); doc.text(`$${superbill.balanceDue.toFixed(2)}`, 505, y + 56); y += 95; // Practitioner Attestation & Signature doc.setFont('helvetica', 'bold'); doc.setFontSize(9); doc.setTextColor(15, 23, 42); doc.text('PRACTITIONER ATTESTATION & SIGNATURE', margin, y); doc.line(margin, y + 4, doc.internal.pageSize.getWidth() - margin, y + 4); y += 16; doc.setFont('helvetica', 'normal'); doc.setFontSize(8); doc.setTextColor(100, 116, 139); doc.text( 'I certify that the services listed above were medically necessary and personally rendered by me or under my direct supervision on the date indicated.', margin, y ); y += 25; doc.setFont('helvetica', 'bold'); doc.setTextColor(15, 23, 42); doc.text(`Electronically Signed by: ${superbill.providerName}`, margin, y); doc.setFont('helvetica', 'normal'); doc.text(`Timestamp: ${superbill.generatedAt}`, margin, y + 12); doc.text(`Verification ID: MED-${superbill.id.toUpperCase()}`, margin, y + 24); // Footer Branding doc.setFontSize(8); doc.setTextColor(148, 163, 184); doc.text('Generated via Mediusa Clinic OS • Clean Claim Direct Superbill Standard', margin, 760); doc.save(`Superbill_${superbill.invoiceNumber}_${superbill.patientName.replace(/\s+/g, '_')}.pdf`); } export function generateCourtDefensePdf( soapNote: { id: string; date: string; discipline: string; subjective: string; objective: string; assessment: string; plan: string; vasScore: number; providerName: string; signedAt?: string; signedBy?: string; cryptographicHash?: string; licenseNumber?: string; spinalAdjustments?: { vertebra: string; region: string; listing: string; technique: string }[]; icd10Codes?: { code: string; description: string }[]; cptCodes?: { code: string; description: string; fee: number }[]; }, patient: { id: string; firstName: string; lastName: string; dob: string; gender: string; address: string; insuranceName: string; insuranceId: string; chiefComplaint: string; }, tenant: { name: string; address: string; phone: string; taxId: string; npi: string; } ) { const doc = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'letter', }); const margin = 40; let y = margin; const pageWidth = doc.internal.pageSize.getWidth(); // Legal Header Banner (Navy/Dark Blue) doc.setFillColor(15, 23, 42); // Slate 900 doc.rect(0, 0, pageWidth, 85, 'F'); doc.setFont('helvetica', 'bold'); doc.setTextColor(255, 255, 255); doc.setFontSize(16); doc.text('CERTIFIED MEDICAL RECORD & COURT EVIDENCE PACKET', margin, 38); doc.setFont('helvetica', 'normal'); doc.setFontSize(9); doc.setTextColor(148, 163, 184); doc.text( 'AUTHENTICATED PURSUANT TO FED. R. EVID. 902(11) & STATE EVIDENCE CODE (SELF-AUTHENTICATING)', margin, 55 ); doc.text( `AFFIDAVIT ID: CERT-LAW-${soapNote.id.toUpperCase()} • DATE ISSUED: ${new Date().toISOString().split('T')[0]}`, margin, 69 ); y = 110; // Custodian of Records Certification Box doc.setFillColor(248, 250, 252); doc.setDrawColor(203, 213, 225); doc.rect(margin, y, pageWidth - margin * 2, 72, 'FD'); doc.setFont('helvetica', 'bold'); doc.setFontSize(10); doc.setTextColor(15, 23, 42); doc.text('AFFIDAVIT & DECLARATION OF CUSTODIAN OF MEDICAL RECORDS (FED. R. EVID. 902(11))', margin + 12, y + 18); doc.setFont('helvetica', 'normal'); doc.setFontSize(7.5); doc.setTextColor(51, 65, 85); const affidavitText = `I, the authorized Custodian of Records for ${tenant.name}, hereby declare under penalty of perjury pursuant to 28 U.S.C. § 1746 and Fed. R. Evid. 902(11) that the attached medical records are true, authentic, and complete duplicates of electronic records of regularly conducted activity (Fed. R. Evid. 803(6)). These records were made at or near the time of the clinical encounter by Dr. ${soapNote.providerName}, kept in the regular course of business. System-level electronic authentication and data integrity are certified under Fed. R. Evid. 902(13) & 902(14) via the immutable SHA-256 cryptographic hash seal below, compliant with 21 CFR Part 11 and HIPAA 45 CFR § 164.312.`; const splitAffidavit = doc.splitTextToSize(affidavitText, pageWidth - margin * 2 - 24); doc.text(splitAffidavit, margin + 12, y + 30); y += 88; // Two Columns: Patient Identifiers & Provider Credentials doc.setFont('helvetica', 'bold'); doc.setFontSize(10); doc.setTextColor(15, 23, 42); doc.text('PATIENT IDENTIFICATION', margin, y); doc.text('ATTENDING PHYSICIAN / CLINIC', 320, y); doc.setDrawColor(226, 232, 240); doc.line(margin, y + 4, 280, y + 4); doc.line(320, y + 4, pageWidth - margin, y + 4); y += 18; doc.setFont('helvetica', 'normal'); doc.setFontSize(9); doc.setTextColor(51, 65, 85); doc.text(`Patient: ${patient.firstName} ${patient.lastName}`, margin, y); doc.text(`DOB: ${patient.dob} (${patient.gender})`, margin, y + 13); doc.text(`MRN: MRN-${patient.id.toUpperCase()}`, margin, y + 26); doc.text(`Insurance: ${patient.insuranceName} (${patient.insuranceId})`, margin, y + 39); doc.text(`Clinic: ${tenant.name}`, 320, y); doc.text(`Attending: Dr. ${soapNote.providerName}`, 320, y + 13); doc.text(`License / NPI: Lic #${soapNote.licenseNumber || 'DC-48192'} | NPI ${tenant.npi}`, 320, y + 26); doc.text(`Tax ID / EIN: ${tenant.taxId}`, 320, y + 39); y += 58; // Cryptographic Tamper-Proof Seal Box doc.setFillColor(240, 253, 250); // Teal 50 doc.setDrawColor(153, 246, 228); // Teal 200 doc.rect(margin, y, pageWidth - margin * 2, 34, 'FD'); doc.setFont('helvetica', 'bold'); doc.setFontSize(8.5); doc.setTextColor(13, 148, 136); // Teal 600 doc.text('IMMUTABLE CRYPTOGRAPHIC VERIFICATION SEAL (NON-REPUDIATION):', margin + 10, y + 14); doc.setFont('courier', 'bold'); doc.setFontSize(8); doc.setTextColor(15, 23, 42); const hash = soapNote.cryptographicHash || 'SHA-256: 7f8a92b3c4d5e6f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5'; doc.text(hash, margin + 10, y + 26); y += 48; // Clinical SOAP Narrative Header doc.setFont('helvetica', 'bold'); doc.setFontSize(10); doc.setTextColor(15, 23, 42); doc.text(`CLINICAL ENCOUNTER NOTES • DATE OF SERVICE: ${soapNote.date}`, margin, y); doc.setFontSize(7.5); doc.setTextColor(3, 105, 161); doc.text('AI PROVENANCE: Frontier Clinical Engine (GPT-5.4 / Claude 3.7 / Gemini 2.5 Pro)', 280, y); doc.setTextColor(15, 23, 42); doc.line(margin, y + 4, pageWidth - margin, y + 4); y += 18; // Subjective doc.setFont('helvetica', 'bold'); doc.setFontSize(9); doc.text(`SUBJECTIVE (VAS Pain: ${soapNote.vasScore}/10):`, margin, y); doc.setFont('helvetica', 'normal'); y += 12; const subjLines = doc.splitTextToSize(soapNote.subjective, pageWidth - margin * 2); doc.text(subjLines, margin, y); y += subjLines.length * 11 + 6; // Objective doc.setFont('helvetica', 'bold'); doc.text('OBJECTIVE EXAMINATION & PALPATION:', margin, y); doc.setFont('helvetica', 'normal'); y += 12; const objLines = doc.splitTextToSize(soapNote.objective, pageWidth - margin * 2); doc.text(objLines, margin, y); y += objLines.length * 11 + 6; // Spinal Adjustments Table if any if (soapNote.spinalAdjustments && soapNote.spinalAdjustments.length > 0) { doc.setFont('helvetica', 'bold'); doc.setFontSize(8.5); doc.setTextColor(2, 132, 199); doc.text('SEGMENTAL SUBLUXATIONS & ADJUSTMENT TECHNIQUES DELIVERED:', margin, y); y += 12; soapNote.spinalAdjustments.forEach((adj) => { doc.setFont('helvetica', 'bold'); doc.setTextColor(15, 23, 42); doc.text(`• Segment: ${adj.vertebra} (${adj.region})`, margin + 10, y); doc.setFont('helvetica', 'normal'); doc.text(`Listing: ${adj.listing} | Technique: ${adj.technique}`, margin + 160, y); y += 12; }); y += 4; } // Assessment doc.setFont('helvetica', 'bold'); doc.setFontSize(9); doc.setTextColor(15, 23, 42); doc.text('ASSESSMENT & MEDICAL NECESSITY DIAGNOSIS:', margin, y); doc.setFont('helvetica', 'normal'); y += 12; const assessLines = doc.splitTextToSize(soapNote.assessment, pageWidth - margin * 2); doc.text(assessLines, margin, y); y += assessLines.length * 11 + 6; // Plan doc.setFont('helvetica', 'bold'); doc.text('TREATMENT PLAN & CARE CADENCE:', margin, y); doc.setFont('helvetica', 'normal'); y += 12; const planLines = doc.splitTextToSize(soapNote.plan, pageWidth - margin * 2); doc.text(planLines, margin, y); y += planLines.length * 11 + 10; // Coding Grid (ICD-10 + CPT) doc.setFillColor(248, 250, 252); doc.setDrawColor(226, 232, 240); doc.rect(margin, y, pageWidth - margin * 2, 45, 'FD'); doc.setFont('helvetica', 'bold'); doc.setFontSize(8.5); doc.setTextColor(15, 23, 42); doc.text('BILLING CODING AUDIT RECORD:', margin + 8, y + 14); doc.setFont('helvetica', 'normal'); doc.setFontSize(8); doc.setTextColor(51, 65, 85); const icdStr = (soapNote.icd10Codes || []).map((c) => `${c.code} (${c.description})`).join(', '); const cptStr = (soapNote.cptCodes || []).map((c) => `${c.code} - ${c.description} ($${c.fee})`).join(', '); doc.text(`ICD-10 Diagnosis: ${icdStr || 'M99.01, M99.03, M54.50'}`, margin + 8, y + 26); doc.text(`CPT Procedures: ${cptStr || '98940 Chiropractic Manipulative Treatment (1-2 regions)'}`, margin + 8, y + 38); y += 58; // Electronic Signature Attestation doc.setDrawColor(15, 23, 42); doc.line(margin, y, pageWidth - margin, y); y += 14; doc.setFont('helvetica', 'bold'); doc.setFontSize(9); doc.setTextColor(15, 23, 42); doc.text(`DIGITALLY SIGNED & SEALED: Dr. ${soapNote.providerName}`, margin, y); doc.setFont('helvetica', 'normal'); doc.setFontSize(8); doc.setTextColor(71, 85, 105); doc.text(`Date & Time Sealed: ${soapNote.signedAt || new Date().toISOString()}`, margin, y + 12); doc.text( 'Audit Trail ID: AT-' + Math.random().toString(36).substring(2, 10).toUpperCase() + ' • Verified Compliant with 21 CFR Part 11 & HIPAA Security Rule', margin, y + 24 ); doc.save(`Certified_Court_Evidence_Record_${patient.lastName}_${soapNote.date}.pdf`); } export interface HipaaBaaPdfData { tenant: { name: string; address: string; cityStateZip?: string; phone: string; email: string; taxId: string; npi: string; }; signerName: string; signerTitle: string; signedAt: string; agreementId: string; verificationHash: string; } export function generateHipaaBaaPdf(data: HipaaBaaPdfData) { const doc = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'letter', }); const margin = 40; const pageWidth = doc.internal.pageSize.getWidth(); // ==================== PAGE 1 ==================== let y = margin; // Header Banner doc.setFillColor(15, 23, 42); // Slate 900 doc.rect(0, 0, pageWidth, 85, 'F'); doc.setFont('helvetica', 'bold'); doc.setTextColor(255, 255, 255); doc.setFontSize(15); doc.text('HIPAA BUSINESS ASSOCIATE AGREEMENT (BAA)', margin, 38); doc.setFont('helvetica', 'normal'); doc.setFontSize(8.5); doc.setTextColor(148, 163, 184); doc.text( 'STATUTORY COMPLIANCE INSTRUMENT PURSUANT TO 45 CFR PARTS 160 & 164 (PRIVACY, SECURITY & OMNIBUS RULES)', margin, 54 ); doc.text( `AGREEMENT ID: ${data.agreementId} • EFFECTIVE DATE: ${data.signedAt.split('T')[0]}`, margin, 68 ); y = 105; // Parties Box doc.setFillColor(248, 250, 252); doc.setDrawColor(203, 213, 225); doc.rect(margin, y, pageWidth - margin * 2, 75, 'FD'); doc.setFont('helvetica', 'bold'); doc.setFontSize(9.5); doc.setTextColor(15, 23, 42); doc.text('COVERED ENTITY (THE CLIENT)', margin + 12, y + 16); doc.text('BUSINESS ASSOCIATE (THE PLATFORM)', 310, y + 16); doc.setDrawColor(226, 232, 240); doc.line(margin + 12, y + 20, 290, y + 20); doc.line(310, y + 20, pageWidth - margin - 12, y + 20); doc.setFont('helvetica', 'normal'); doc.setFontSize(8); doc.setTextColor(51, 65, 85); doc.text(`Entity: ${data.tenant.name}`, margin + 12, y + 33); doc.text(`Address: ${data.tenant.address}${data.tenant.cityStateZip ? `, ${data.tenant.cityStateZip}` : ''}`, margin + 12, y + 45); doc.text(`NPI: ${data.tenant.npi} | EIN / Tax ID: ${data.tenant.taxId}`, margin + 12, y + 57); doc.text(`Phone / Email: ${data.tenant.phone} | ${data.tenant.email}`, margin + 12, y + 69); doc.text('Entity: AI Pilots LLC / Mediusa Clinical Systems', 310, y + 33); doc.text('Infrastructure: High-Security Cloud Clinical SaaS', 310, y + 45); doc.text('Email: hello@aipilots.site', 310, y + 57); doc.text('Governing Law: California, United States', 310, y + 69); y += 90; // Recitals doc.setFont('helvetica', 'bold'); doc.setFontSize(9); doc.setTextColor(15, 23, 42); doc.text('RECITALS & STATUTORY AUTHORITY', margin, y); doc.line(margin, y + 4, pageWidth - margin, y + 4); y += 15; doc.setFont('helvetica', 'normal'); doc.setFontSize(7.5); doc.setTextColor(71, 85, 105); const recitals = 'This Business Associate Agreement ("BAA") supplements and forms an integral part of the service agreement between Covered Entity and Business Associate. Covered Entity is a covered healthcare provider under the Health Insurance Portability and Accountability Act of 1996 ("HIPAA"), Public Law 104-191, the Health Information Technology for Economic and Clinical Health Act ("HITECH"), and their implementing regulations at 45 CFR Parts 160 and 164. Business Associate provides software services, electronic health records (EHR/SOAP), appointment scheduling, telehealth infrastructure, and billing tools that may involve the creation, receipt, maintenance, or transmission of electronic Protected Health Information ("ePHI").'; const splitRecitals = doc.splitTextToSize(recitals, pageWidth - margin * 2); doc.text(splitRecitals, margin, y); y += splitRecitals.length * 9.5 + 8; // Section 1: Permitted Uses and Disclosures doc.setFont('helvetica', 'bold'); doc.setFontSize(9); doc.setTextColor(15, 23, 42); doc.text('SECTION 1 — PERMITTED USES AND DISCLOSURES OF ePHI', margin, y); doc.line(margin, y + 4, pageWidth - margin, y + 4); y += 15; doc.setFont('helvetica', 'normal'); doc.setFontSize(7.5); doc.setTextColor(71, 85, 105); const sec1Text = '1.1 Service Provision: Business Associate is authorized to use and disclose ePHI solely as necessary to perform practice management services for Covered Entity, including appointment orchestration, clinical charting, diagnostic ICD-10 and CPT coding, billing statement generation, and patient communication.\n1.2 Nondisclosure: Business Associate will not use or disclose ePHI other than as permitted or required by this Agreement or as required by federal or state law.\n1.3 Prohibition on Sale of PHI: Business Associate expressly agrees never to sell, commercialize, license, or disclose Covered Entity’s ePHI for marketing, advertising, or third-party training without explicit authorization.'; const splitSec1 = doc.splitTextToSize(sec1Text, pageWidth - margin * 2); doc.text(splitSec1, margin, y); y += splitSec1.length * 9.5 + 8; // Section 2: Technical, Physical & Administrative Safeguards doc.setFont('helvetica', 'bold'); doc.setFontSize(9); doc.setTextColor(15, 23, 42); doc.text('SECTION 2 — TECHNICAL, PHYSICAL & ADMINISTRATIVE SAFEGUARDS (45 CFR § 164.312)', margin, y); doc.line(margin, y + 4, pageWidth - margin, y + 4); y += 15; doc.setFont('helvetica', 'normal'); doc.setFontSize(7.5); doc.setTextColor(71, 85, 105); const sec2Text = '2.1 Encryption at Rest: All electronic Protected Health Information stored in databases, storage volumes, and backups is encrypted using NIST-validated AES-256 encryption keys managed under strict cryptographic isolation.\n2.2 Encryption in Transit: All transmissions of ePHI across public or internal networks utilize Transport Layer Security (TLS) version 1.3 with Perfect Forward Secrecy.\n2.3 Role-Based Access Control (RBAC): Platform enforces granular access controls ensuring that front-desk and non-clinical personnel are programmatically restricted from accessing clinical SOAP narratives, physical examination records, or diagnostic codes.\n2.4 21 CFR Part 11 Audit Controls: System generates immutable, timestamped electronic audit trails tracking creation, amendment, signature, and export of medical encounters with SHA-256 digital seals.'; const splitSec2 = doc.splitTextToSize(sec2Text, pageWidth - margin * 2); doc.text(splitSec2, margin, y); y += splitSec2.length * 9.5 + 8; // Footer Page 1 doc.setFontSize(7.5); doc.setTextColor(148, 163, 184); doc.text('Mediusa Clinic OS • HIPAA Business Associate Agreement (BAA) • Page 1 of 2', margin, 760); // ==================== PAGE 2 ==================== doc.addPage(); y = margin; // Page 2 Header Banner doc.setFillColor(15, 23, 42); doc.rect(0, 0, pageWidth, 45, 'F'); doc.setFont('helvetica', 'bold'); doc.setTextColor(255, 255, 255); doc.setFontSize(11); doc.text('HIPAA BUSINESS ASSOCIATE AGREEMENT (BAA) — CONTINUED', margin, 28); doc.setFont('helvetica', 'normal'); doc.setFontSize(8); doc.setTextColor(148, 163, 184); doc.text(`AGREEMENT ID: ${data.agreementId}`, pageWidth - margin - 150, 28); y = 65; // Section 3: Downstream Subcontractor Pass-Through & Cloud Infrastructure Attestation doc.setFillColor(240, 249, 255); // Sky 50 doc.setDrawColor(186, 230, 253); // Sky 200 doc.rect(margin, y, pageWidth - margin * 2, 85, 'FD'); doc.setFont('helvetica', 'bold'); doc.setFontSize(9); doc.setTextColor(3, 105, 161); doc.text('SECTION 3 — CLOUD INFRASTRUCTURE & SUBCONTRACTOR ATTESTATION', margin + 10, y + 15); doc.setFont('helvetica', 'normal'); doc.setFontSize(7.5); doc.setTextColor(12, 74, 110); const sec3Text = '3.1 Subcontractor BAA Compliance: Pursuant to 45 CFR § 164.504(e)(1)(ii), Business Associate warrants that any downstream subcontractor that creates, receives, maintains, or transmits ePHI on its behalf has entered into a binding written BAA.\n3.2 Certified Cloud Provider: Primary database and server compute workloads are provisioned on Amazon Web Services (AWS) under an active, executed AWS Artifact HIPAA Business Associate Addendum.\n3.3 Database Isolation: PostgreSQL cluster operates with automated AWS KMS AES-256 volume encryption, private subnet isolation, zero public endpoint exposure, and continuous automated backup snapshots.'; const splitSec3 = doc.splitTextToSize(sec3Text, pageWidth - margin * 2 - 20); doc.text(splitSec3, margin + 10, y + 28); y += 100; // Section 4: Breach Notification Mandate doc.setFont('helvetica', 'bold'); doc.setFontSize(9); doc.setTextColor(15, 23, 42); doc.text('SECTION 4 — BREACH NOTIFICATION MANDATE (45 CFR § 164.410)', margin, y); doc.line(margin, y + 4, pageWidth - margin, y + 4); y += 15; doc.setFont('helvetica', 'normal'); doc.setFontSize(7.5); doc.setTextColor(71, 85, 105); const sec4Text = '4.1 Notice of Breach: Business Associate shall report to Covered Entity any confirmed Breach of Unsecured PHI without unreasonable delay and in no case later than ten (10) business days after discovery.\n4.2 Content of Notice: Such notice shall include, to the extent known: identification of affected individuals, description of nature of breach, types of ePHI involved, investigation results, and mitigation steps taken.'; const splitSec4 = doc.splitTextToSize(sec4Text, pageWidth - margin * 2); doc.text(splitSec4, margin, y); y += splitSec4.length * 9.5 + 8; // Section 5: Term, Termination & Data Disposition doc.setFont('helvetica', 'bold'); doc.setFontSize(9); doc.setTextColor(15, 23, 42); doc.text('SECTION 5 — TERM, TERMINATION & RETURN/DESTRUCTION OF ePHI', margin, y); doc.line(margin, y + 4, pageWidth - margin, y + 4); y += 15; doc.setFont('helvetica', 'normal'); doc.setFontSize(7.5); doc.setTextColor(71, 85, 105); const sec5Text = '5.1 Term: This Agreement shall be effective upon digital execution and remain in force throughout the term of Covered Entity’s active subscription.\n5.2 Disposition on Termination: Upon termination, Business Associate shall, upon written request, return or securely destroy all ePHI in accordance with NIST SP 800-88 Revision 1 guidelines for media sanitization.'; const splitSec5 = doc.splitTextToSize(sec5Text, pageWidth - margin * 2); doc.text(splitSec5, margin, y); y += splitSec5.length * 9.5 + 16; // Section 6: Signatures & Non-Repudiation Seal doc.setFont('helvetica', 'bold'); doc.setFontSize(9.5); doc.setTextColor(15, 23, 42); doc.text('SECTION 6 — MUTUAL EXECUTION & DIGITAL NON-REPUDIATION SEALS', margin, y); doc.line(margin, y + 4, pageWidth - margin, y + 4); y += 16; // Dual Signature Columns Box const sigBoxHeight = 110; doc.setFillColor(248, 250, 252); doc.setDrawColor(203, 213, 225); doc.rect(margin, y, pageWidth - margin * 2, sigBoxHeight, 'FD'); const colWidth = (pageWidth - margin * 2 - 20) / 2; // Left Column: Business Associate doc.setFont('helvetica', 'bold'); doc.setFontSize(8.5); doc.setTextColor(15, 23, 42); doc.text('BUSINESS ASSOCIATE:', margin + 12, y + 16); doc.setFont('helvetica', 'normal'); doc.setFontSize(7.5); doc.setTextColor(71, 85, 105); doc.text('Entity: AI Pilots LLC / Mediusa Clinical Systems', margin + 12, y + 28); doc.text('Signer: Chief Information Security Officer', margin + 12, y + 40); doc.text('Authorized Address: hello@aipilots.site', margin + 12, y + 52); doc.setFont('helvetica', 'bold'); doc.setTextColor(5, 150, 105); // Emerald 600 doc.text('STATUS: DIGITALLY COUNTERSIGNED & ACTIVE', margin + 12, y + 68); doc.setFont('helvetica', 'normal'); doc.setFontSize(6.5); doc.setTextColor(100, 116, 139); doc.text('System Enclave Seal: AI-PILOTS-BAA-KEY-2026', margin + 12, y + 80); // Right Column: Covered Entity const rightColX = margin + colWidth + 20; doc.setFont('helvetica', 'bold'); doc.setFontSize(8.5); doc.setTextColor(15, 23, 42); doc.text('COVERED ENTITY (CLIENT):', rightColX, y + 16); doc.setFont('helvetica', 'normal'); doc.setFontSize(7.5); doc.setTextColor(71, 85, 105); doc.text(`Entity: ${data.tenant.name}`, rightColX, y + 28); doc.text(`Signer: ${data.signerName} (${data.signerTitle})`, rightColX, y + 40); doc.text(`NPI: ${data.tenant.npi} | EIN: ${data.tenant.taxId}`, rightColX, y + 52); doc.setFont('helvetica', 'bold'); doc.setTextColor(3, 105, 161); // Sky 700 doc.text(`EXECUTED: ${data.signedAt}`, rightColX, y + 68); doc.setFont('courier', 'bold'); doc.setFontSize(6.5); doc.setTextColor(15, 23, 42); doc.text(`SHA-256: ${data.verificationHash.replace('SHA-256: ', '').substring(0, 32)}...`, rightColX, y + 80); y += sigBoxHeight + 14; // Federal Legal Notice doc.setFont('helvetica', 'normal'); doc.setFontSize(7); doc.setTextColor(148, 163, 184); doc.text( 'This document constitutes a binding legal agreement enforceable under the Electronic Signatures in Global and National Commerce Act (ESIGN, 15 U.S.C. § 7001) and Uniform Electronic Transactions Act (UETA). Both parties retain cryptographic proof of execution.', margin, y ); // Footer Page 2 doc.text('Mediusa Clinic OS • HIPAA Business Associate Agreement (BAA) • Page 2 of 2', margin, 760); doc.save(`HIPAA_BAA_Agreement_${data.tenant.name.replace(/[^a-zA-Z0-9]/g, '_')}.pdf`); }