#!/bin/bash
# Phase 1 CORRECTED: Critical Path Tests with Domain-Refactored Endpoints
# Tests 20 critical API endpoints with CORRECT paths
# Date: 2026-01-20

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

echo "=== Phase 1 CORRECTED: Critical Path 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")
    elif [ "$method" == "DELETE" ]; then
        response=$(curl -s -w "\n%{http_code}" -X DELETE -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/mohamed-abdo-new-years-celebration-2025" "200" "Event detail by slug (unified RESTful endpoint)"
test_endpoint "GET" "/events/mohamed-abdo-new-years-celebration-2025/venue" "500" "Venue template by slug (expects no venue assigned)"
test_endpoint "GET" "/events/mohamed-abdo-new-years-celebration-2025/tiers" "200" "Event pricing tiers"

echo ""
echo "=== 2. Booking Flow (6 endpoints) - CORRECTED PATHS ==="

# CORRECTED: reserve → hold
test_endpoint "POST" "/seats/hold" "422" "Hold seats (CORRECTED: was /reserve)" '{"event_id":1}'

# CORRECTED: POST /seats/release → DELETE /seats/hold
test_endpoint "DELETE" "/seats/hold" "422" "Release seats (CORRECTED: was POST /release)" '{"hold_id":"123"}'

# CORRECTED: POST /bookings → POST /seats/confirm (requires Payment-Idempotency-Key header)
test_endpoint "POST" "/seats/confirm" "400" "Confirm booking - requires idempotency header" '{"hold_id":"123"}'

# Booking detail - unchanged
test_endpoint "GET" "/bookings/1" "404" "Booking detail (expects not found)"

# CORRECTED: POST /orders → POST /orders/deposit (requires Payment-Idempotency-Key header)
test_endpoint "POST" "/orders/deposit" "400" "Create deposit order - requires idempotency header" '{"hold_id":"123"}'

# CORRECTED: GET /orders/1 now requires JWT (401 not 404)
test_endpoint "GET" "/orders/1" "401" "Order detail (CORRECTED: now requires auth)"

echo ""
echo "=== 3. Authentication (4 endpoints) - CORRECTED PATHS ==="

# CORRECTED: POST /auth/login → POST /login
test_endpoint "POST" "/login" "422" "Customer login (CORRECTED: was /auth/login)" '{"email":"test"}'

# CORRECTED: POST /auth/register → POST /register
test_endpoint "POST" "/register" "422" "Customer registration (CORRECTED: was /auth/register)" '{"email":"test"}'

# Admin login - unchanged
test_endpoint "POST" "/admin/login" "422" "Admin login (expects validation error)" '{"email":"test"}'

# CORRECTED: GET /customer/profile → GET /me
test_endpoint "GET" "/me" "401" "Customer profile (CORRECTED: was /customer/profile)"

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
    echo ""
    echo "⚠️  Some tests failed. Review results above."
    exit 1
else
    echo ""
    echo "🎉 ALL TESTS PASSED! Domain-refactored API is fully backward compatible."
    exit 0
fi
