#!/bin/bash
# Story: #1335 (Admin can edit object schema, picklists, and record types)
# AC3:   (Happy path) Admin creates a picklist on Lead → adds 3 values → creates a
#        RecordType → assigns a layout → a fresh user session sees the configured
#        picklist values in the correct layout, end-to-end
#
# This script narrowly tests the FIRST blocking step: RecordType POST.
# Prior rounds: R4 saw 404 (endpoint missing); R6 saw 500 (endpoint exists but broken).
#
# PASS:    POST /sobjects/RecordType returns 201 with id
# FAIL:    POST returns 4xx-5xx error
# BLOCKED: admin token unavailable

# Related issue: pack#1091

. "$(dirname "$0")/../common/lib.sh"

TOK=$(get_admin_token) || {
  emit_verdict "#1335" "AC3" "BLOCKED" "admin token unavailable"
  exit 0
}

TS=$(date +%s)
RT_RESP=$(api_post "$TOK" "/sobjects/RecordType" "{
  \"Name\":\"qa_r6_rt_${TS}\",
  \"DeveloperName\":\"qa_r6_rt_${TS}\",
  \"SobjectType\":\"Lead\",
  \"IsActive\":true
}")
RT_ID=$(echo "$RT_RESP" | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('id','') if isinstance(d,dict) else '')" 2>/dev/null)

EV="response=$(echo "$RT_RESP" | head -c 200)"

if [ -n "$RT_ID" ]; then
  api_status "$TOK" DELETE "/sobjects/RecordType/$RT_ID" > /dev/null
  emit_verdict "#1335" "AC3" "PASS" "rt_id=$RT_ID (first step of happy path passes; full layout-assign + cross-session verify NOT covered by this narrow script)"
else
  emit_verdict "#1335" "AC3" "FAIL" "$EV"
fi
