library TreatmentForAcuteMigraineAttacks version '0.1.000'

using QICore version '6.0.0'

include CQMCommon version '4.1.000' called CQMCommon
include FHIRHelpers version '4.4.000' called FHIRHelpers
include QICoreCommon version '4.0.000' called QICoreCommon
include SupplementalDataElements version '5.1.000' called SDE

codesystem "SNOMEDCT": 'http://snomed.info/sct'

valueset "Acute migraine medication overuse": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1034.579'
valueset "Emergency Department Visit": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.117.1.7.1.292'
valueset "Medical Reason": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.526.2.313'
valueset "Medical Reason For Not Providing Treatment": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.117.1.7.1.473'
valueset "Migraine Diagnosis (ICD)": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1034.663'
valueset "Neuromodulation for migraine": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1034.641'
valueset "Office Visit": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.101.11.1005'
valueset "Online Evaluation and Management Encounters": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1034.539'
valueset "Patient Refusal": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.117.1.7.1.93'
valueset "Patient Refusal of Medication (SNOMED)": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1034.221'
valueset "Prescribable NSAIDs and Acute Migraine Attack Medication": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1034.664'
valueset "Telephone Visits All": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1260.161'

code "Acute migraine (disorder)": '471661000124108' from "SNOMEDCT" display 'Acute migraine (disorder)'
code "Migraine aura without headache (disorder)": '230465000' from "SNOMEDCT" display 'Migraine aura without headache (disorder)'
code "Pain (finding)": '22253000' from "SNOMEDCT" display 'Pain (finding)'

parameter "Measurement Period" Interval<DateTime>

context Patient

define "Initial Population":
  exists ( "Acute Migraine Encounter During Measurement Period For Ages 6 And Older" )

define "Denominator":
  "Initial Population"

define "Numerator":
  exists ( "FDA Approved Medication Or Neuromodulation During Measurement Period" )

define "Denominator Exceptions":
  "Has Migraine Medication Not Ordered Due To Patient Or Medical Reasons"
    or "Has Allergy Or Adverse Event To Migraine Treatment"
    or "Has Migraine Treatment Overuse"
    or "Has Acute Migraine Without Pain"
    or "Has No Neuromodulation Device Due To Patient Or Medical Reason"

define "Acute Migraine Encounter During Measurement Period For Ages 6 And Older":
  ( [Encounter: "Office Visit"]
    union [Encounter: "Online Evaluation and Management Encounters"]
    union [Encounter: "Telephone Visits All"] ) MigraineEncounter
    where ( MigraineEncounter.reasonCode in "Migraine Diagnosis (ICD)"
        or MigraineEncounter.encounterDiagnosis ( ).code in "Migraine Diagnosis (ICD)"
    )
      and AgeInYearsAt(date from start of MigraineEncounter.period) >= 6
      and MigraineEncounter.period during "Measurement Period"
      and not exists ( [Encounter: "Emergency Department Visit"] EDVisit
          where EDVisit.period during day of MigraineEncounter.period
      )

//Numerator


define "FDA Approved Medication Or Neuromodulation During Measurement Period":
  ( "FDA Approved Medication"
      union "Neuromodulation Device"
  )

define "FDA Approved Medication":
  [MedicationRequest: "Prescribable NSAIDs and Acute Migraine Attack Medication"] FDAApprovedTX
    where FDAApprovedTX.authoredOn during "Measurement Period"

define "Neuromodulation Device":
  ( [DeviceRequest: "Neuromodulation for migraine"] NeuroDeviceOrder
      where NeuroDeviceOrder.authoredOn during day of "Measurement Period"
        and NeuroDeviceOrder.intent ~ 'order'
        and NeuroDeviceOrder.status in { 'active', 'completed' }
        and not exists ( ["TaskRejected"] TaskReject
            where TaskReject.focus.references ( NeuroDeviceOrder )
              and TaskReject.code ~ QICoreCommon."Fulfill"
        )
  )
    union ( [Procedure: "Neuromodulation for migraine"] ProcedurePerformed
        where ProcedurePerformed.performed.toInterval ( ) during day of "Measurement Period"
          and ProcedurePerformed.status ~ 'completed'
    )
    union ( [DeviceUseStatement] DeviceInUse
      where DeviceInUse.recordedOn during day of "Measurement Period"
        and DeviceInUse.status ~ 'active' ) DeviceUse
      where exists ( [Device: "Neuromodulation for migraine"] )

