/**
 * Medicare / Medicaid Billing Compliance Pack
 *
 * 31 U.S.C. §§3729-3733 — False Claims Act
 * 42 CFR Part 415 — Services Furnished by Physicians in Providers
 * 42 CFR Part 412 — Prospective Payment System (DRG)
 * 42 CFR Part 410 — Supplementary Medical Insurance Benefits
 * 42 CFR Part 414 — Payment for Part B Medical and Other Health Services
 * 42 CFR Part 418 — Hospice Care
 * 42 CFR Part 422 — Medicare Advantage
 * 42 CFR Part 424 — Conditions for Medicare Payment
 *
 * Enforces 6 of 8 rules from rules.md:
 *   BLOCK: MMB-001 (E&M code-level validation), MMB-003 (incident-to supervision),
 *          MMB-004 (medical necessity basis), MMB-006 (home health cert),
 *          MMB-007 (hospice terminal prognosis)
 *   WARN:  MMB-002 (DRG CC/MCC support), MMB-008 (HCC current-year docs)
 *   SKIP:  MMB-005 — requires-ncci-integration (NCCI edit table pending), not wired here
 *
 * @connexum/ai-governance | Kai (CXNI-005) | 2026-05-24
 */
export const MEDICARE_MEDICAID_BILLING_CATEGORY_CLASSIFIERS = {
    /**
     * MMB-001 — BLOCK
     * E&M code selections must match documented MDM complexity or total time.
     * 42 CFR §415.100; CMS E&M documentation guidelines (2021 AMA/CMS revisions)
     *
     * enforcement_pattern: semantic:em-code-documentation-match
     * Structural signal: code selected at 99205/99215 (high complexity) without
     * high MDM documentation or significant time documented.
     */
    EM_CODE_OVERDOCUMENTED: {
        references: ['42 CFR §415.100 — E&M service documentation requirements; CMS 2021 E&M guidelines'],
        classifier: (input) => {
            // Must be an E&M code selection output (natural language or JSON with cpt_code field)
            const isEmCode = /\b(E[&+]?M\s+(code|level|selection)|evaluation\s+and\s+management\s+(code|level|billing)|CPT\s+(99201|99202|99203|99204|99205|99211|99212|99213|99214|99215|99232|99233)|cpt[_\s]code["']?\s*[:=]\s*["']?(99201|99202|99203|99204|99205|99211|99212|99213|99214|99215)|code_rationale)/i.test(input);
            if (!isEmCode)
                return false;
            // Check if a high-complexity code is selected (99205 or 99215)
            const isHighComplexityCode = /\b(99205|99215)\b/.test(input);
            if (!isHighComplexityCode)
                return false;
            // If MDM complexity is explicitly documented as straightforward or low, that's an upcoding signal
            const hasLowMDM = /(mdm[_\s]complexity["']?\s*:\s*["']?(straightforward|low|moderate)["']?|mdm[_\s]basis[:\s]*(straightforward|low|minimal|simple))/i.test(input);
            if (hasLowMDM)
                return true;
            // Flag if there is no documentation of high MDM or sufficient time
            const hasHighMDMDocumented = /(MDM[_\s]complexity["']?\s*:\s*["']?high|MDM[:\s]+(high|level\s+4|level\s+III)|medical\s+decision\s+making[:\s]+(high|complex)|high\s+risk\s+decision)/i.test(input);
            const hasSufficientTime = /(total\s+time[:\s]+[4-9]\d\s*min|total\s+time[:\s]+[1-9]\d{2}\s*min|time\s+based[:\s]+[4-9]\d\s*min|\d{2,3}\s*minutes?\s+(of\s+)?(total\s+)?time)/i.test(input);
            return !hasHighMDMDocumented && !hasSufficientTime;
        },
    },
    /**
     * MMB-002 — WARN
     * DRG assignment with CC/MCC secondary diagnoses must have clinical documentation anchors.
     * 42 CFR §412; OIG guidance on DRG upcoding
     *
     * enforcement_pattern: semantic:drg-cc-mcc-support-check
     */
    DRG_CC_MCC_UNSUPPORTED: {
        references: ['42 CFR §412 — Prospective Payment System DRG requirements; OIG DRG upcoding guidance'],
        classifier: (input) => {
            // Must be a DRG assignment output
            const isDRG = /\b(DRG\s+(assignment|code|selection|result)|diagnosis.?related\s+group|MS.?DRG)\b/i.test(input);
            if (!isDRG)
                return false;
            // Check for CC or MCC present in the output
            const hasCCMCC = /\b(CC|MCC|complication|comorbidity|major\s+comorbidity)\b/i.test(input);
            if (!hasCCMCC)
                return false;
            // Flag if no clinical documentation anchor is referenced
            const hasClinicalAnchor = /(physician\s+(attestation|documentation|note)|clinical\s+(documentation|evidence|note)|attending\s+(physician|note|documentation)|physician\s+query|query\s+response|clarification\s+from\s+(physician|provider))/i.test(input);
            return !hasClinicalAnchor;
        },
    },
    /**
     * MMB-003 — BLOCK
     * Incident-to billing requires supervising physician physically present in office suite,
     * service within established plan of care, and service not a new problem.
     * 42 CFR §410.26; CMS Benefit Policy Manual Chapter 15
     *
     * enforcement_pattern: semantic:incident-to-supervision-check
     */
    INCIDENT_TO_SUPERVISION_MISSING: {
        references: ['42 CFR §410.26 — Incident-to services requirements; CMS Benefit Policy Manual Ch. 15'],
        classifier: (input) => {
            // Must be an incident-to billing output
            const isIncidentTo = /\b(incident.to\s+(billing|service|claim)|billing\s+incident.to|incident\s+to\s+(supervision|services?|billing)|incident.to\s+bill(ed|ing))\b/i.test(input);
            if (!isIncidentTo)
                return false;
            // Check for supervising physician present confirmation
            const hasSupervisorPresent = /(supervising[_\s]physician[_\s]present\s*[:=]\s*(true|yes)|physician\s+(physically\s+)?present\s+in\s+(the\s+)?office\s+suite|direct\s+supervision\s+(confirmed?|documented?))/i.test(input);
            // Check for "not new problem" confirmation
            const hasNotNewProblem = /(new[_\s]problem\s*[:=]\s*(false|no|0)|existing\s+(plan\s+of\s+care|condition|problem)|established\s+(plan|condition|care))/i.test(input);
            return !hasSupervisorPresent || !hasNotNewProblem;
        },
    },
    /**
     * MMB-004 — BLOCK
     * Claims for medically unnecessary services must not be submitted.
     * Diagnosis codes must be supported by clinical documentation.
     * 42 U.S.C. §1320a-7a(a)(1)(A); False Claims Act
     *
     * enforcement_pattern: semantic:medical-necessity-basis-check
     */
    MEDICAL_NECESSITY_UNSUPPORTED: {
        references: ['42 U.S.C. §1320a-7a(a)(1)(A) — False Claims Act; medical necessity documentation'],
        classifier: (input) => {
            // Must be a claim coding output
            const isClaim = /\b(claim\s+(code|submission|line|coding)|diagnosis\s+(code|coding)|ICD.?10\s+(code|diagnosis)|coding\s+output|billed\s+diagnosis)\b/i.test(input);
            if (!isClaim)
                return false;
            // Look for diagnosis code without clinical basis documentation
            const hasDiagnosisCode = /\b[A-Z]\d{2}\.?\d{0,2}[A-Z]?\b/.test(input); // ICD-10 code pattern
            if (!hasDiagnosisCode)
                return false;
            // Flag if there is explicit indication that documentation is absent
            const hasUnsupportedIndicator = /(no\s+(clinical\s+)?(documentation|basis|support|evidence)|not\s+(documented|supported|evidenced)\s+in\s+(the\s+)?record|diagnosis\s+(not|without)\s+(clinical\s+)?(support|documentation|basis)|undocumented\s+diagnosis)/i.test(input);
            return hasUnsupportedIndicator;
        },
    },
    /**
     * MMB-006 — BLOCK
     * Home health certification requires face-to-face encounter and supporting documentation.
     * Required: face-to-face date, certifying provider NPI, homebound documentation, skilled care need.
     * 42 CFR §424.22
     *
     * enforcement_pattern: semantic:home-health-cert-elements
     */
    HOME_HEALTH_CERT_INCOMPLETE: {
        references: ['42 CFR §424.22 — Home health certification face-to-face requirements'],
        classifier: (input) => {
            // Must be a home health certification output (JSON cert_type or natural language)
            const isHomeHealthCert = /\b(home\s+health\s+(certification|cert|order|episode)|HH\s+(certification|cert|order)|start\s+of\s+care\s+(certification|cert)|OASIS\s+certification)\b|home[_\s]health[_\s]certif|cert[_\s]type["':\s]+(["'])?home[_\s]health/i.test(input);
            if (!isHomeHealthCert)
                return false;
            // Face-to-face: explicit null/absent is a BLOCK trigger; explicit date/provider is OK
            const hasFaceToFaceDateAbsent = /(face[_\s]to[_\s]face[_\s]date["']?\s*:\s*null|face.to.face[_\s](date|encounter)\s*(is\s+)?(not\s+(documented|present|available)|absent|missing))/i.test(input);
            if (hasFaceToFaceDateAbsent)
                return true;
            const hasFaceToFaceDate = /(face.to.face[_\s]date["']?\s*[:=]\s*["']?\d{4}|face.to.face\s+(encounter|visit)\s+(date|on)\s+\d|f2f[_\s](date|encounter)|face.to.face.window.check|face.to.face.clinical.findings)/i.test(input);
            const hasCertifyingProvider = /(certifying[_\s](physician|provider|NPI)|certifying\s+(physician|provider)\s+NPI|certifying[_\s]npi)/i.test(input);
            const hasHomeboundDoc = /(homebound[_\s](status|documentation|documented?|confirmed?|basis)|patient\s+(is\s+)?homebound|homebound[_\s](criteria|condition))/i.test(input);
            const hasSkilledCare = /(skilled[_\s](care|nursing|therapy)[_\s](need|requirement|documented?|basis)|clinical\s+findings\s+support\s+(skilled|homebound|care\s+need))/i.test(input);
            return !hasFaceToFaceDate || !hasCertifyingProvider || !hasHomeboundDoc || !hasSkilledCare;
        },
    },
    /**
     * MMB-007 — BLOCK
     * Hospice election requires physician/NP certification of terminal prognosis (6 months or less).
     * 42 CFR §418.22
     *
     * enforcement_pattern: semantic:hospice-terminal-prognosis
     */
    HOSPICE_TERMINAL_PROGNOSIS_MISSING: {
        references: ['42 CFR §418.22 — Hospice certification of terminal illness prognosis'],
        classifier: (input) => {
            // Must be a hospice election or billing output
            const isHospice = /\b(hospice\s+(election|certification|billing|claim|benefit|care)|hospice\s+(elected?|selected?|initiated?|enrolled?))\b/i.test(input);
            if (!isHospice)
                return false;
            // Check for terminal prognosis certification
            const hasTerminalPrognosis = /(terminal[_\s]prognosis[_\s](6|six)[_\s]month[_\s]certified\s*[:=]\s*(true|yes)|6\s*month(s)?\s+(or\s+less\s+)?prognosis\s+(certified?|documented?|signed?)|life\s+expectancy\s+of\s+(6|six)\s+month(s)?\s+or\s+less)/i.test(input);
            return !hasTerminalPrognosis;
        },
    },
    /**
     * MMB-008 — WARN
     * HCC risk adjustment codes submitted to Medicare Advantage must be supported
     * by current-year clinical documentation.
     * 42 CFR §422.310; CMS RADV audit requirements
     *
     * enforcement_pattern: semantic:hcc-current-year-documentation
     */
    HCC_CURRENT_YEAR_DOCS_MISSING: {
        references: ['42 CFR §422.310 — Medicare Advantage risk adjustment; CMS RADV audit requirements'],
        classifier: (input) => {
            // Must be an HCC/risk adjustment output (function names, field names, or natural language)
            const isHCC = /\b(HCC\s+(code|submission|risk\s+adjustment)|hierarchical\s+condition\s+categor(y|ies)|risk\s+adjustment\s+(code|submission|factor|HCC))\b|(map_to_hcc|submit_hcc_codes|hcc[_\s]code|risk[_\s]adjustment[_\s]submission|generate[_\s]risk[_\s]adjustment)/i.test(input);
            if (!isHCC)
                return false;
            // Check if there is any current-year filtering or verification
            const hasCurrentYearFilter = /(current[_\s]year[_\s](verified|documentation|diagnosis|visit|encounter)|current\s+calendar\s+year|encounter[_\s]year\s*!=?\s*current[_\s]year|year\s*=\s*current[_\s]year|year\s*==?\s*plan[_\s]year|dx\.encounter[_\s]year|current[_\s]year\s*=|year=current)/i.test(input);
            if (hasCurrentYearFilter)
                return false;
            // Flag if HCC submission has no current-year filtering anchor at all
            const hasAnyYearCheck = /(encounter[_\s]year|current[_\s]year|plan[_\s]year\s+check|year\s+verified|year\s+filter)/i.test(input);
            // Without a current-year anchor, this is suspect HCC overcoding
            return !hasAnyYearCheck;
        },
    },
};
export const MEDICARE_MEDICAID_BILLING_PACK = {
    categoryClassifiers: MEDICARE_MEDICAID_BILLING_CATEGORY_CLASSIFIERS,
    id: 'medicare-medicaid-billing',
    name: 'Medicare / Medicaid Billing Compliance',
    version: '1.0.0',
    contentHash: 'e8512f5e35f413a7989c11cab1e23b7dff435c2710a5feeb1e7253ffaca9d2b6',
    description: 'False Claims Act and Medicare/Medicaid billing compliance for AI coding, CDI, and revenue cycle systems. Covers E&M upcoding prevention, DRG CC/MCC documentation requirements, incident-to billing verification, home health face-to-face certification, hospice terminal prognosis, and HCC risk adjustment current-year documentation.',
    authority: 'CMS / OIG — Medicare and Medicaid Programs',
    industries: ['healthcare', 'hospital', 'physician-practice', 'revenue-cycle', 'health-it', 'coding'],
    requiredModules: [
        {
            name: 'audit-integrity',
            required: true,
            reason: 'Billing audit trail for OIG and MAC audits',
            reference: '31 U.S.C. §§3729-3733 — False Claims Act',
        },
        {
            name: 'governance-runtime',
            required: true,
            reason: 'Block billing outputs without required documentation',
            reference: '42 CFR §415.100, §410.26, §418.22, §424.22',
        },
        {
            name: 'data-classifier',
            required: true,
            reason: 'E&M code-level validation and medical necessity detection',
            reference: 'CMS E&M 2021 documentation guidelines',
        },
    ],
    moduleConfig: {
        'audit-integrity': {
            retentionDays: 2555, // 7 years per Medicare record retention requirements
        },
        'governance-runtime': {
            denyByDefaultRequired: true,
        },
    },
    providerPolicy: {
        blockedProviders: [],
        requiredCapabilities: [],
        blockReasons: {},
    },
    dataPolicy: {
        scanCategories: [
            'EM_CODE_OVERDOCUMENTED',
            'DRG_CC_MCC_UNSUPPORTED',
            'INCIDENT_TO_SUPERVISION_MISSING',
            'MEDICAL_NECESSITY_UNSUPPORTED',
            'HOME_HEALTH_CERT_INCOMPLETE',
            'HOSPICE_TERMINAL_PROGNOSIS_MISSING',
            'HCC_CURRENT_YEAR_DOCS_MISSING',
        ],
        categoryActions: {
            EM_CODE_OVERDOCUMENTED: 'BLOCK',
            DRG_CC_MCC_UNSUPPORTED: 'WARN',
            INCIDENT_TO_SUPERVISION_MISSING: 'BLOCK',
            MEDICAL_NECESSITY_UNSUPPORTED: 'BLOCK',
            HOME_HEALTH_CERT_INCOMPLETE: 'BLOCK',
            HOSPICE_TERMINAL_PROGNOSIS_MISSING: 'BLOCK',
            HCC_CURRENT_YEAR_DOCS_MISSING: 'WARN',
        },
        encryptionRequired: true,
        encryptionStandard: 'AES-256-GCM',
        minimumClassification: 'RESTRICTED',
    },
    retentionPolicy: {
        auditLogRetentionDays: 2555,
        sessionRetentionDays: 2555,
        incidentRetentionDays: 2555,
        reference: 'Medicare billing record retention — 7 years per CMS guidance',
        forceDelete: false,
        requiresLegalHoldSupport: true,
        sovereignty: ['us'],
    },
    incidentPolicy: {
        notificationDeadlineHours: 24,
        reference: '31 U.S.C. §3730 — False Claims Act qui tam and voluntary disclosure',
        requiredRecipients: ['compliance-officer', 'billing-director', 'legal-counsel'],
        formalNotificationRequired: true,
    },
    validators: [
        {
            id: 'mmb-em-code-check',
            description: 'E&M code-level validation classifier active',
            reference: '42 CFR §415.100; CMS E&M 2021 guidelines',
            check: 'data_classifier_active',
            failSeverity: 'CRITICAL',
        },
        {
            id: 'mmb-medical-necessity',
            description: 'Medical necessity basis check classifier active',
            reference: '42 U.S.C. §1320a-7a; False Claims Act',
            check: 'data_classifier_active',
            failSeverity: 'CRITICAL',
        },
    ],
    requiresRetrievalAuditLogging: true,
    maxAutonomyLevelWithoutAttestation: 2,
    recommendedHookProfile: 'strict',
};
//# sourceMappingURL=medicare-medicaid-billing.js.map