#!/bin/bash
# Script to test coordinator bot functionality in Gitea
# Usage: ./scripts/coordinator/test-gitea-bot.sh

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Configuration (load from environment or .env)
if [ -f .env ]; then
    set -a
    source .env
    set +a
fi

GITEA_URL="${GITEA_URL:-https://git.mosaicstack.dev}"
GITEA_BOT_TOKEN="${GITEA_BOT_TOKEN:-}"
GITEA_BOT_USERNAME="${GITEA_BOT_USERNAME:-mosaic}"
GITEA_REPO_OWNER="${GITEA_REPO_OWNER:-mosaic}"
GITEA_REPO_NAME="${GITEA_REPO_NAME:-stack}"
TEST_ISSUE="${TEST_ISSUE:-156}"

# Functions
print_header() {
    echo -e "\n${BLUE}========================================${NC}"
    echo -e "${BLUE}$1${NC}"
    echo -e "${BLUE}========================================${NC}\n"
}

print_success() {
    echo -e "${GREEN}✓ $1${NC}"
}

print_error() {
    echo -e "${RED}✗ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}! $1${NC}"
}

print_info() {
    echo -e "${BLUE}ℹ $1${NC}"
}

# Check dependencies
command -v curl >/dev/null 2>&1 || { echo -e "${RED}curl is required but not installed.${NC}"; exit 1; }
command -v jq >/dev/null 2>&1 || { echo -e "${RED}jq is required but not installed.${NC}"; exit 1; }

# Check for bot token
if [ -z "$GITEA_BOT_TOKEN" ]; then
    print_error "GITEA_BOT_TOKEN environment variable not set"
    echo -e "\n${YELLOW}To use this script:${NC}"
    echo "1. Ensure .env file contains GITEA_BOT_TOKEN"
    echo "2. Or export: export GITEA_BOT_TOKEN='your-bot-token'"
    echo "3. Run: ./scripts/coordinator/test-gitea-bot.sh"
    exit 1
fi

print_header "Gitea Bot Functionality Tests"
print_info "Gitea URL: $GITEA_URL"
print_info "Bot Username: $GITEA_BOT_USERNAME"
print_info "Repository: $GITEA_REPO_OWNER/$GITEA_REPO_NAME"
print_info "Test Issue: #$TEST_ISSUE"

# Test 1: Verify Bot Authentication
print_header "Test 1: Bot Authentication"

AUTH_RESPONSE=$(curl -s -w "\n%{http_code}" \
    -H "Authorization: token $GITEA_BOT_TOKEN" \
    "$GITEA_URL/api/v1/user")

AUTH_HTTP_CODE=$(echo "$AUTH_RESPONSE" | tail -n1)
AUTH_BODY=$(echo "$AUTH_RESPONSE" | head -n-1)

if [[ "$AUTH_HTTP_CODE" == "200" ]]; then
    BOT_ID=$(echo "$AUTH_BODY" | jq -r '.id')
    BOT_NAME=$(echo "$AUTH_BODY" | jq -r '.username')
    print_success "Authentication successful"
    print_info "Bot ID: $BOT_ID"
    print_info "Bot Username: $BOT_NAME"
else
    print_error "Authentication failed (HTTP $AUTH_HTTP_CODE)"
    print_error "Response: $AUTH_BODY"
    exit 1
fi

# Test 2: List Repository
print_header "Test 2: Repository Access"

REPO_RESPONSE=$(curl -s -w "\n%{http_code}" \
    -H "Authorization: token $GITEA_BOT_TOKEN" \
    "$GITEA_URL/api/v1/repos/$GITEA_REPO_OWNER/$GITEA_REPO_NAME")

REPO_HTTP_CODE=$(echo "$REPO_RESPONSE" | tail -n1)
REPO_BODY=$(echo "$REPO_RESPONSE" | head -n-1)

if [[ "$REPO_HTTP_CODE" == "200" ]]; then
    REPO_ID=$(echo "$REPO_BODY" | jq -r '.id')
    print_success "Repository access successful"
    print_info "Repository ID: $REPO_ID"
else
    print_error "Repository access failed (HTTP $REPO_HTTP_CODE)"
    exit 1
fi

