#!/bin/bash

# Event Filtering QA Test Suite
# Run comprehensive tests for event filtering system

API_URL="http://localhost:8000/api/events"

echo "========================================"
echo "EVENT FILTERING QA TEST SUITE"
echo "========================================"
echo ""

# Test Group 1: Test Event Exclusion
echo "=== TEST GROUP 1: Test Event Exclusion ==="
echo ""

echo "Test 1.1: filter=all - should exclude test events"
curl -s "${API_URL}?filter=all&per_page=100" | jq -c '{total: (.data | length), test_count: ([.data[] | select(.slug | startswith("test-"))] | length), status: (([.data[] | select(.slug | startswith("test-"))] | length) == 0 ? "PASS" : "FAIL")}'
echo ""

echo "Test 1.2: filter=past - should exclude test archived events"
curl -s "${API_URL}?filter=past&per_page=100" | jq -c '{total: (.data | length), test_count: ([.data[] | select(.slug | startswith("test-"))] | length), status: (([.data[] | select(.slug | startswith("test-"))] | length) == 0 ? "PASS" : "FAIL")}'
echo ""

echo "Test 1.3: featured=true - should exclude test events"
curl -s "${API_URL}?featured=true" | jq -c '{total: (.data | length), test_count: ([.data[] | select(.slug | startswith("test-"))] | length), status: (([.data[] | select(.slug | startswith("test-"))] | length) == 0 ? "PASS" : "FAIL")}'
echo ""

# Test Group 2: Published Flag Filtering
echo "=== TEST GROUP 2: Published Flag Filtering ==="
echo ""

echo "Test 2.1: filter=all - all should be published"
curl -s "${API_URL}?filter=all&per_page=100" | jq -c '{total: (.data | length), unpublished_count: ([.data[] | select(.is_published == false)] | length), status: (([.data[] | select(.is_published == false)] | length) == 0 ? "PASS" : "FAIL")}'
echo ""

echo "Test 2.2: filter=past - all should be published"
curl -s "${API_URL}?filter=past&per_page=100" | jq -c '{total: (.data | length), unpublished_count: ([.data[] | select(.is_published == false)] | length), status: (([.data[] | select(.is_published == false)] | length) == 0 ? "PASS" : "FAIL")}'
echo ""

# Test Group 3: Featured Flag Filtering
echo "=== TEST GROUP 3: Featured Flag Filtering ==="
echo ""

echo "Test 3.1: featured=true (no filter) - should exclude archived events"
curl -s "${API_URL}?featured=true" | jq -c '{total: (.data | length), archived_count: ([.data[] | select(.is_archived == true)] | length), status: (([.data[] | select(.is_archived == true)] | length) == 0 ? "PASS" : "FAIL")}'
echo ""

echo "Test 3.2: filter=past&featured=true - only featured archived"
curl -s "${API_URL}?filter=past&featured=true&per_page=100" | jq -c '{total: (.data | length), non_featured: ([.data[] | select(.is_featured == false)] | length), status: (([.data[] | select(.is_featured == false)] | length) == 0 ? "PASS" : "FAIL")}'
echo ""

echo "Test 3.3: filter=all&featured=true - current + archived featured"
curl -s "${API_URL}?filter=all&featured=true&per_page=100" | jq -c '{total: (.data | length), current: ([.data[] | select(.is_archived != true)] | length), archived: ([.data[] | select(.is_archived == true)] | length)}'
echo ""

# Test Group 4: Filter Parameter Behavior
echo "=== TEST GROUP 4: Filter Parameter Behavior ==="
echo ""

echo "Test 4.1: Default (no params) - should return all published events"
curl -s "${API_URL}?per_page=100" | jq -c '{total: (.data | length), current: ([.data[] | select(.is_archived != true)] | length), archived: ([.data[] | select(.is_archived == true)] | length)}'
echo ""

echo "Test 4.2: filter=upcoming - no archived events"
curl -s "${API_URL}?filter=upcoming&per_page=100" | jq -c '{total: (.data | length), archived_count: ([.data[] | select(.is_archived == true)] | length), status: (([.data[] | select(.is_archived == true)] | length) == 0 ? "PASS" : "FAIL")}'
echo ""

echo "Test 4.3: filter=past - only archived events"
curl -s "${API_URL}?filter=past&per_page=100" | jq -c '{total: (.data | length), all_archived: ([.data[] | select(.is_archived == true)] | length), status: ((.data | length) == ([.data[] | select(.is_archived == true)] | length) ? "PASS" : "FAIL")}'
echo ""

echo "========================================"
echo "QA TEST SUITE COMPLETE"
echo "========================================"
