#!/usr/bin/env bash
# Regenerate the BASE Postman collection from the live OpenAPI spec, so the
# endpoint list can never drift from the API. The committed
# demo-api.postman_collection.json is this base *enriched* by hand (collection-level
# bearer auth, token-capture scripts, the MailHog-driven Smoke folder, assertions).
#
# Usage:  BASE_URL=http://localhost:4455 ./scripts/gen-postman.sh
set -euo pipefail

BASE_URL="${BASE_URL:-http://localhost:4455}"
OUT="docs/postman/generated-base.postman_collection.json"

echo "==> Exporting OpenAPI from ${BASE_URL}/v3/api-docs"
curl -fsS "${BASE_URL}/v3/api-docs" -o /tmp/demo-openapi.json

echo "==> Converting to a Postman v2.1 collection (openapi-to-postmanv2)"
npx --yes openapi-to-postmanv2 \
  -s /tmp/demo-openapi.json \
  -o "${OUT}" -p \
  -O folderStrategy=Tags,requestParametersResolution=Example

echo "==> Wrote ${OUT}"
echo "Enrichment (scripts, env vars, Smoke folder) is maintained in"
echo "docs/postman/demo-api.postman_collection.json — re-apply it after regenerating the base."