# Test 3: List Issues
print_header "Test 3: List Issues"

ISSUES_RESPONSE=$(curl -s -w "\n%{http_code}" \
    -H "Authorization: token $GITEA_BOT_TOKEN" \
    "$GITEA_URL/api/v1/repos/$GITEA_REPO_OWNER/$GITEA_REPO_NAME/issues?limit=5")

ISSUES_HTTP_CODE=$(echo "$ISSUES_RESPONSE" | tail -n1)
ISSUES_BODY=$(echo "$ISSUES_RESPONSE" | head -n-1)

if [[ "$ISSUES_HTTP_CODE" == "200" ]]; then
    ISSUE_COUNT=$(echo "$ISSUES_BODY" | jq 'length')
    print_success "Issue listing successful"
    print_info "Found $ISSUE_COUNT issues"
    echo "$ISSUES_BODY" | jq -r '.[] | "  #\(.number): \(.title)"' | head -5
else
    print_error "Issue listing failed (HTTP $ISSUES_HTTP_CODE)"
    exit 1
fi

# Test 4: Read Specific Issue
print_header "Test 4: Read Issue #$TEST_ISSUE"

ISSUE_RESPONSE=$(curl -s -w "\n%{http_code}" \
    -H "Authorization: token $GITEA_BOT_TOKEN" \
    "$GITEA_URL/api/v1/repos/$GITEA_REPO_OWNER/$GITEA_REPO_NAME/issues/$TEST_ISSUE")

ISSUE_HTTP_CODE=$(echo "$ISSUE_RESPONSE" | tail -n1)
ISSUE_BODY=$(echo "$ISSUE_RESPONSE" | head -n-1)

if [[ "$ISSUE_HTTP_CODE" == "200" ]]; then
    ISSUE_TITLE=$(echo "$ISSUE_BODY" | jq -r '.title')
    ISSUE_STATE=$(echo "$ISSUE_BODY" | jq -r '.state')
    print_success "Issue #$TEST_ISSUE read successfully"
    print_info "Title: $ISSUE_TITLE"
    print_info "State: $ISSUE_STATE"
else
    print_warning "Issue #$TEST_ISSUE not found or not accessible (HTTP $ISSUE_HTTP_CODE)"
    print_info "Using first available issue for subsequent tests..."
    # Get first issue for testing
    TEST_ISSUE=$(echo "$ISSUES_BODY" | jq -r '.[0].number')
    print_info "Using issue #$TEST_ISSUE instead"
fi

# Test 5: Assign Issue to Bot
print_header "Test 5: Assign Issue #$TEST_ISSUE to Bot"

ASSIGN_RESPONSE=$(curl -s -w "\n%{http_code}" -X PATCH \
    -H "Authorization: token $GITEA_BOT_TOKEN" \
    -H "Content-Type: application/json" \
    "$GITEA_URL/api/v1/repos/$GITEA_REPO_OWNER/$GITEA_REPO_NAME/issues/$TEST_ISSUE" \
    -d "{\"assignees\":[\"$GITEA_BOT_USERNAME\"]}")

ASSIGN_HTTP_CODE=$(echo "$ASSIGN_RESPONSE" | tail -n1)
ASSIGN_BODY=$(echo "$ASSIGN_RESPONSE" | head -n-1)

if [[ "$ASSIGN_HTTP_CODE" == "201" ]] || [[ "$ASSIGN_HTTP_CODE" == "200" ]]; then
    ASSIGNEES=$(echo "$ASSIGN_BODY" | jq -r '.assignees[].username' | tr '\n' ',' | sed 's/,$//')
    print_success "Issue assigned successfully"
    print_info "Assignees: $ASSIGNEES"
else
    print_error "Assignment failed (HTTP $ASSIGN_HTTP_CODE)"
    print_error "Response: $ASSIGN_BODY"
    # Don't exit, continue with next test
fi

# Test 6: Comment on Issue
print_header "Test 6: Comment on Issue #$TEST_ISSUE"

TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
COMMENT_TEXT="Test comment from coordinator bot ($TIMESTAMP) - [Automated test, safe to delete]"

