#!/usr/bin/env bash
# =============================================================================
# Smartflow Integration Test Script
# =============================================================================
# Fill in the two variables below, then run:
#   chmod +x smartflow_integration_test.sh
#   ./smartflow_integration_test.sh
#
# Requirements: curl, python3 (for JSON pretty-printing)
# =============================================================================

SMARTFLOW_HOST="https://your-smartflow.example.com"   # No trailing slash
VIRTUAL_KEY="sk-sf-your-virtual-key"                  # Issued by your admin

# =============================================================================

RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
PASS=0; FAIL=0

run_test() {
    local name="$1"; local cmd="$2"; local expect="$3"
    printf "  %-40s" "$name"
    result=$(eval "$cmd" 2>&1)
    if echo "$result" | grep -qi "$expect"; then
        echo -e "${GREEN}PASS${NC}"
        ((PASS++))
    else
        echo -e "${RED}FAIL${NC}"
        echo "    Response: $(echo "$result" | head -3)"
        ((FAIL++))
    fi
}

echo ""
echo "Smartflow Integration Tests"
echo "Host: $SMARTFLOW_HOST"
echo "───────────────────────────────────────────────"

# ── Test 1: Health Check ─────────────────────────────────────────────────────
echo ""
echo "[ 1 ] Health & Connectivity"
run_test "GET /health" \
    "curl -sf --max-time 10 '$SMARTFLOW_HOST/health'" \
    "ok\|healthy\|running\|status"

# ── Test 2: OpenAI-compatible chat ───────────────────────────────────────────
echo ""
echo "[ 2 ] OpenAI-Compatible Endpoint (/v1/chat/completions)"
run_test "GPT-4o chat" \
    "curl -s --max-time 30 '$SMARTFLOW_HOST/v1/chat/completions' \
     -H 'Authorization: Bearer $VIRTUAL_KEY' \
     -H 'Content-Type: application/json' \
     -d '{\"model\":\"gpt-4o\",\"messages\":[{\"role\":\"user\",\"content\":\"Reply with one word: PASS\"}],\"max_tokens\":10}'" \
    "choices\|content"

run_test "Claude via OpenAI route" \
    "curl -s --max-time 30 '$SMARTFLOW_HOST/v1/chat/completions' \
     -H 'Authorization: Bearer $VIRTUAL_KEY' \
     -H 'Content-Type: application/json' \
     -d '{\"model\":\"claude-sonnet-4-6\",\"messages\":[{\"role\":\"user\",\"content\":\"Reply: PASS\"}],\"max_tokens\":10}'" \
    "choices\|content"

run_test "GET /v1/models" \
    "curl -s --max-time 10 '$SMARTFLOW_HOST/v1/models' \
     -H 'Authorization: Bearer $VIRTUAL_KEY'" \
    "data\|object"

# ── Test 3: Anthropic native messages ────────────────────────────────────────
echo ""
echo "[ 3 ] Anthropic Native Endpoint (/anthropic/v1/messages)"
run_test "Claude Sonnet 4.6 message" \
    "curl -s --max-time 30 '$SMARTFLOW_HOST/anthropic/v1/messages' \
     -H 'x-api-key: $VIRTUAL_KEY' \
     -H 'anthropic-version: 2023-06-01' \
     -H 'Content-Type: application/json' \
     -d '{\"model\":\"claude-sonnet-4-6\",\"max_tokens\":32,\"messages\":[{\"role\":\"user\",\"content\":\"Reply: PASS\"}]}'" \
    "content\|text"

# ── Test 4: Cache endpoints ───────────────────────────────────────────────────
echo ""
echo "[ 4 ] Cache & Stats"
run_test "GET /api/metacache/stats" \
    "curl -s --max-time 10 '$SMARTFLOW_HOST/api/metacache/stats' \
     -H 'Authorization: Bearer $VIRTUAL_KEY'" \
    "hit\|miss\|stats\|data\|cache"

run_test "Second request (expect cache activity)" \
    "curl -s --max-time 30 '$SMARTFLOW_HOST/v1/chat/completions' \
     -H 'Authorization: Bearer $VIRTUAL_KEY' \
     -H 'Content-Type: application/json' \
     -d '{\"model\":\"claude-sonnet-4-6\",\"messages\":[{\"role\":\"user\",\"content\":\"Reply: PASS\"}],\"max_tokens\":10}'" \
    "choices\|content"

# ── Test 5: Auth rejection ────────────────────────────────────────────────────
echo ""
echo "[ 5 ] Auth Validation"
run_test "Invalid key is rejected (401/403)" \
    "curl -s --max-time 10 '$SMARTFLOW_HOST/v1/chat/completions' \
     -H 'Authorization: Bearer invalid-key-should-fail' \
     -H 'Content-Type: application/json' \
     -d '{\"model\":\"gpt-4o\",\"messages\":[{\"role\":\"user\",\"content\":\"test\"}]}'" \
    "401\|403\|unauthorized\|forbidden\|invalid\|error"

# ── Summary ───────────────────────────────────────────────────────────────────
echo ""
echo "───────────────────────────────────────────────"
echo -e "Results: ${GREEN}$PASS passed${NC}  ${RED}$FAIL failed${NC}"
if [ $FAIL -eq 0 ]; then
    echo -e "${GREEN}All tests passed. Deployment looks healthy.${NC}"
else
    echo -e "${YELLOW}Some tests failed. Check host, virtual key, and deployed services.${NC}"
fi
echo ""
