#!/usr/bin/env bash

# Note: 
# To add or update benchmarks for 2018-2019, rename your csv to 'benchmarks.csv'
# and place that file in staging/$YEAR/benchmarks/. 
# Replace any existing files of the same name.
# Run `npm run build:benchmarks $YEAR` to update benchmark JSON files under benchmarks/.
# $YEAR refers to the performance year you are looking to update. 

# JSON files under staging/$YEAR/benchmarks/json/ are combined to generate updated 
# benchmark JSON under benchmarks/.
# Benchmarks are unique on measure ID, benchmark year, performance year, and submission method;
# duplicate benchmarks w/in the same JSON file or between JSON files will throw an error and
# terminate build:benchmarks.
# The feature where a duplicate benchmark in a later JSON file under the staging directory will 
# overwrite the earlier benchmark in the earlier JSON file is no longer supported.
# `git checkout 811d9e2` for how merging used to work.

# For 2018-2019, the first historical benchmarks are generated by calculate-deciles-lib.js in
# the qpp-submissions-api repo, then the result is exported to the /benchmarks
# directory in this repo. Subsequent updated benchmarks for 2019 will
# be generated by this build:benchmarks script.

# Updates to 2017 benchmarks are not supported. 
# If you wish to update benchmarks for 2017, check out the commit before this change: `git checkout 811d9e2`.
# The old flow to generate benchmarks:
# input.csv => node scripts/parse-benchmarks-data.js => output.json
# staging/${YEAR}/benchmarks/json/output.json => npm run build:benchmarks => benchmarks/${YEAR}.json

performanceYear=$1

if [[ -z "$performanceYear" ]]; then
  echo 'Performance year unspecified.'
  exit 126
else
  stagingDirectory=staging/${performanceYear}/benchmarks
  benchmarkYear=$(($performanceYear-2))
  sourceFilename=${stagingDirectory}/source.csv
  csvFilename=${stagingDirectory}/benchmarks.csv
  cahpsCSVFilename=${stagingDirectory}/benchmarks_cahps.csv
  mccCSVFilename=${stagingDirectory}/benchmarks_mcc.csv
  benchmarkBaseFilename=${stagingDirectory}/json/benchmarks.json
  benchmarkCAHPSFilename=${stagingDirectory}/json/benchmarks_cahps.json
  mccBenchmarkFilename=${stagingDirectory}/json/benchmarks_mcc.json
  benchmarkFilename=benchmarks/${performanceYear}.json

  if [[ -f "$sourceFilename" ]]; then
    echo "Pre-processing source file."
    node scripts/benchmarks/archive/helpers/pre-process-source-csv.js $performanceYear
  fi

  # generate benchmarks base
  echo 'Generating new benchmarks JSON.'
  cat $csvFilename | node scripts/benchmarks/archive/parse-benchmarks-data.js $benchmarkYear $performanceYear > $benchmarkBaseFilename 

  if [[ -f "$cahpsCSVFilename" ]]; then
    echo 'Generating CAHPS benchmarks JSON.'
    cat $cahpsCSVFilename | node scripts/benchmarks/archive/parse-benchmarks-data.js $benchmarkYear $performanceYear > $benchmarkCAHPSFilename 
  fi

  if [[ -f "$mccCSVFilename" ]]; then
    echo 'Generating MCC benchmarks JSON.'
    # This is a new measure starting in 2021, this is why the benchmark and performance years are hard-coded to 2021
    # TODO update these values for future years
    cat $mccCSVFilename | node scripts/benchmarks/archive/parse-benchmarks-data.js 2021 2021 MCC > $mccBenchmarkFilename 
  fi

  echo 'Squashing staging JSON.'
  node scripts/benchmarks/archive/merge-benchmark-data.js $performanceYear > $benchmarkFilename
 
  # validate benchmarks
  echo 'Validating new benchmarks JSON.'
  node dist/scripts/validate-data.js benchmarks $performanceYear $benchmarkFilename
fi
