#!/usr/bin/env bash
# Integración local para POST /wp-json/fea/v1/crear-traduccion (#222).
# Requiere un post ES temporal SIN traducción inglesa; el caller lo elimina
# junto con la traducción creada al terminar.
set -euo pipefail

: "${FEA_TEST_URL:?Falta FEA_TEST_URL}"
: "${FEA_TEST_AUTH:?Falta FEA_TEST_AUTH}"
: "${FEA_TEST_ES_ID:?Falta FEA_TEST_ES_ID}"

endpoint="${FEA_TEST_URL%/}/wp-json/fea/v1/crear-traduccion"
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT

assert_status() {
  local expected="$1" actual="$2" label="$3" response_file="$4"
  if [[ "$actual" != "$expected" ]]; then
    echo "FAIL: $label — esperado HTTP $expected, recibido $actual" >&2
    cat "$response_file" >&2 || true
    exit 1
  fi
}

status="$(curl -sS -o "$tmpdir/unauth.json" -w '%{http_code}' -X POST "$endpoint")"
assert_status 401 "$status" 'llamada sin autenticar' "$tmpdir/unauth.json"

status="$(curl -sS -o "$tmpdir/no-source.json" -w '%{http_code}' -u "$FEA_TEST_AUTH" -F 'lang=en' "$endpoint")"
assert_status 400 "$status" 'falta es_id' "$tmpdir/no-source.json"

status="$(curl -sS -o "$tmpdir/no-lang.json" -w '%{http_code}' -u "$FEA_TEST_AUTH" -F "es_id=$FEA_TEST_ES_ID" "$endpoint")"
assert_status 400 "$status" 'falta lang' "$tmpdir/no-lang.json"

status="$(curl -sS -o "$tmpdir/bad-lang.json" -w '%{http_code}' -u "$FEA_TEST_AUTH" -F "es_id=$FEA_TEST_ES_ID" -F 'lang=de' "$endpoint")"
assert_status 400 "$status" 'lang no admitido' "$tmpdir/bad-lang.json"

status="$(curl -sS -o "$tmpdir/bad-status.json" -w '%{http_code}' -u "$FEA_TEST_AUTH" -F "es_id=$FEA_TEST_ES_ID" -F 'lang=en' -F 'status=private' "$endpoint")"
assert_status 400 "$status" 'status no admitido' "$tmpdir/bad-status.json"

status="$(curl -sS -o "$tmpdir/no-such-source.json" -w '%{http_code}' -u "$FEA_TEST_AUTH" -F 'es_id=999999999' -F 'lang=en' "$endpoint")"
assert_status 404 "$status" 'post ES inexistente' "$tmpdir/no-such-source.json"

status="$(curl -sS -o "$tmpdir/created.json" -w '%{http_code}' -u "$FEA_TEST_AUTH" -F "es_id=$FEA_TEST_ES_ID" -F 'lang=en' -F 'title=Translation API test' --form-string 'content=<p>Body API test</p>' -F 'excerpt=Excerpt API test' -F 'status=draft' -F 'model=test-model' "$endpoint")"
assert_status 201 "$status" 'creación de traducción' "$tmpdir/created.json"

python3 - "$tmpdir/created.json" "$FEA_TEST_ES_ID" <<'PY'
import json, sys
payload = json.load(open(sys.argv[1]))
assert int(payload['es_id']) == int(sys.argv[2]), payload
assert payload['lang'] == 'en', payload
assert int(payload['translation_id']) > 0, payload
assert payload['created'] is True, payload
assert payload['url'].startswith(('http://', 'https://')), payload
print('PASS: traducción creada', payload['translation_id'])
PY

status="$(curl -sS -o "$tmpdir/idempotent.json" -w '%{http_code}' -u "$FEA_TEST_AUTH" -F "es_id=$FEA_TEST_ES_ID" -F 'lang=en' -F 'title=No debe crear otro post' "$endpoint")"
assert_status 200 "$status" 'idempotencia' "$tmpdir/idempotent.json"

python3 - "$tmpdir/created.json" "$tmpdir/idempotent.json" <<'PY'
import json, sys
created = json.load(open(sys.argv[1]))
again = json.load(open(sys.argv[2]))
assert again['translation_id'] == created['translation_id'], (created, again)
assert again['created'] is False, again
print('PASS: idempotencia', again['translation_id'])
PY
