#!/usr/bin/env bash
# onx-postqueue-hold — postsuper -h <QUEUE_ID> ("ALL" desteklenir)
# stdin: {"queue_id":"A1B2C3D4E"}  veya {"queue_id":"ALL"}
# stdout: {"ok":true,"queue_id":"…","message":"…"}

set -euo pipefail
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
# shellcheck disable=SC1091
source "${SCRIPT_DIR}/_lib/common.sh"

require_root
require_cmd postsuper
require_cmd jq

INPUT="$(cat)"
QUEUE_ID="$(_json_get "$INPUT" "queue_id")"

if [[ -z "$QUEUE_ID" ]]; then
    json_fail 1 "Eksik alan: queue_id"
fi

if [[ "$QUEUE_ID" != "ALL" ]] && ! [[ "$QUEUE_ID" =~ ^[A-Za-z0-9]+$ ]]; then
    json_fail 1 "Geçersiz queue_id formatı"
fi

if ! postsuper -h "$QUEUE_ID" 2>/dev/null; then
    json_fail 3 "postsuper -h ${QUEUE_ID} başarısız"
fi

json_ok "{\"ok\":true,\"queue_id\":\"${QUEUE_ID}\",\"message\":\"postsuper -h ${QUEUE_ID} tamamlandı (hold)\"}"
