#!/usr/bin/env bash
# Lecture seule de l'API publique. Aucune écriture.
# Variables : TWA_API_KEY, TWA_ORG_ID, TWA_PROJECT_ID.
# TWA_SINCE (optionnel) : instant ISO 8601, sinon les dernières 24 heures.
set -euo pipefail

BASE="${TWA_BASE_URL:-https://app.techwatchalert.com}"
auth="Authorization: Bearer ${TWA_API_KEY:?}"

if [[ -z "${TWA_SINCE:-}" ]]; then
  TWA_SINCE="$(python3 -c 'from datetime import datetime, timedelta, timezone; print((datetime.now(timezone.utc) - timedelta(hours=24)).strftime("%Y-%m-%dT%H:%M:%SZ"))')"
fi

curl -fsS -H "$auth" "$BASE/api/v1/me"
curl -fsS -H "$auth" "$BASE/api/v1/organizations"
curl -fsS -H "$auth" "$BASE/api/v1/organizations/${TWA_ORG_ID:?}/projects"
curl -fsS -H "$auth" "$BASE/api/v1/organizations/${TWA_ORG_ID}/projects/${TWA_PROJECT_ID:?}/stack"
curl -fsS -G -H "$auth" \
  --data-urlencode "since=${TWA_SINCE}" \
  --data-urlencode "limit=50" \
  "$BASE/api/v1/organizations/${TWA_ORG_ID}/projects/${TWA_PROJECT_ID}/alerts"
