#!/usr/bin/env bash

set -euo pipefail

BASE_URL="${BASE_URL:-http://localhost:8500}"
ENDPOINT="${BASE_URL%/}/api/exam-sprint/achievement-reports"
RESPONSE_BODY="$(mktemp)"

cleanup() {
  rm -f "$RESPONSE_BODY"
}

trap cleanup EXIT

http_code="$({
  curl -sS \
    -o "$RESPONSE_BODY" \
    -w '%{http_code}' \
    -X POST \
    -H 'Content-Type: application/json' \
    --data-binary @- \
    "$ENDPOINT" <<'JSON'
{
  "reportTitle": "高考英语临考突击学习成果报告",
  "reportSubtitle": "2024真题 · 两周专项训练 · 真实提分效果",
  "completionTitle": "恭喜完成两周考前突击专项训练",
  "completionSubtitle": "基于2024英语真题试卷 · 真实学习效果分析",
  "summaryMetrics": {
    "vocabularyGrowthText": "+19",
    "paperKnownWordsGrowthText": "+4",
    "unknownWordHitRateText": "0.0193",
    "learningEfficiencyText": "0.48"
  },
  "vocabularyComparison": {
    "beforeValue": 2328,
    "afterValue": 2347,
    "beforeText": "2328",
    "afterText": "2347",
    "growthText": "+19"
  },
  "paperKnownWordsComparison": {
    "beforeValue": 650,
    "afterValue": 654,
    "beforeText": "650",
    "afterText": "654",
    "growthText": "+4"
  },
  "examUnknownWordsHitStatus": {
    "unknownWordHitRateText": "0.0193",
    "learningEfficiencyText": "0.48",
    "unknownWordsBeforeText": "207",
    "unknownWordsAfterText": "203",
    "reducedUnknownWordsText": "4",
    "hitWords": ["number", "bear", "popular", "importance"]
  }
}
JSON
})"

printf 'HTTP %s\n' "$http_code"
cat "$RESPONSE_BODY"

if [ "$http_code" != "202" ]; then
  exit 1
fi