COMMENT_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
    -H "Authorization: token $GITEA_BOT_TOKEN" \
    -H "Content-Type: application/json" \
    "$GITEA_URL/api/v1/repos/$GITEA_REPO_OWNER/$GITEA_REPO_NAME/issues/$TEST_ISSUE/comments" \
    -d "{\"body\":\"$COMMENT_TEXT\"}")

COMMENT_HTTP_CODE=$(echo "$COMMENT_RESPONSE" | tail -n1)
COMMENT_BODY=$(echo "$COMMENT_RESPONSE" | head -n-1)

if [[ "$COMMENT_HTTP_CODE" == "201" ]]; then
    COMMENT_ID=$(echo "$COMMENT_BODY" | jq -r '.id')
    COMMENT_AUTHOR=$(echo "$COMMENT_BODY" | jq -r '.user.username')
    print_success "Comment posted successfully"
    print_info "Comment ID: $COMMENT_ID"
    print_info "Author: $COMMENT_AUTHOR"
else
    print_error "Comment posting failed (HTTP $COMMENT_HTTP_CODE)"
    print_error "Response: $COMMENT_BODY"
fi

# Test 7: Add Labels
print_header "Test 7: Add Labels to Issue #$TEST_ISSUE"

LABELS_RESPONSE=$(curl -s -w "\n%{http_code}" -X PATCH \
    -H "Authorization: token $GITEA_BOT_TOKEN" \
    -H "Content-Type: application/json" \
    "$GITEA_URL/api/v1/repos/$GITEA_REPO_OWNER/$GITEA_REPO_NAME/issues/$TEST_ISSUE" \
    -d '{"labels":["coordinator-test"]}')

LABELS_HTTP_CODE=$(echo "$LABELS_RESPONSE" | tail -n1)
LABELS_BODY=$(echo "$LABELS_RESPONSE" | head -n-1)

if [[ "$LABELS_HTTP_CODE" == "201" ]] || [[ "$LABELS_HTTP_CODE" == "200" ]]; then
    LABELS=$(echo "$LABELS_BODY" | jq -r '.labels[].name' | tr '\n' ',' | sed 's/,$//')
    print_success "Labels added successfully"
    print_info "Labels: $LABELS"
else
    print_warning "Labels update failed (HTTP $LABELS_HTTP_CODE)"
fi

# Test 8: Repository Permissions
print_header "Test 8: Check Bot Repository Permissions"

# Try to get repository branches (requires read access)
BRANCHES_RESPONSE=$(curl -s -w "\n%{http_code}" \
    -H "Authorization: token $GITEA_BOT_TOKEN" \
    "$GITEA_URL/api/v1/repos/$GITEA_REPO_OWNER/$GITEA_REPO_NAME/branches?limit=5")

BRANCHES_HTTP_CODE=$(echo "$BRANCHES_RESPONSE" | tail -n1)
BRANCHES_BODY=$(echo "$BRANCHES_RESPONSE" | head -n-1)

if [[ "$BRANCHES_HTTP_CODE" == "200" ]]; then
    BRANCH_COUNT=$(echo "$BRANCHES_BODY" | jq 'length')
    DEFAULT_BRANCH=$(echo "$BRANCHES_BODY" | jq -r '.[0].name')
    print_success "Repository read access confirmed"
    print_info "Found $BRANCH_COUNT branches"
    print_info "Default branch: $DEFAULT_BRANCH"
else
    print_error "Repository read access failed (HTTP $BRANCHES_HTTP_CODE)"
fi

# Summary
print_header "Test Results Summary"

echo -e "${GREEN}All critical tests passed!${NC}"
echo ""
echo -e "${YELLOW}Bot capabilities verified:${NC}"
echo "  ✓ Authentication via API token"
echo "  ✓ Repository access"
echo "  ✓ Issue reading and listing"
echo "  ✓ Issue assignment"
echo "  ✓ Issue commenting"
echo "  ✓ Label management"
echo "  ✓ Repository permissions"
echo ""
echo -e "${BLUE}Next steps:${NC}"
echo "1. Review the coordinator bot documentation:"
echo "   docs/1-getting-started/3-configuration/4-gitea-coordinator.md"
echo ""
echo "2. Configure coordinator webhook (see Issue #157)"
echo ""
echo "3. Deploy coordinator service (see Issue #158)"
