#!/bin/bash
#
# Script zum Anlegen des Gitea Issues ohne jq dependency
#

GITEA_TOKEN="b3d27c7d87d2acf39490d0c58f26922164edb4e8"
GITEA_URL="http://192.168.2.144:3000"
REPO_OWNER="Lars"
REPO_NAME="mitai-jinkendo"

# Issue Body aus Datei lesen (erste 29 Zeilen überspringen = Metadaten)
ISSUE_BODY=$(tail -n +30 "c:/Dev/mitai-jinkendo/.claude/task/rework_0b_placeholder/ISSUE_METADATEN_REVIEW.md" | python3 -c "import sys, json; print(json.dumps(sys.stdin.read()))")

# JSON Payload mit Python erstellen
python3 << PYEOF > /tmp/issue_payload.json
import json

body = $ISSUE_BODY

payload = {
    "title": "Placeholder Registry: UNRESOLVED & TO_VERIFY Metadaten prüfen",
    "body": body,
    "labels": [2, 3]
}

print(json.dumps(payload, ensure_ascii=False))
PYEOF

echo "Erstelle Gitea Issue..."
echo "Repository: $REPO_OWNER/$REPO_NAME"
echo ""

# Issue via API anlegen
RESPONSE=$(curl -s -X POST \
  "$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/issues" \
  -H "Authorization: token $GITEA_TOKEN" \
  -H "Content-Type: application/json" \
  -d @/tmp/issue_payload.json)

# Response mit Python parsen
python3 << PYEOF
import json

response = '''$RESPONSE'''
try:
    data = json.loads(response)
    if 'number' in data:
        print(f"✓ Issue erfolgreich erstellt!")
        print(f"")
        print(f"Issue #{data['number']}")
        print(f"URL: {data['html_url']}")
        print(f"")
        print(f"✓ Fertig!")
    else:
        print(f"✗ Fehler beim Erstellen des Issues:")
        print(json.dumps(data, indent=2))
except Exception as e:
    print(f"✗ Fehler: {e}")
    print(response)
PYEOF

# Cleanup
rm -f /tmp/issue_payload.json
