#!/bin/bash

# Test script for Gitea wiki functionality

set -e

# Configuration
SKILL_DIR="/Users/gabrielradureau/Work/Vibe/DanceLessonsCoach/.vibe/skills/product-owner-assistant"
GITEA_API="https://gitea.arcodange.lab/api/v1"
OWNER="arcodange"
REPO="dance-lessons-coach"

# Check if token is available
if [ -z "$GITEA_API_TOKEN" ] && [ -z "$GITEA_API_TOKEN_FILE" ]; then
    echo "Error: Gitea API token not configured"
    echo "Set GITEA_API_TOKEN or GITEA_API_TOKEN_FILE environment variable"
    exit 1
fi

# Get token
if [ -n "$GITEA_API_TOKEN_FILE" ]; then
    TOKEN=$(cat "$GITEA_API_TOKEN_FILE")
else
    TOKEN="$GITEA_API_TOKEN"
fi

# Test 1: List existing wiki pages
echo "=== Test 1: Listing existing wiki pages ==="
curl -s -X GET "${GITEA_API}/repos/${OWNER}/${REPO}/wiki/pages" \
  -H "Authorization: token ${TOKEN}" \
  -H "Accept: application/json" | jq '.'

echo ""
echo "=== Test 2: Create a test wiki page ==="
# Create test content
CONTENT="# Test Wiki Page\n\nThis is a test page created at $(date)\n\n- Test item 1\n- Test item 2\n"
CONTENT_BASE64=$(echo -n "$CONTENT" | base64)

# Create the page
curl -s -X POST "${GITEA_API}/repos/${OWNER}/${REPO}/wiki/new" \
  -H "Authorization: token ${TOKEN}" \
  -H "Content-Type: application/json" \
  -d "{
    \"title\": \"TestPage_$(date +%Y%m%d_%H%M%S)\",
    \"content_base64\": \"${CONTENT_BASE64}\",
    \"message\": \"Test page creation\"
  }" | jq '.'

echo ""
echo "=== Test 3: Get a specific wiki page (if exists) ==="
# Try to get the home page if it exists
curl -s -X GET "${GITEA_API}/repos/${OWNER}/${REPO}/wiki/page/Home" \
  -H "Authorization: token ${TOKEN}" \
  -H "Accept: application/json" | jq '.' || echo "Home page not found"

echo ""
echo "=== Wiki API Test Complete ==="
echo "✅ Wiki functionality is working"
echo "📚 The Product Owner Assistant can create wiki pages for epics"