#!/bin/bash
# SmartStack Health Check Script
#
# Usage: ./scripts/health-check.sh
#
# Exit codes:
#   0 - All checks passed
#   1 - Some checks failed
#
# This script can be used in CI/CD pipelines to verify SmartStack setup.

set -e

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

ERRORS=0
WARNINGS=0

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

log_warn() {
    echo -e "${YELLOW}⚠${NC} $1"
    ((WARNINGS++))
}

log_error() {
    echo -e "${RED}✗${NC} $1"
    ((ERRORS++))
}

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

echo ""
echo "╔══════════════════════════════════════════════╗"
echo "║      SmartStack Health Check                 ║"
echo "╚══════════════════════════════════════════════╝"
echo ""

# Check Node.js
echo "Checking dependencies..."

if command -v node &> /dev/null; then
    NODE_VERSION=$(node --version)
    NODE_MAJOR=$(echo "$NODE_VERSION" | cut -d'.' -f1 | tr -d 'v')
    if [ "$NODE_MAJOR" -ge 18 ]; then
        log_ok "Node.js $NODE_VERSION"
    else
        log_error "Node.js $NODE_VERSION (requires 18+)"
    fi
else
    log_error "Node.js not found"
fi

# Check npm
if command -v npm &> /dev/null; then
    log_ok "npm $(npm --version)"
else
    log_error "npm not found"
fi

# Check Git
if command -v git &> /dev/null; then
    log_ok "Git $(git --version | cut -d' ' -f3)"
else
    log_error "Git not found"
fi

# Check .NET SDK
if command -v dotnet &> /dev/null; then
    log_ok ".NET SDK $(dotnet --version)"
else
    log_warn ".NET SDK not found (optional for non-.NET projects)"
fi

echo ""
echo "Checking Claude Code..."

# Check Claude Code CLI
if command -v claude &> /dev/null; then
    log_ok "Claude Code CLI installed"

    # Check MCP servers
    echo ""
    echo "Checking MCP servers..."

    if claude mcp list 2>/dev/null | grep -qi "smartstack"; then
        log_ok "SmartStack MCP available"
    else
        log_error "SmartStack MCP not installed"
        log_info "  Install: npm install -g @atlashub/smartstack-mcp && claude mcp add smartstack -- npx @atlashub/smartstack-mcp"
    fi

    if claude mcp list 2>/dev/null | grep -qi "context7"; then
        log_ok "Context7 MCP available"
    else
        log_error "Context7 MCP not installed"
        log_info "  Install: claude mcp add context7 -- npx @upstash/context7-mcp"
    fi
else
    log_error "Claude Code CLI not found"
    log_info "  Install: curl -fsSL https://claude.ai/install.sh | bash"
    log_info "  More info: https://code.claude.com/docs/en/setup"
fi

echo ""
echo "Checking SmartStack CLI..."

# Check SmartStack CLI
if command -v smartstack &> /dev/null; then
    log_ok "SmartStack CLI installed"

    # Check templates installation
    if [ -d "$HOME/.claude/commands" ] && [ -f "$HOME/.claude/commands/gitflow.md" ]; then
        log_ok "SmartStack commands installed"
    else
        log_warn "SmartStack commands not installed"
        log_info "  Run: smartstack install"
    fi
else
    log_error "SmartStack CLI not found"
    log_info "  Install: npm install -g @atlashub/smartstack-cli"
fi

# Check local Ralph config
echo ""
echo "Checking local project..."

if [ -f ".ralph/ralph.config.yaml" ]; then
    log_ok "Ralph configured in this project"
else
    log_warn "Ralph not configured in this project"
    log_info "  Run: smartstack ralph init"
fi

if [ -d ".git" ]; then
    BRANCH=$(git branch --show-current 2>/dev/null || echo "unknown")
    log_ok "Git repository (branch: $BRANCH)"
else
    log_warn "Not a Git repository"
fi

# Summary
echo ""
echo "════════════════════════════════════════════════"

if [ $ERRORS -eq 0 ]; then
    if [ $WARNINGS -eq 0 ]; then
        echo -e "${GREEN}All checks passed!${NC}"
        echo "SmartStack is ready to use."
    else
        echo -e "${YELLOW}$WARNINGS warning(s)${NC}"
        echo "SmartStack is usable but some features may be limited."
    fi
    exit 0
else
    echo -e "${RED}$ERRORS error(s), $WARNINGS warning(s)${NC}"
    echo "Please fix the errors above before using SmartStack."
    exit 1
fi
