#!/usr/bin/env bash
# Probe four-class entropy, then one stage-2 tag if the enrichment gate passes.
# Keep best.pth only when Score is above the 66.83 stage-2 mark.
set -uo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

SEED="${SEED:-1}"
BATCH_SIZE="${BATCH_SIZE:-16}"
FLOOR="${FLOOR:-66.83}"
MIN_FREE_MB="${MIN_FREE_MB:-1500}"
CKPT="${CKPT:-$ROOT/save/icbhi_beats_frame_asd_frame_asd_F12_sastruct_s1_rerun2/best.pth}"
PROBE_JSON="$ROOT/save/entropy_anchor_s1.json"
BOARD="$ROOT/save/s2_entropy_board.json"
KEEP_DIR="$ROOT/save/icbhi_beats_frame_asd_frame_asd_F12_feedback_s2"
STAGE1_DIR="$ROOT/save/icbhi_beats_frame_asd_frame_asd_F12_sastruct_s1_rerun2"
TAG="frame_asd_F12_s2exp_entanchor"
OUT="$ROOT/save/icbhi_beats_frame_asd_${TAG}"

[[ -f "$CKPT" ]] || { echo "Missing stage-1 checkpoint $CKPT"; exit 1; }
[[ -f "$ROOT/pretrained_models/BEATs_iter3_plus_AS2M.pt" ]] || { echo "Missing pretrained_models/BEATs_iter3_plus_AS2M.pt"; exit 1; }
mkdir -p "$ROOT/save"
if [[ ! -f "$BOARD" ]]; then
  echo '[]' > "$BOARD"
fi
if [[ "$OUT" == "$KEEP_DIR" || "$OUT" == "$STAGE1_DIR" ]]; then
  echo "Refusing to write a protected directory"
  exit 1
fi

record_stop() {
  local reason="$1"
  python - "$BOARD" "$TAG" "$OUT" "$PROBE_JSON" "$reason" "$KEEP_DIR" "$STAGE1_DIR" <<'PY'
import json, os, sys
board_path, tag, out, probe_path, reason, keep_dir, stage1_dir = sys.argv[1:8]
if os.path.abspath(out) in (os.path.abspath(keep_dir), os.path.abspath(stage1_dir)):
    raise SystemExit('Refusing to record a stop inside a protected checkpoint directory')
entry = {
    'tag': tag,
    'dir': out,
    'score': None,
    'sp': None,
    'se': None,
    'both': None,
    'normal_wheeze_fp': None,
    'kept': False,
    'trained': False,
    'reason': reason,
}
if os.path.isfile(probe_path):
    probe = json.load(open(probe_path))
    entry['entropy_layer'] = probe.get('entropy_layer')
    entry['enrichment'] = {
        str(row['layer']): {
            'both': row['enrichment']['both'],
            'crackle': row['enrichment']['crackle'],
        }
        for row in probe.get('layers', [])
    }
board = json.load(open(board_path)) if os.path.isfile(board_path) else []
board = [row for row in board if row.get('tag') != tag]
board.append(entry)
with open(board_path, 'w') as handle:
    json.dump(board, handle, indent=2)
print('STOP {} {}'.format(tag, reason))
PY
}

record_one() {
  python - "$BOARD" "$TAG" "$OUT" "$FLOOR" "$KEEP_DIR" "$STAGE1_DIR" <<'PY'
import json, os, sys
board_path, tag, out, floor, keep_dir, stage1_dir = sys.argv[1:7]
floor = float(floor)
if os.path.abspath(out) in (os.path.abspath(keep_dir), os.path.abspath(stage1_dir)):
    raise SystemExit('Refusing to score the protected checkpoint directory')
thr_path = os.path.join(out, 'best_threshold.json')
metrics_path = os.path.join(out, 'class_metrics.json')
entry = {
    'tag': tag,
    'dir': out,
    'score': None,
    'sp': None,
    'se': None,
    'both': None,
    'normal_wheeze_fp': None,
    'kept': False,
    'trained': True,
}
if os.path.isfile(thr_path):
    data = json.load(open(thr_path))
    entry['score'] = float(data['score'])
    entry['sp'] = float(data['sp'])
    entry['se'] = float(data['se'])
    entry['kept'] = entry['score'] > floor
if os.path.isfile(metrics_path):
    metrics = json.load(open(metrics_path))
    entry['both'] = float(metrics['abnormal_recalls']['both'])
    entry['normal_wheeze_fp'] = int(metrics['normal_wheeze_fp'])
ckpt = os.path.join(out, 'best.pth')
if entry['kept']:
    print('KEEP {} Score {:.2f} Sp {:.1f} Se {:.1f}'.format(
        tag, entry['score'], entry['sp'], entry['se']))
elif os.path.isfile(ckpt):
    os.remove(ckpt)
    shown = entry['score'] if entry['score'] is not None else float('nan')
    print('DROP checkpoint {} Score {:.2f} is not above {:.2f}'.format(tag, shown, floor))
else:
    print('DROP {} no checkpoint (score {})'.format(tag, entry['score']))
board = json.load(open(board_path)) if os.path.isfile(board_path) else []
board = [row for row in board if row.get('tag') != tag]
board.append(entry)
with open(board_path, 'w') as handle:
    json.dump(board, handle, indent=2)
PY
}

