library QOLNeurologicConditions version '0.1.000'

using QICore version '6.0.0'

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

codesystem "ActCode": 'http://terminology.hl7.org/CodeSystem/v3-ActCode'

valueset "Global Health 10 (LOINC)": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1034.595'
valueset "Neurologic Disorders": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1034.640'
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 "Outpatient Consultation": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.101.11.1040'
valueset "PROMIS 10 Global Health Scores (LOINC)": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1034.391'
valueset "Telephone Visits All": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1260.161'

code "virtual": 'VR' from "ActCode" display 'virtual'

parameter "Measurement Period" Interval<DateTime>

context Patient

define "Initial Population":
  "Age 18 Or Older At Time Of First PROMIS"

define "Denominator":
  "Initial Population"

define "Denominator Exclusion":
  "Patient Expired"
    or "Second PROMIS Not Collected"

define "Numerator":
  "Last PROMIS Same Or Higher Score Compared To First PROMIS"

define "Age 18 Or Older At Time Of First PROMIS":
  AgeInYearsAt(date from start of "First Index PROMIS Assessment With Encounter During ID Period".effective) >= 18

define "Outpatient Encounters With Neurologic Disorders During ID Period":
  "Encounters With Neurologic Disorders During ID Period"

define "Encounters With Neurologic Disorders During ID Period":
  ( [Encounter: "Office Visit"]
    union [Encounter: "Outpatient Consultation"]
    union [Encounter: "Online Evaluation and Management Encounters"]
    union [Encounter: "Telephone Visits All"] ) ValidEncounter
    where ValidEncounter."encounterDiagnosis" ( ).code in "Neurologic Disorders"

define "First Index PROMIS Assessment With Encounter During ID Period":
  First("PROMIS 10 Global Health" PROMISAssessment
      with "Outpatient Encounters With Neurologic Disorders During ID Period" QualifyingEncounter
        such that PROMISAssessment.effective.toInterval() starts during day of "Denominator Identification Period"
      sort by start of effective.toInterval()
  )

define "PROMIS 10 Global Health":
  ( [ObservationScreeningAssessment: "Global Health 10 (LOINC)"] PROMISAssessment
      where PROMISAssessment.effective.toInterval ( ) is not null
        and PROMISAssessment.value is not null
        and PROMISAssessment.status ~ 'final'
  )

define "Patient Expired":
  Patient.deceased as DateTime on or before end of "Measure Assessment Period"

define "Second PROMIS Not Collected":
  not exists ( ( "PROMIS 10 Global Health" PROMISAssessment
        where start of PROMISAssessment.effective.toInterval ( ) during day of "Measure Assessment Period"
        sort by start of effective.toInterval ( )
    )
  )

define "Measure Assessment Period":
  "First Index PROMIS Assessment With Encounter During ID Period" FirstIndexAssessment
    let YearAfterIndexAssessment: date from start of FirstIndexAssessment.effective.toInterval ( ) + 12 months
    return Interval[YearAfterIndexAssessment - 60 days, YearAfterIndexAssessment + 60 days]

define "First Index PROMIS Assessment Score":
  First("PROMIS 10 Global Health" PROMISAssessment
      with "Outpatient Encounters With Neurologic Disorders During ID Period" QualifyingEncounter
        such that PROMISAssessment.effective.toInterval() starts during day of "Denominator Identification Period"
      sort by start of effective.toInterval()
  ).value as Integer

define "Last PROMIS Same Or Higher Score Compared To First PROMIS":
  ( "First Index PROMIS Assessment Score" IndexScore
      with "Last PROMIS Assessment" MeasureAssessmentScore
        such that MeasureAssessmentScore >= IndexScore
  ) is not null

define "Denominator Identification Period":
  Interval[start of "Measurement Period" - 14 months, start of "Measurement Period" - 2 months )

define "Last PROMIS Assessment":
  Last("PROMIS 10 Global Health" PROMISAssessment
      where start of PROMISAssessment.effective.toInterval() during day of "Measure Assessment Period"
      sort by start of effective.toInterval()
  ).value as Integer

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

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

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

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