#!/bin/bash

# Endpoint Health Check Script
# Tests all critical API endpoints to ensure they work after refactor

echo "🧪 Testing Vibn API Endpoints"
echo "======================================"
echo ""

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

# Base URL
BASE_URL="http://localhost:3000"

# Test counter
TOTAL=0
PASSED=0
FAILED=0

# Helper function to test an endpoint
test_endpoint() {
  local METHOD=$1
  local PATH=$2
  local EXPECTED_STATUS=$3
  local DESCRIPTION=$4
  local AUTH=${5:-""}
  
  TOTAL=$((TOTAL + 1))
  
  if [ -z "$AUTH" ]; then
    STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X $METHOD "$BASE_URL$PATH" 2>/dev/null)
  else
    STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X $METHOD -H "Authorization: Bearer $AUTH" "$BASE_URL$PATH" 2>/dev/null)
  fi
  
  if [ "$STATUS" == "$EXPECTED_STATUS" ]; then
    echo -e "${GREEN}✓${NC} $DESCRIPTION"
    echo "  └─ $METHOD $PATH → $STATUS"
    PASSED=$((PASSED + 1))
  else
    echo -e "${RED}✗${NC} $DESCRIPTION"
    echo "  └─ $METHOD $PATH → Expected $EXPECTED_STATUS, got $STATUS"
    FAILED=$((FAILED + 1))
  fi
}

echo "1️⃣  Frontend Pages"
echo "-----------------------------------"
test_endpoint "GET" "/" "200" "Home page"
echo ""

echo "2️⃣  Project APIs (No Auth Required for Testing Structure)"
echo "-----------------------------------"
test_endpoint "POST" "/api/projects/create" "401" "Create project (should require auth)"
test_endpoint "GET" "/api/projects/phase" "405" "Phase endpoint (POST only)"
test_endpoint "GET" "/api/debug/first-project" "200" "Debug: First project"
echo ""

echo "3️⃣  Knowledge & Context APIs"
echo "-----------------------------------"
test_endpoint "POST" "/api/projects/test-project/knowledge/upload-document" "401" "Upload document (should require auth)"
test_endpoint "POST" "/api/projects/test-project/knowledge/import-document" "401" "Import document (should require auth)"
test_endpoint "POST" "/api/projects/test-project/knowledge/import-ai-chat" "401" "Import AI chat (should require auth)"
test_endpoint "POST" "/api/projects/test-project/knowledge/batch-extract" "401" "Batch extract (should require auth)"
test_endpoint "GET" "/api/debug/knowledge-items" "200" "Debug: Knowledge items"
test_endpoint "GET" "/api/debug/context-sources" "200" "Debug: Context sources"
echo ""

echo "4️⃣  AI Chat APIs"
echo "-----------------------------------"
test_endpoint "POST" "/api/ai/chat" "401" "AI chat (should require auth)"
test_endpoint "GET" "/api/ai/conversation" "400" "Get conversation (requires projectId)"
test_endpoint "POST" "/api/ai/conversation/reset" "400" "Reset conversation (requires projectId)"
echo ""

echo "5️⃣  Extraction APIs"
echo "-----------------------------------"
test_endpoint "POST" "/api/projects/test-project/extract-from-chat" "401" "Extract from chat (should require auth)"
test_endpoint "POST" "/api/projects/test-project/aggregate" "401" "Aggregate extractions (should require auth)"
echo ""

echo "6️⃣  GitHub Integration APIs"
echo "-----------------------------------"
test_endpoint "GET" "/api/github/repos" "401" "Get GitHub repos (should require auth)"
test_endpoint "POST" "/api/github/connect" "401" "Connect GitHub (should require auth)"
test_endpoint "GET" "/api/github/repo-tree" "400" "Get repo tree (requires params)"
test_endpoint "GET" "/api/github/file-content" "400" "Get file content (requires params)"
echo ""

echo "7️⃣  Planning & Vision APIs"
echo "-----------------------------------"
test_endpoint "POST" "/api/projects/test-project/plan/mvp" "401" "Generate MVP plan (should require auth)"
test_endpoint "POST" "/api/projects/test-project/plan/marketing" "401" "Generate marketing plan (should require auth)"
test_endpoint "POST" "/api/vision/update" "400" "Update vision (requires projectId)"
echo ""

echo "8️⃣  Utility APIs"
echo "-----------------------------------"
test_endpoint "POST" "/api/context/summarize" "400" "Summarize context (requires body)"
test_endpoint "GET" "/api/debug/env" "200" "Debug: Environment check"
test_endpoint "GET" "/api/diagnose" "200" "Diagnose system"
echo ""

echo ""
echo "======================================"
echo "📊 Test Results"
echo "======================================"
echo -e "Total:  $TOTAL"
echo -e "${GREEN}Passed: $PASSED${NC}"
echo -e "${RED}Failed: $FAILED${NC}"
echo ""

if [ $FAILED -eq 0 ]; then
  echo -e "${GREEN}✅ All endpoint structure tests passed!${NC}"
  echo ""
  echo "Note: 401/400 responses are EXPECTED for auth-protected and"
  echo "parameter-required endpoints. This confirms they exist and"
  echo "are properly configured."
  exit 0
else
  echo -e "${RED}❌ Some endpoints failed. Check the output above.${NC}"
  exit 1
fi

