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

usage() {
  printf '%s\n' \
    'usage:' \
    '  finalize-task-policy.sh pair <build-base-basis> <large-indivisible-context> <loop-risk-count> <review-rework-count> <evidence-integrity-failure> <build-scope> <build-state> <build-blast> <build-evidence> <build-verification> official-review <review-scope> <review-state> <review-blast> <review-evidence> <review-verification>' \
    '  build-base-basis: local-fit | capability-gap | grade-boundary' \
    '  boolean fields: true | false; loop-risk-count: 0..5; review-rework-count: non-negative integer' >&2
  exit 2
}

script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
formatter="$script_dir/finalize-task-route.sh"

field() {
  local output=$1
  local key=$2
  local value
  value=$(sed -n "s/^${key}=//p" <<<"$output")
  if [[ -z "$value" ]]; then
    printf 'missing formatter field: %s\n' "$key" >&2
    exit 3
  fi
  printf '%s\n' "$value"
}

finalize_build() {
  local base_basis=$1
  shift
  local scores=("$@")
  local lane
  local route

  case "$base_basis" in
    local-fit|capability-gap|grade-boundary)
      ;;
    *)
      usage
      ;;
  esac

  BUILD_BASE_ROUTE_BASIS=$base_basis
  BUILD_RISK_BOUNDARY_MATCHED=false
  BUILD_RECOVERY_BOUNDARY_MATCHED=false
  if [[ "$BUILD_LARGE_INDIVISIBLE_CONTEXT" == true ||
        "$BUILD_LOOP_RISK_COUNT" -ge 4 ]]; then
    BUILD_RISK_BOUNDARY_MATCHED=true
  fi
  if [[ "$BUILD_EVIDENCE_INTEGRITY_FAILURE" == true ||
        "$BUILD_REVIEW_REWORK_COUNT" -ge 2 ]]; then
    BUILD_RECOVERY_BOUNDARY_MATCHED=true
  fi

  if [[ "$base_basis" == capability-gap ]]; then
    BUILD_ROUTE_BASIS=capability-gap
  elif [[ "$base_basis" == local-fit &&
          "$BUILD_RECOVERY_BOUNDARY_MATCHED" == true ]]; then
    BUILD_ROUTE_BASIS=recovery-boundary
  elif [[ "$base_basis" == local-fit &&
          "$BUILD_RISK_BOUNDARY_MATCHED" == true ]]; then
    BUILD_ROUTE_BASIS=risk-boundary
  else
    BUILD_ROUTE_BASIS=$base_basis
  fi

  if [[ "$BUILD_ROUTE_BASIS" == local-fit ]]; then
    lane=local
  else
    lane=cloud
  fi

  route=$("$formatter" build "$lane" "${scores[@]}")
  BUILD_GRADE=$(field "$route" grade)
  if [[ "$base_basis" == local-fit &&
        ( "$BUILD_GRADE" == G09 || "$BUILD_GRADE" == G10 ) ]]; then
    printf 'local-fit is valid only for G01-G08\n' >&2
    exit 2
  fi
  if [[ "$base_basis" == grade-boundary &&
        "$BUILD_GRADE" != G09 && "$BUILD_GRADE" != G10 ]]; then
    printf 'grade-boundary is valid only for G09-G10\n' >&2
    exit 2
  fi
  BUILD_LANE=$(field "$route" lane)
  BUILD_FILENAME=$(field "$route" filename)
}

finalize_review() {
  local route_basis=$1
  shift
  local scores=("$@")
  local route

  [[ "$route_basis" == official-review ]] || usage
  route=$("$formatter" review cloud "${scores[@]}")
  REVIEW_ROUTE_BASIS=$route_basis
  REVIEW_LANE=$(field "$route" lane)
  REVIEW_GRADE=$(field "$route" grade)
  REVIEW_FILENAME=$(field "$route" filename)
}

emit_build() {
  printf 'build_base_route_basis=%s\n' "$BUILD_BASE_ROUTE_BASIS"
  printf 'build_route_basis=%s\n' "$BUILD_ROUTE_BASIS"
  printf 'build_large_indivisible_context=%s\n' "$BUILD_LARGE_INDIVISIBLE_CONTEXT"
  printf 'build_loop_risk_count=%s\n' "$BUILD_LOOP_RISK_COUNT"
  printf 'build_risk_boundary_matched=%s\n' "$BUILD_RISK_BOUNDARY_MATCHED"
  printf 'build_review_rework_count=%s\n' "$BUILD_REVIEW_REWORK_COUNT"
  printf 'build_evidence_integrity_failure=%s\n' "$BUILD_EVIDENCE_INTEGRITY_FAILURE"
  printf 'build_recovery_boundary_matched=%s\n' "$BUILD_RECOVERY_BOUNDARY_MATCHED"
  printf 'build_lane=%s\n' "$BUILD_LANE"
  printf 'build_grade=%s\n' "$BUILD_GRADE"
  printf 'build_filename=%s\n' "$BUILD_FILENAME"
  printf 'build_catalog_route=worker/%s/%s\n' "$BUILD_LANE" "$BUILD_GRADE"
}

emit_review() {
  printf 'review_route_basis=%s\n' "$REVIEW_ROUTE_BASIS"
  printf 'review_lane=%s\n' "$REVIEW_LANE"
  printf 'review_grade=%s\n' "$REVIEW_GRADE"
  printf 'review_filename=%s\n' "$REVIEW_FILENAME"
  printf 'review_catalog_route=review/%s/%s\n' "$REVIEW_LANE" "$REVIEW_GRADE"
}

mode=${1:-}
[[ "$mode" == pair && $# -eq 17 ]] || usage

case "$3" in
  true|false)
    BUILD_LARGE_INDIVISIBLE_CONTEXT=$3
    ;;
  *)
    usage
    ;;
esac

case "$4" in
  0|1|2|3|4|5)
    BUILD_LOOP_RISK_COUNT=$4
    ;;
  *)
    usage
    ;;
esac
[[ "$5" =~ ^[0-9]+$ ]] || usage
BUILD_REVIEW_REWORK_COUNT=$5

case "$6" in
  true|false)
    BUILD_EVIDENCE_INTEGRITY_FAILURE=$6
    ;;
  *)
    usage
    ;;
esac

finalize_build "$2" "${@:7:5}"
finalize_review "${12}" "${@:13:5}"

printf 'finalizer=finalize-task-policy.sh\n'
printf 'finalizer_mode=%s\n' "$mode"
emit_build
emit_review
