#!/bin/bash

# Test script for Server Recommendation System
# Usage: ./test.sh [worker-url]

WORKER_URL="${1:-http://localhost:8787}"

echo "Testing Server Recommendation System"
echo "Worker URL: $WORKER_URL"
echo ""

# Test 1: Health Check
echo "1. Testing Health Check..."
curl -s "$WORKER_URL/api/health" | jq '.'
echo ""

# Test 2: Get All Servers
echo "2. Testing Get All Servers..."
curl -s "$WORKER_URL/api/servers" | jq '{count: .count, servers: .servers[0:2]}'
echo ""

# Test 3: Get Servers with Filters
echo "3. Testing Get Servers with Filters (minCpu=4, minMemory=8)..."
curl -s "$WORKER_URL/api/servers?minCpu=4&minMemory=8" | jq '{count: .count, filters: .filters}'
echo ""

# Test 4: Get Servers by Provider
echo "4. Testing Get Servers by Provider (aws)..."
curl -s "$WORKER_URL/api/servers?provider=aws" | jq '{count: .count, filters: .filters}'
echo ""

# Test 5: Recommendation Request (Small)
echo "5. Testing Recommendation Request (Small server)..."
curl -s -X POST "$WORKER_URL/api/recommend" \
  -H "Content-Type: application/json" \
  -d '{
    "cpu_cores": 2,
    "memory_gb": 4,
    "storage_gb": 80
  }' | jq '{total_candidates: .total_candidates, cached: .cached, recommendation_count: (.recommendations | length), top_recommendation: .recommendations[0].server_name}'
echo ""

# Test 6: Recommendation Request (Medium)
echo "6. Testing Recommendation Request (Medium server with budget)..."
curl -s -X POST "$WORKER_URL/api/recommend" \
  -H "Content-Type: application/json" \
  -d '{
    "cpu_cores": 4,
    "memory_gb": 16,
    "storage_gb": 200,
    "budget_monthly": 150,
    "sla_requirement": 99.9
  }' | jq '{total_candidates: .total_candidates, cached: .cached, recommendations: [.recommendations[] | {server: .server_name, score: .score, price: .price_monthly}]}'
echo ""

# Test 7: Recommendation Request with Region Filter
echo "7. Testing Recommendation Request with Region Filter..."
curl -s -X POST "$WORKER_URL/api/recommend" \
  -H "Content-Type: application/json" \
  -d '{
    "cpu_cores": 4,
    "memory_gb": 8,
    "storage_gb": 100,
    "regions": ["us-east-1"]
  }' | jq '{total_candidates: .total_candidates, cached: .cached}'
echo ""

# Test 8: Cache Hit Test (same request as #5)
echo "8. Testing Cache Hit (same request as #5)..."
curl -s -X POST "$WORKER_URL/api/recommend" \
  -H "Content-Type: application/json" \
  -d '{
    "cpu_cores": 2,
    "memory_gb": 4,
    "storage_gb": 80
  }' | jq '{cached: .cached, top_recommendation: .recommendations[0].server_name}'
echo ""

# Test 9: Invalid Request (missing required field)
echo "9. Testing Invalid Request (missing cpu_cores)..."
curl -s -X POST "$WORKER_URL/api/recommend" \
  -H "Content-Type: application/json" \
  -d '{
    "memory_gb": 4,
    "storage_gb": 80
  }' | jq '.'
echo ""

# Test 10: Invalid Request (negative value)
echo "10. Testing Invalid Request (negative cpu_cores)..."
curl -s -X POST "$WORKER_URL/api/recommend" \
  -H "Content-Type: application/json" \
  -d '{
    "cpu_cores": -1,
    "memory_gb": 4,
    "storage_gb": 80
  }' | jq '.'
echo ""

echo "Test suite completed!"
