#!/bin/bash
# Phase 1: Critical Path Smoke Tests
# Tests 20 critical API endpoints for backward compatibility
# Date: 2026-01-20

API_BASE="http://localhost:8100/api"
RESULTS_FILE="/tmp/phase1_test_results.txt"
PASSED=0
FAILED=0

echo "=== Phase 1: Critical Path Smoke Tests ===" > $RESULTS_FILE
echo "Date: $(date)" >> $RESULTS_FILE
echo "API Base: $API_BASE" >> $RESULTS_FILE
echo "" >> $RESULTS_FILE

# Test function
test_endpoint() {
    local method=$1
    local endpoint=$2
    local expected_status=$3
    local description=$4
    local data=$5

    echo -n "Testing $method $endpoint ... "

    if [ "$method" == "GET" ]; then
        response=$(curl -s -w "\n%{http_code}" "$API_BASE$endpoint")
    elif [ "$method" == "POST" ]; then
        response=$(curl -s -w "\n%{http_code}" -X POST -H "Content-Type: application/json" -d "$data" "$API_BASE$endpoint")
    fi

    status=$(echo "$response" | tail -n 1)
    body=$(echo "$response" | sed '$d')

    if [ "$status" == "$expected_status" ]; then
        echo "✅ PASS"
        echo "✅ PASS: $method $endpoint - $description (Status: $status)" >> $RESULTS_FILE
        ((PASSED++))
    else
        echo "❌ FAIL (Expected: $expected_status, Got: $status)"
        echo "❌ FAIL: $method $endpoint - $description" >> $RESULTS_FILE
        echo "   Expected: $expected_status, Got: $status" >> $RESULTS_FILE
        echo "   Response: $(echo "$body" | head -c 200)" >> $RESULTS_FILE
        ((FAILED++))
    fi
}

echo "=== 1. Events API (6 endpoints) ==="

test_endpoint "GET" "/events" "200" "Events list endpoint"
test_endpoint "GET" "/events?filter=past" "200" "Events list with past filter"
test_endpoint "GET" "/events?featured=true" "200" "Events list with featured filter"
test_endpoint "GET" "/events/slug/mohamed-abdo-new-years-celebration-2025" "200" "Event detail by slug"
test_endpoint "GET" "/events/mohamed-abdo-new-years-celebration-2025/venue" "200" "Venue template by slug"
test_endpoint "GET" "/events/mohamed-abdo-new-years-celebration-2025/tiers" "200" "Event pricing tiers"

echo ""
echo "=== 2. Booking Flow (6 endpoints) ==="

# Note: These will fail with 422/401 without valid data/auth, but we're testing they exist
test_endpoint "POST" "/seats/reserve" "422" "Reserve seats (expects validation error)" '{"event_id":1}'
test_endpoint "POST" "/seats/release" "422" "Release seats (expects validation error)" '{"reservation_id":1}'
test_endpoint "POST" "/bookings" "422" "Create booking (expects validation error)" '{"event_id":1}'
test_endpoint "GET" "/bookings/1" "404" "Booking detail (expects not found)"
test_endpoint "POST" "/orders" "422" "Create order (expects validation error)" '{"booking_id":1}'
test_endpoint "GET" "/orders/1" "404" "Order detail (expects not found)"

echo ""
echo "=== 3. Authentication (4 endpoints) ==="

test_endpoint "POST" "/auth/login" "422" "Customer login (expects validation error)" '{"email":"test"}'
test_endpoint "POST" "/auth/register" "422" "Customer registration (expects validation error)" '{"email":"test"}'
test_endpoint "POST" "/admin/login" "422" "Admin login (expects validation error)" '{"email":"test"}'
test_endpoint "GET" "/customer/profile" "401" "Customer profile (expects unauthorized)"

echo ""
echo "=== 4. Admin Operations (4 endpoints) ==="

test_endpoint "GET" "/admin/events" "401" "Admin event list (expects unauthorized)"
test_endpoint "POST" "/admin/events" "401" "Create event (expects unauthorized)" '{}'
test_endpoint "GET" "/admin/events/1" "401" "Event detail (expects unauthorized)"
test_endpoint "GET" "/admin/orders" "401" "Admin orders (expects unauthorized)"

echo ""
echo "=========================="
echo "Results: $PASSED passed, $FAILED failed"
echo "" >> $RESULTS_FILE
echo "=========================="  >> $RESULTS_FILE
echo "SUMMARY: $PASSED passed, $FAILED failed" >> $RESULTS_FILE

cat $RESULTS_FILE

if [ $FAILED -gt 0 ]; then
    exit 1
fi
