#!/bin/bash
set -e

# Activate sbsports repository in Woodpecker CI
# Prerequisites: WP_TOKEN, Gitea remote ID

WP_TOKEN="${WP_TOKEN:-}"
WP_API="https://ci.thax.es/api"
GITEA_REMOTE_ID="${SBSPORTS_REPO_ID:-}"

if [ -z "$WP_TOKEN" ]; then
    echo "Error: WP_TOKEN not set"
    echo "Usage: WP_TOKEN=your_token SBSPORTS_REPO_ID=34 ./scripts/activate-woodpecker.sh"
    exit 1
fi

if [ -z "$GITEA_REMOTE_ID" ]; then
    echo "Error: SBSPORTS_REPO_ID not set (Gitea remote ID)"
    exit 1
fi

echo "=== Activating sbsports in Woodpecker ==="
echo ""

# 1. Activate the repository
echo "Activating repository (Gitea remote ID: $GITEA_REMOTE_ID)..."
curl -sSLk -X POST "$WP_API/repos?forge_remote_id=$GITEA_REMOTE_ID" \
  -H "Authorization: Bearer $WP_TOKEN" | jq '.'

echo ""
echo "2. Set the following secrets in Woodpecker (via UI or API):"
echo "   - gitea_user (username for git.thax.es registry)"
echo "   - gitea_token (token for git.thax.es registry)"
echo "   - coolify_api_token (Coolify API token)"
echo ""
echo "3. After setting up Coolify, add:"
echo "   - SBSPORTS_COOLIFY_UUID (service UUID from Coolify)"
echo ""
echo "Done!"