echo "=== Entropy probe ==="
python -u scripts/probe_entropy_anchors.py \
  --resume "$CKPT" \
  --out "$PROBE_JSON" \
  --model beats --dataset icbhi --seed "$SEED" \
  --class_split lungsound --n_cls 4 --batch_size "$BATCH_SIZE" \
  --desired_length 5 --pad_types repeat \
  --test_fold official \
  --mil_pool topk --mil_topk 3 \
  --beats_wave_scale 32768 \
  --num_workers 4
probe_status=$?
if [[ "$probe_status" -ne 0 ]]; then
  echo "Probe failed (exit $probe_status)"
  exit "$probe_status"
fi

mapfile -t DECISION < <(python - "$PROBE_JSON" <<'PY'
import json, sys
data = json.load(open(sys.argv[1]))
print('1' if data.get('train') else '0')
print(int(data['entropy_layer']))
print(int(data['frame_layer']))
print(int(data['slot_layer']))
print(data.get('reason', ''))
PY
)
TRAIN_FLAG="${DECISION[0]}"
ENTROPY_LAYER="${DECISION[1]}"
FRAME_LAYER="${DECISION[2]}"
SLOT_LAYER="${DECISION[3]}"
REASON="${DECISION[4]}"

if [[ "$TRAIN_FLAG" != "1" ]]; then
  record_stop "$REASON"
  exit 0
fi

if [[ -f "$OUT/train.log" ]] && grep -q '^Finished\. Best Score:' "$OUT/train.log"; then
  echo "=== $TAG already finished; applying keep rule ==="
  record_one
  exit 0
fi

free_mb=$(df --output=avail -BM "$ROOT/save" | tail -1 | tr -dc '0-9')
if (( free_mb < MIN_FREE_MB )); then
  echo "Only ${free_mb}MB free (need >= ${MIN_FREE_MB}MB)"
  exit 1
fi
mkdir -p "$OUT"
rm -f "$OUT/best.pth"
echo "=== Stage 2 $TAG entropy $ENTROPY_LAYER frame $FRAME_LAYER slot $SLOT_LAYER ==="
set +e
python -u train_feedback.py --tag "$TAG" \
  --method frame_asd --model beats --dataset icbhi --seed "$SEED" \
  --save_dir "$ROOT/save" \
  --class_split lungsound --n_cls 4 --batch_size "$BATCH_SIZE" \
  --desired_length 5 --optimizer adam --weight_decay 1e-6 \
  --cosine --test_fold official --pad_types repeat \
  --ma_update --ma_beta 0.5 --print_freq 50 \
  --min_se 48 --min_sp 72 --early_stop_patience 8 \
  --mil_pool topk --mil_topk 3 \
  --use_specaug --specaug_policy structured \
  --beats_wave_scale 32768 --threshold_n_points 41 \
  --ablation_stage F12_s2exp_entanchor \
  --feedback_slots 4 --feedback_layers 2 --feedback_drop 0.2 \
  --reason_codes 16 \
  --epochs 10 \
  --frame_layer "$FRAME_LAYER" \
  --slot_layer "$SLOT_LAYER" \
  --entropy_anchor \
  --entropy_layer "$ENTROPY_LAYER" \
  --reset_epoch_on_resume \
  --resume "$CKPT" \
  2>&1 | tee "$OUT/train.log"
status=${PIPESTATUS[0]}
set -e
if [[ "$status" -ne 0 ]]; then
  echo "Run failed for $TAG (exit $status)"
  exit "$status"
fi
record_one
