#!/usr/bin/env bash
#
# AgentVibes BMAD PR Testing Command
# Quick command to test BMAD PRs with AgentVibes integration
#
# Usage:
#   npx agentvibes test-bmad-pr [PR_NUMBER]
#   npx agentvibes test-bmad-pr 934
#

set -e

# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# Default PR number
PR_NUMBER="${1:-934}"

clear

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "${CYAN}🎙️  AgentVibes BMAD PR Testing Tool${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo -e "${BLUE}Testing PR #${PR_NUMBER}${NC}"
echo ""
echo -e "${YELLOW}This will download and run the automated test script for${NC}"
echo -e "${YELLOW}BMAD's AgentVibes integration with multi-agent party mode.${NC}"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Create temp directory for the script
TEMP_DIR=$(mktemp -d)
SCRIPT_PATH="$TEMP_DIR/test-bmad-pr.sh"

echo -e "${BLUE}📥 Downloading test script...${NC}"

# Download the test script from the PR branch
SCRIPT_URL="https://raw.githubusercontent.com/paulpreibisch/BMAD-METHOD/feature/agentvibes-tts-integration/test-bmad-pr.sh"

if curl -fsSL "$SCRIPT_URL" -o "$SCRIPT_PATH"; then
    chmod +x "$SCRIPT_PATH"
    echo -e "${GREEN}✓ Test script downloaded${NC}"
    echo ""

    # Run the test script
    exec "$SCRIPT_PATH"
else
    echo -e "${YELLOW}⚠ Could not download test script from PR branch${NC}"
    echo ""
    echo "Trying alternative: Official BMAD repository..."
    echo ""

    # Fallback to official repo
    SCRIPT_URL="https://raw.githubusercontent.com/bmad-code-org/BMAD-METHOD/refs/pull/${PR_NUMBER}/merge/test-bmad-pr.sh"

    if curl -fsSL "$SCRIPT_URL" -o "$SCRIPT_PATH"; then
        chmod +x "$SCRIPT_PATH"
        echo -e "${GREEN}✓ Test script downloaded${NC}"
        echo ""
        exec "$SCRIPT_PATH"
    else
        echo -e "${YELLOW}⚠ Could not download test script${NC}"
        echo ""
        echo "Please try manual testing:"
        echo -e "${GREEN}git clone https://github.com/bmad-code-org/BMAD-METHOD.git${NC}"
        echo -e "${GREEN}cd BMAD-METHOD${NC}"
        echo -e "${GREEN}git fetch origin pull/${PR_NUMBER}/head:test-pr-${PR_NUMBER}${NC}"
        echo -e "${GREEN}git checkout test-pr-${PR_NUMBER}${NC}"
        echo ""
        exit 1
    fi
fi