//Exceptions


define "Has Migraine Medication Not Ordered Due To Patient Or Medical Reasons":
  exists ( [MedicationNotRequested: "Prescribable NSAIDs and Acute Migraine Attack Medication"] FDAApprovedTX
      where FDAApprovedTX.reasonCode in "Medical Reason"
        or FDAApprovedTX.reasonCode in "Patient Refusal of Medication (SNOMED)"
          and FDAApprovedTX.status = 'completed'
          and FDAApprovedTX.doNotPerform is true
  )

define "Has Allergy Or Adverse Event To Migraine Treatment":
  exists ( [AllergyIntolerance: "Prescribable NSAIDs and Acute Migraine Attack Medication"] AllergyToTx
      with "Acute Migraine Encounter During Measurement Period For Ages 6 And Older" ValidEncounter
        such that AllergyToTx.clinicalStatus is null
          or ( AllergyToTx.clinicalStatus ~ QICoreCommon."allergy-active"
              and AllergyToTx.onset before end of "Measurement Period"
          )
  )
    or exists ( [AdverseEvent: "Prescribable NSAIDs and Acute Migraine Attack Medication"] AdverseEventToTX
        where AdverseEventToTX.recordedDate before end of "Measurement Period"
          and AdverseEventToTX.actuality ~ 'actual'
    )

define "Has Migraine Treatment Overuse":
  exists ( [ConditionProblemsHealthConcerns: "Acute migraine medication overuse"] Overuse
      where Overuse.prevalenceInterval ( ) starts during "Measurement Period"
  )

define "Has Acute Migraine Without Pain":
  exists ( ( [ConditionProblemsHealthConcerns: "Acute migraine (disorder)"] AcuteMigraine
        where not exists [ConditionProblemsHealthConcerns: "Pain (finding)"] Pain
          and AcuteMigraine.prevalenceInterval ( ) starts during "Measurement Period"
    )
      union ( [ConditionProblemsHealthConcerns: "Migraine aura without headache (disorder)"] NoHA
          where NoHA.prevalenceInterval ( ) starts during "Measurement Period"
      )
  )

define "Has No Neuromodulation Device Due To Patient Or Medical Reason":
  exists ( [DeviceNotRequested: "Neuromodulation for migraine"] DeviceNotRequested
      where DeviceNotRequested.authoredOn during day of "Measurement Period"
        and DeviceNotRequested.doNotPerformReason in "Medical Reason For Not Providing Treatment"
        and DeviceNotRequested.status ~ 'completed'
  )
    or exists ( [ProcedureNotDone: "Neuromodulation for migraine"] NoProcedure
        where NoProcedure.recorded during day of "Measurement Period"
          or NoProcedure.statusReason in "Medical Reason For Not Providing Treatment"
            and NoProcedure.status ~ 'not-done'
    )
    or exists ( from
        ( [DeviceRequest: "Neuromodulation for migraine"] ) DeviceOrderReject,
        [TaskRejected] T
        where DeviceOrderReject.intent in { 'order', 'original-order', 'reflex-order', 'filler-order', 'instance-order' }
          and DeviceOrderReject.status = 'active'
          and T.focus.references ( DeviceOrderReject )
          and T.code ~ QICoreCommon."Fulfill"
          and DeviceOrderReject.authoredOn during day of "Measurement Period"
          and ( T.statusReason in "Medical Reason For Not Providing Treatment"
              or T.statusReason in "Patient Refusal"
          )
        return {
          id: DeviceOrderReject.id,
          requestStatusReason: T.statusReason,
          authoredOn: DeviceOrderReject.authoredOn
        }
    )

define "SDE Ethnicity":
  SDE."SDE Ethnicity"

define "SDE Payer":
  SDE."SDE Payer"

define "SDE Race":
  SDE."SDE Race"

define "SDE Sex":
  SDE."SDE Sex"