#!/usr/bin/env bash
################################################################################
# Claude Code Setup Verification Script
# Checks if all customizations are properly installed
################################################################################

set -e

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

CLAUDE_DIR="$HOME/.claude"
AGENTS_DIR="$CLAUDE_DIR/agents"
PLUGINS_DIR="$CLAUDE_DIR/plugins"

PASSED=0
FAILED=0
WARNINGS=0

check_pass() {
    echo -e "${GREEN}✓${NC} $1"
    PASSED=$((PASSED+1))
}

check_fail() {
    echo -e "${RED}✗${NC} $1"
    FAILED=$((FAILED+1))
}

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

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

echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║     Claude Code Customizations - Verification             ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}"
echo ""

# 1. Check directory structure
echo "═══════════════════════════════════════════════════════════"
echo "Directory Structure"
echo "═══════════════════════════════════════════════════════════"

[ -d "$CLAUDE_DIR" ] && check_pass "Claude directory exists" || check_fail "Claude directory missing"
[ -d "$AGENTS_DIR" ] && check_pass "Agents directory exists" || check_fail "Agents directory missing"
[ -d "$PLUGINS_DIR" ] && check_pass "Plugins directory exists" || check_fail "Plugins directory missing"

echo ""
echo "═══════════════════════════════════════════════════════════"
echo "Agent Categories"
echo "═══════════════════════════════════════════════════════════"

CATEGORIES=("engineering" "marketing" "product" "studio-operations" "project-management" "testing" "design" "bonus")
AGENT_COUNT=0

for category in "${CATEGORIES[@]}"; do
    if [ -d "$AGENTS_DIR/$category" ]; then
        count=$(ls -1 "$AGENTS_DIR/$category"/*.md 2>/dev/null | wc -l)
        if [ $count -gt 0 ]; then
            echo -e "${GREEN}✓${NC} $category: $count agents"
            AGENT_COUNT=$((AGENT_COUNT + count))
        else
            check_warn "$category: directory exists but no agents"
        fi
    else
        check_fail "$category: directory missing"
    fi
done

echo ""
check_info "Total agents: $AGENT_COUNT"

# 2. Check configuration files
echo ""
echo "═══════════════════════════════════════════════════════════"
echo "Configuration Files"
echo "═══════════════════════════════════════════════════════════"

[ -f "$CLAUDE_DIR/settings.json" ] && check_pass "settings.json exists" || check_fail "settings.json missing"
[ -f "$CLAUDE_DIR/settings.local.json" ] && check_pass "settings.local.json exists" || check_fail "settings.local.json missing"
[ -f "$PLUGINS_DIR/installed_plugins.json" ] && check_pass "installed_plugins.json exists" || check_fail "installed_plugins.json missing"
[ -f "$PLUGINS_DIR/known_marketplaces.json" ] && check_pass "known_marketplaces.json exists" || check_fail "known_marketplaces.json missing"

# 3. Check MCP tools
echo ""
echo "═══════════════════════════════════════════════════════════"
echo "MCP Tools"
echo "═══════════════════════════════════════════════════════════"

if command -v npx &> /dev/null; then
    check_pass "npx available"

    # Check if @z_ai/mcp-server can be accessed
    if npx -y @z_ai/mcp-server --help &> /dev/null; then
        check_pass "@z_ai/mcp-server accessible"
    else
        check_warn "@z_ai/mcp-server not directly accessible (may download on first use)"
    fi

    # Check if @z_ai/coding-helper can be accessed
    if npx -y @z_ai/coding-helper --help &> /dev/null; then
        check_pass "@z_ai/coding-helper accessible"
    else
        check_warn "@z_ai/coding-helper not directly accessible (may download on first use)"
    fi
else
    check_fail "npx not available - MCP tools may not work"
fi

# 4. Check plugins
echo ""
echo "═══════════════════════════════════════════════════════════"
echo "Plugins"
echo "═══════════════════════════════════════════════════════════"

if [ -f "$PLUGINS_DIR/installed_plugins.json" ]; then
    # Check if GLM plugins are registered
    if grep -q "glm-plan-bug" "$PLUGINS_DIR/installed_plugins.json" 2>/dev/null; then
        check_pass "glm-plan-bug plugin registered"
    else
        check_warn "glm-plan-bug plugin not registered"
    fi

    if grep -q "glm-plan-usage" "$PLUGINS_DIR/installed_plugins.json" 2>/dev/null; then
        check_pass "glm-plan-usage plugin registered"
    else
        check_warn "glm-plan-usage plugin not registered"
    fi
fi

# 5. Sample agent check
echo ""
echo "═══════════════════════════════════════════════════════════"
echo "Agent Content Verification"
echo "═══════════════════════════════════════════════════════════"

CRITICAL_AGENTS=(
    "engineering/test-writer-fixer.md"
    "engineering/frontend-developer.md"
    "marketing/tiktok-strategist.md"
    "product/sprint-prioritizer.md"
    "studio-operations/studio-producer.md"
    "project-management/project-shipper.md"
    "design/whimsy-injector.md"
)

for agent in "${CRITICAL_AGENTS[@]}"; do
    if [ -f "$AGENTS_DIR/$agent" ]; then
        # Check file has content
        if [ -s "$AGENTS_DIR/$agent" ]; then
            check_pass "$agent exists and has content"
        else
            check_warn "$agent exists but is empty"
        fi
    else
        check_warn "$agent missing"
    fi
done

# 6. Settings validation
echo ""
echo "═══════════════════════════════════════════════════════════"
echo "Settings Validation"
echo "═══════════════════════════════════════════════════════════"

if [ -f "$CLAUDE_DIR/settings.json" ]; then
    # Check if JSON is valid
    if python3 -m json.tool "$CLAUDE_DIR/settings.json" &> /dev/null; then
        check_pass "settings.json is valid JSON"

        # Check for API token placeholder
        if grep -q "YOUR_API_TOKEN_HERE\|YOUR_TOKEN_HERE" "$CLAUDE_DIR/settings.json" 2>/dev/null; then
            check_warn "API token not configured (still placeholder)"
        else
            if grep -q "ANTHROPIC_AUTH_TOKEN" "$CLAUDE_DIR/settings.json" 2>/dev/null; then
                check_pass "ANTHROPIC_AUTH_TOKEN is set"
            fi
        fi
    else
        check_fail "settings.json is not valid JSON"
    fi
fi

# 7. Summary
echo ""
echo "═══════════════════════════════════════════════════════════"
echo "Summary"
echo "═══════════════════════════════════════════════════════════"

if [ $FAILED -eq 0 ]; then
    echo -e "${GREEN}✓ All critical checks passed!${NC}"
    echo ""
    echo "Passed:   $PASSED"
    echo "Warnings: $WARNINGS"
    echo "Failed:   $FAILED"
    echo ""
    echo -e "${GREEN}Your Claude Code setup is ready to use!${NC}"
    exit 0
else
    echo -e "${RED}✗ Some checks failed${NC}"
    echo ""
    echo "Passed:   $PASSED"
    echo "Warnings: $WARNINGS"
    echo "Failed:   $FAILED"
    echo ""
    echo "Please fix the failed checks above."
    exit 1
fi
