#!/bin/bash
# Test script for TypeDialog Agents

set -e

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

echo -e "${CYAN}=== TypeDialog Agent System Tests ===${NC}\n"

# Test 1: Validate all agents
echo -e "${YELLOW}Test 1: Validating all agents...${NC}"
for agent in agents/*.agent.mdx; do
    echo -n "  Validating $(basename "$agent")... "
    if cargo run --quiet --package typedialog-ag -- validate "$agent" > /dev/null 2>&1; then
        echo -e "${GREEN}✓${NC}"
    else
        echo -e "${RED}✗${NC}"
        exit 1
    fi
done
echo ""

# Test 2: Transpile agents
echo -e "${YELLOW}Test 2: Transpiling agents...${NC}"
echo -n "  Transpiling greeting.agent.mdx... "
OUTPUT=$(cargo run --quiet --package typedialog-ag -- transpile agents/greeting.agent.mdx 2>/dev/null)
if echo "$OUTPUT" | grep -q "config ="; then
    echo -e "${GREEN}✓${NC}"
else
    echo -e "${RED}✗${NC}"
    exit 1
fi
echo ""

# Test 3: Cache stats
echo -e "${YELLOW}Test 3: Cache functionality...${NC}"
echo -n "  Getting cache stats... "
if cargo run --quiet --package typedialog-ag -- cache stats > /dev/null 2>&1; then
    echo -e "${GREEN}✓${NC}"
else
    echo -e "${RED}✗${NC}"
    exit 1
fi
echo ""

# Test 4: HTTP Server
echo -e "${YELLOW}Test 4: HTTP Server endpoints...${NC}"

# Start server in background
cargo run --quiet --release --package typedialog-ag -- serve --port 8771 > /tmp/test-server.log 2>&1 &
SERVER_PID=$!
sleep 3

# Health check
echo -n "  GET /health... "
RESPONSE=$(curl -s http://127.0.0.1:8771/health)
if [ "$RESPONSE" = "OK" ]; then
    echo -e "${GREEN}✓${NC}"
else
    echo -e "${RED}✗${NC}"
    kill $SERVER_PID 2>/dev/null
    exit 1
fi

# Validate endpoint
echo -n "  POST /validate... "
RESPONSE=$(curl -s -X POST http://127.0.0.1:8771/validate \
    -H "Content-Type: application/json" \
    -d '{"agent_file":"agents/greeting.agent.mdx"}')
if echo "$RESPONSE" | jq -e '.valid == true' > /dev/null 2>&1; then
    echo -e "${GREEN}✓${NC}"
else
    echo -e "${RED}✗${NC}"
    kill $SERVER_PID 2>/dev/null
    exit 1
fi

# Transpile endpoint
echo -n "  POST /transpile... "
RESPONSE=$(curl -s -X POST http://127.0.0.1:8771/transpile \
    -H "Content-Type: application/json" \
    -d '{"content":"---\n@agent { role: test, llm: gpt-4 }\n---\nTest"}')
if echo "$RESPONSE" | jq -e '.nickel_code' > /dev/null 2>&1; then
    echo -e "${GREEN}✓${NC}"
else
    echo -e "${RED}✗${NC}"
    kill $SERVER_PID 2>/dev/null
    exit 1
fi

# Stop server
kill $SERVER_PID 2>/dev/null
wait $SERVER_PID 2>/dev/null || true
echo ""

echo -e "${GREEN}=== All tests passed! ===${NC}\n"

# Summary
echo -e "${CYAN}Available agents:${NC}"
find agents -name "*.agent.mdx" -type f | while read -r agent; do
    NAME=$(basename "$agent" .agent.mdx)
    echo "  - $NAME"
done
echo ""

echo -e "${CYAN}Usage examples:${NC}"
echo "  # Validate an agent"
echo "  typedialog-ag validate agents/greeting.agent.mdx"
echo ""
echo "  # Transpile to Nickel"
echo "  typedialog-ag transpile agents/architect.agent.mdx"
echo ""
echo "  # Start HTTP server"
echo "  typedialog-ag serve --port 8765"
echo ""
echo "  # Execute via HTTP"
echo "  curl -X POST http://localhost:8765/agents/greeting/execute \\"
echo "    -H \"Content-Type: application/json\" \\"
echo "    -d '{\"name\":\"World\"}'"
echo ""

echo -e "${GREEN}Ready to use!${NC}"
