#!/bin/bash

# BMAD Enhanced - Test Published Stable Version
# Downloads and tests the published stable version from NPM

set -e

# Load common validation functions
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/common-validation.sh"

# Colors for output (also defined in common-validation.sh)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

echo -e "${BLUE}🧪 BMAD Enhanced - Testing Published Stable${NC}"
echo "==========================================="
echo ""

# Get the latest stable version from NPM
echo -e "${YELLOW}📦 Checking latest stable version on NPM...${NC}"
STABLE_VERSION=$(npm view @cloudkinetix/bmad-enhanced version 2>/dev/null || echo "none")

if [ "$STABLE_VERSION" = "none" ]; then
    echo -e "${RED}❌ No stable version found on NPM${NC}"
    echo "Please publish a stable version first with: npm run publish:stable"
    exit 1
fi

echo -e "${GREEN}✅ Found stable version: $STABLE_VERSION${NC}"
echo ""

# Base directory for this script's tests
BASE_DIR="test-results/07-test-published-stable"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
TEST_INSTANCE="$BASE_DIR/$TIMESTAMP"

# Clean up and create directories
echo -e "${YELLOW}🧹 Setting up test directories...${NC}"
mkdir -p "$TEST_INSTANCE"
echo "Test results will be saved in: $TEST_INSTANCE/"

# Test 1: Default installation
echo -e "${BLUE}📋 Test 1: Default Installation${NC}"
echo "Testing: npx @cloudkinetix/bmad-enhanced install --default"
echo ""

TEST_DIR="$TEST_INSTANCE/stable-validation-default"
rm -rf "$TEST_DIR"
mkdir -p "$TEST_DIR"

if npx @cloudkinetix/bmad-enhanced install --default --directory "$TEST_DIR"; then
    echo -e "${GREEN}✅ Default installation successful${NC}"
    
    # Validate basic structure using common function
    if ! validate_basic_structure "$TEST_DIR"; then
        exit 1
    fi
else
    echo -e "${RED}❌ Default installation failed${NC}"
    exit 1
fi

echo ""

# Test 2: Full installation
echo -e "${BLUE}📋 Test 2: Full Installation${NC}"
echo "Testing: npx @cloudkinetix/bmad-enhanced install --full"
echo ""

TEST_DIR="$TEST_INSTANCE/stable-validation-full"
rm -rf "$TEST_DIR"
mkdir -p "$TEST_DIR"

if npx @cloudkinetix/bmad-enhanced install --full --directory "$TEST_DIR"; then
    echo -e "${GREEN}✅ Full installation successful${NC}"
    
    # Validate IDE installations using common function
    if ! validate_ide_installation "$TEST_DIR" 8; then
        exit 1
    fi
    
    # Check if KiloCode is installed (optional)
    check_kilocode_installation "$TEST_DIR"
else
    echo -e "${RED}❌ Full installation failed${NC}"
    exit 1
fi

echo ""

# Summary
echo -e "${BLUE}📊 Stable Testing Summary${NC}"
echo "========================"
echo -e "${GREEN}✅ Stable version $STABLE_VERSION tested successfully!${NC}"
echo ""
echo "Tests completed:"
echo "  ✓ Default installation"
echo "  ✓ Full installation with all IDEs"
echo ""
echo "The stable release is working correctly!"
echo "Users can install with: npx @cloudkinetix/bmad-enhanced install"