#!/bin/bash

# Test Upstream BMAD Installation
# This script tests what pure upstream BMAD creates without any CK modifications
# This helps us understand what we need to enhance/modify

set -e  # Exit on error

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

# Base directory for this script's tests
BASE_DIR="test-results/00-test-upstream-bmad"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
TEST_DIR="$BASE_DIR/$TIMESTAMP"

echo -e "${BLUE}🧪 Testing Pure Upstream BMAD Installation${NC}"
echo "This shows what upstream BMAD creates without CK enhancements"
echo "Test directory: $TEST_DIR"
echo ""

# Clean up and create test directory
rm -rf "$BASE_DIR"
mkdir -p "$TEST_DIR"

# Step 1: Install upstream BMAD directly using npx
echo -e "${YELLOW}📦 Installing upstream BMAD with Claude and Cursor IDEs...${NC}"
echo "Command: npx bmad-method@latest install --ide claude-code --ide cursor --directory $TEST_DIR --full"
echo "Note: Using --full flag for non-interactive installation"
echo ""

# Check if we can use npx (will fail in local dev)
if command -v npx &> /dev/null; then
    # Use --full flag to avoid prompts
    if npx bmad-method@latest install --ide claude-code --ide cursor --directory "$TEST_DIR" --full 2>&1; then
        echo -e "${GREEN}✅ Upstream BMAD installed successfully${NC}"
    else
        echo -e "${RED}❌ Upstream BMAD installation failed${NC}"
        echo -e "${YELLOW}Note: This is expected in local development. Run after publishing to NPM.${NC}"
        exit 0
    fi
else
    echo -e "${RED}❌ npx not found${NC}"
    exit 1
fi

echo ""
echo -e "${YELLOW}🔍 Analyzing upstream BMAD installation...${NC}"

# Step 2: Document what was created
echo -e "\n${CYAN}📂 Directory Structure:${NC}"
tree -L 2 "$TEST_DIR" 2>/dev/null || ls -la "$TEST_DIR"

# Step 3: Check for legacy files
echo -e "\n${CYAN}🔎 Checking for legacy rule files:${NC}"
LEGACY_FILES=(".cursorrules" ".windsurfrules" ".clinerules" ".geminirules")
for file in "${LEGACY_FILES[@]}"; do
    if [ -f "$TEST_DIR/$file" ]; then
        echo -e "${YELLOW}  ⚠️  Found legacy file: $file${NC}"
        echo "     Content preview: $(head -n 1 "$TEST_DIR/$file" 2>/dev/null || echo 'empty')"
    else
        echo -e "${GREEN}  ✓ No $file found (good!)${NC}"
    fi
done

# Step 4: Check IDE directory structures
echo -e "\n${CYAN}📁 IDE Directory Structures:${NC}"
IDE_DIRS=(".cursor" ".claude" ".windsurf" ".roo" ".cline" ".clinerules" ".trae" ".gemini" ".github" ".vscode" ".kilocodemodes")
for dir in "${IDE_DIRS[@]}"; do
    if [ -d "$TEST_DIR/$dir" ]; then
        echo -e "${GREEN}  ✓ Found $dir directory${NC}"
        # Show subdirectory structure
        if [ "$(ls -A "$TEST_DIR/$dir" 2>/dev/null)" ]; then
            echo "     Contents: $(ls "$TEST_DIR/$dir" | head -3 | tr '\n' ' ')..."
        fi
    elif [ -f "$TEST_DIR/$dir" ]; then
        echo -e "${YELLOW}  ⚠️  $dir is a file (not directory)${NC}"
    else
        echo -e "${BLUE}  - $dir not found${NC}"
    fi
done

# Step 5: Check .roomodes specifically
echo -e "\n${CYAN}🔍 Checking .roomodes configuration:${NC}"
if [ -f "$TEST_DIR/.roomodes" ]; then
    echo -e "${GREEN}✓ .roomodes is a file${NC}"
    echo "  File size: $(wc -c < "$TEST_DIR/.roomodes") bytes"
    echo "  First few lines:"
    head -5 "$TEST_DIR/.roomodes" | sed 's/^/    /'
elif [ -d "$TEST_DIR/.roomodes" ]; then
    echo -e "${YELLOW}⚠️  .roomodes is a directory${NC}"
    echo "  Contents: $(ls "$TEST_DIR/.roomodes" 2>/dev/null || echo 'empty')"
else
    echo -e "${BLUE}- .roomodes not found${NC}"
fi

# Step 6: Check expansion packs
echo -e "\n${CYAN}📦 Checking expansion packs:${NC}"
UPSTREAM_PACKS=("bmad-2d-phaser-game-dev" "bmad-2d-unity-game-dev" "bmad-infrastructure-devops" "bmad-creator-tools")
for pack in "${UPSTREAM_PACKS[@]}"; do
    if [ -d "$TEST_DIR/.$pack" ]; then
        echo -e "${GREEN}  ✓ Found .$pack${NC}"
    else
        echo -e "${BLUE}  - .$pack not found${NC}"
    fi
done

# Step 7: Document findings
echo -e "\n${CYAN}📝 Key Findings:${NC}"
echo "1. IDE configurations created by upstream:"
find "$TEST_DIR" -maxdepth 1 -name ".*" -type d | grep -E "(cursor|claude|windsurf|cline|trae|gemini|github|vscode)" | sed 's/^/   - /'

echo -e "\n2. Legacy files created by upstream:"
find "$TEST_DIR" -maxdepth 1 -name ".*rules" -type f | sed 's/^/   - /'

echo -e "\n3. Total file count:"
echo "   - Files: $(find "$TEST_DIR" -type f | wc -l)"
echo "   - Directories: $(find "$TEST_DIR" -type d | wc -l)"

# Step 8: Save detailed report
REPORT_FILE="$TEST_DIR/upstream-analysis.txt"
echo -e "\n${CYAN}💾 Saving detailed report to: $REPORT_FILE${NC}"
{
    echo "Upstream BMAD Installation Analysis"
    echo "==================================="
    echo "Date: $(date)"
    echo ""
    echo "Full directory tree:"
    tree "$TEST_DIR" 2>/dev/null || find "$TEST_DIR" -type f -o -type d | sort
} > "$REPORT_FILE"

echo -e "\n${GREEN}✅ Upstream BMAD analysis complete!${NC}"
echo -e "${BLUE}Test installation preserved at: $TEST_DIR${NC}"
echo -e "${BLUE}View detailed report: cat $REPORT_FILE${NC}"