#!/usr/bin/env bash
set -euo pipefail

: "${TARGET_TOKEN:?TARGET_TOKEN is required}"
base="${TARGET_BASE_URL:-https://gstyj.kxunpt.cn}"
headers=(-H "token: $TARGET_TOKEN" -H "Cookie: _token=$TARGET_TOKEN" -H "Referer: $base/dist/")

post_form() {
  local path=$1
  shift
  curl -fsS -X POST "$base$path" "${headers[@]}" "$@"
}

tree=$(post_form /p/staff/tree -d '')
team_uuid=$(jq -r '.. | objects | select(.name? == "演示运动队") | .uuid' <<<"$tree" | head -1)
[[ -n "$team_uuid" && "$team_uuid" != null ]]

staffs=$(curl -fsS -X POST "$base/p/staff/lists" "${headers[@]}" \
  -H 'Content-Type: application/json' \
  --data "$(jq -nc --arg team "$team_uuid" '{department_uuid_str:$team,page:1,page_size:999}')")
staff_uuid() {
  jq -r --arg name "$1" '.data.rows[] | select(.name == $name) | .uuid' <<<"$staffs" | head -1
}
athlete1=$(staff_uuid '运动员 1')
athlete6=$(staff_uuid '运动员 6')
athlete8=$(staff_uuid '运动员 8')
[[ -n "$athlete1" && -n "$athlete6" && -n "$athlete8" ]]

joint_norm=$(post_form /p/cate/jointNorm -d '')
resolve_path() {
  jq -c --arg root "$1" --arg cate "$2" --arg norm "$3" '
    .data.rows[] | select(.name == $root) as $root_row |
    $root_row.children[] | select(.name == $cate) as $cate_row |
    $cate_row.children[] | select(.name == $norm) |
    [$root_row.uuid,$cate_row.uuid,.uuid]
  ' <<<"$joint_norm" | head -1
}

strength_path=$(resolve_path '体能测试指标' '相对最大握力' '左手握力')
stage_path=$(resolve_path '机能状态指标' '阶段生化测试' '红细胞')
sg_path=$(resolve_path '机能状态指标' '机能设备指标' 'SG')
aly_path=$(resolve_path '机能状态指标' '机能设备指标' 'ALY#')
for path in "$strength_path" "$stage_path" "$sg_path" "$aly_path"; do
  [[ -n "$path" && "$path" != null ]]
done

strength_norm=$(jq -r '.[-1]' <<<"$strength_path")
stage_norm=$(jq -r '.[-1]' <<<"$stage_path")
sg_norm=$(jq -r '.[-1]' <<<"$sg_path")
aly_norm=$(jq -r '.[-1]' <<<"$aly_path")

strength=$(post_form /p/analyse/analyse \
  -d "start_date=2026-05-01&end_date=2026-08-31&staff_uuid=$athlete1&norm_uuid_str=$strength_norm")
jq -e --arg norm "$strength_norm" '
  .code == 0 and
  ([.data.chart[] | select(.uuid == $norm) | .data[]] == ["44","45","47","48"]) and
  ([.data.row[] | select(.key == "平均值") | .[$norm]] == [46])
' >/dev/null <<<"$strength"

mixed_paths=$(jq -cn --argjson strength "$strength_path" --argjson stage "$stage_path" '[$strength,$stage]')
mixed=$(post_form /p/analyse/jointAnalyse \
  --data-urlencode "staff_uuid_str=$athlete1" \
  --data-urlencode 'start_date=2026-05-01' \
  --data-urlencode 'end_date=2026-08-31' \
  --data-urlencode "cate_norm=$mixed_paths")
jq -e --arg strength "$strength_norm" --arg stage "$stage_norm" '
  .code == 0 and
  ([.data.rows[] | select(.name != "平均值") | .[$strength] | select(. != "")] == ["44","45","47","48"]) and
  ([.data.rows[] | select(.name != "平均值") | .[$stage] | select(. != "")] == ["4.62","4.55","4.48","4.67"]) and
  ([.data.rows[] | select(.name == "平均值") | .[$strength]] == [46]) and
  ([.data.rows[] | select(.name == "平均值") | .[$stage]] == [4.58])
' >/dev/null <<<"$mixed"

sg=$(post_form /p/analyse/analyse \
  -d "start_date=2026-05-01&end_date=2026-08-31&staff_uuid=$athlete8&norm_uuid_str=$sg_norm")
jq -e --arg norm "$sg_norm" '
  .code == 0 and
  ([.data.row[] | select(.key == "2026-08-26") | .[$norm].value] == ["0"]) and
  ([.data.row[] | select(.key == "平均值") | .[$norm]] == [0]) and
  ([.data.row[] | select(.key == "标准差") | .[$norm]] == [0]) and
  ([.data.chart[] | select(.uuid == $norm) | .data["2026-08-26"]] == ["0"])
' >/dev/null <<<"$sg"

aly=$(post_form /p/analyse/analyse \
  -d "start_date=2026-05-01&end_date=2026-08-31&staff_uuid=$athlete6&norm_uuid_str=$aly_norm")
jq -e --arg norm "$aly_norm" '
  .code == 0 and
  ([.data.row[] | select(.key == "2026-07-29") | .[$norm].value | tonumber] == [0]) and
  ([.data.chart[] | select(.uuid == $norm) | .data["2026-07-29"] | tonumber] == [0])
' >/dev/null <<<"$aly"

jq -nc '{individual_strength:true,joint_mixed:true,stage_biochemistry:true,device_zero:true,equipment_business_time_latest:true}'
