#!/bin/bash
# Test script for new SAG module endpoints: BE-003 and BE-004

BASE_URL="http://localhost:8001/api/v1/sag"

echo "========================================="
echo "Testing SAG Module New Endpoints"
echo "========================================="
echo ""

# ==============================================================================
# BE-003: Tag State Management
# ==============================================================================

echo "📋 BE-003: Testing Tag State Management"
echo "----------------------------------------"

# Test 1: Create a case with a tag
echo "1️⃣ Creating test case..."
CASE_RESPONSE=$(curl -s -X POST "$BASE_URL/cases" \
  -H "Content-Type: application/json" \
  -d '{
    "titel": "Test Case for Tag State",
    "beskrivelse": "Testing tag state transitions",
    "status": "open",
    "template_key": "standard"
  }')

CASE_ID=$(echo $CASE_RESPONSE | python3 -c "import sys, json; print(json.load(sys.stdin).get('id', ''))")
echo "   ✅ Case created with ID: $CASE_ID"

# Test 2: Add a tag to the case
echo ""
echo "2️⃣ Adding tag to case..."
TAG_RESPONSE=$(curl -s -X POST "$BASE_URL/cases/$CASE_ID/tags" \
  -H "Content-Type: application/json" \
  -d '{
    "tag_navn": "needs_review",
    "state": "open"
  }')

TAG_ID=$(echo $TAG_RESPONSE | python3 -c "import sys, json; print(json.load(sys.stdin).get('id', ''))")
echo "   ✅ Tag created with ID: $TAG_ID (state: open)"

# Test 3: Close the tag
echo ""
echo "3️⃣ Closing tag (state: open → closed)..."
curl -s -X PATCH "$BASE_URL/cases/$CASE_ID/tags/$TAG_ID/state" \
  -H "Content-Type: application/json" \
  -d '{"state": "closed"}' | python3 -m json.tool
echo ""

# Test 4: Reopen the tag
echo "4️⃣ Reopening tag (state: closed → open)..."
curl -s -X PATCH "$BASE_URL/cases/$CASE_ID/tags/$TAG_ID/state" \
  -H "Content-Type: application/json" \
  -d '{"state": "open"}' | python3 -m json.tool
echo ""

# Test 5: Invalid state
echo "5️⃣ Testing invalid state (should fail with 400)..."
curl -s -X PATCH "$BASE_URL/cases/$CASE_ID/tags/$TAG_ID/state" \
  -H "Content-Type: application/json" \
  -d '{"state": "invalid"}' | python3 -m json.tool
echo ""

# Test 6: Non-existent tag
echo "6️⃣ Testing non-existent tag (should fail with 404)..."
curl -s -X PATCH "$BASE_URL/cases/$CASE_ID/tags/99999/state" \
  -H "Content-Type: application/json" \
  -d '{"state": "closed"}' | python3 -m json.tool
echo ""

# ==============================================================================
# BE-004: Bulk Operations
# ==============================================================================

echo ""
echo "========================================="
echo "📦 BE-004: Testing Bulk Operations"
echo "----------------------------------------"

# Test 7: Create multiple cases for bulk operations
echo "1️⃣ Creating 3 test cases for bulk operations..."
CASE_IDS=()
for i in 1 2 3; do
  RESPONSE=$(curl -s -X POST "$BASE_URL/cases" \
    -H "Content-Type: application/json" \
    -d "{
      \"titel\": \"Bulk Test Case $i\",
      \"beskrivelse\": \"Case for bulk operations testing\",
      \"status\": \"open\",
      \"template_key\": \"standard\"
    }")
  BULK_CASE_ID=$(echo $RESPONSE | python3 -c "import sys, json; print(json.load(sys.stdin).get('id', ''))")
  CASE_IDS+=($BULK_CASE_ID)
  echo "   ✅ Created case ID: $BULK_CASE_ID"
done
echo ""

# Test 8: Bulk update status
echo "2️⃣ Bulk updating status to 'in_progress'..."
curl -s -X POST "$BASE_URL/cases/bulk" \
  -H "Content-Type: application/json" \
  -d "{
    \"case_ids\": [${CASE_IDS[0]}, ${CASE_IDS[1]}, ${CASE_IDS[2]}],
    \"action\": \"update_status\",
    \"params\": {
      \"status\": \"in_progress\"
    }
  }" | python3 -m json.tool
echo ""

# Test 9: Bulk add tag
echo "3️⃣ Bulk adding 'urgent' tag to all cases..."
curl -s -X POST "$BASE_URL/cases/bulk" \
  -H "Content-Type: application/json" \
  -d "{
    \"case_ids\": [${CASE_IDS[0]}, ${CASE_IDS[1]}, ${CASE_IDS[2]}],
    \"action\": \"add_tag\",
    \"params\": {
      \"tag_naam\": \"urgent\"
    }
  }" | python3 -m json.tool
echo ""

# Test 10: Bulk close all
echo "4️⃣ Bulk closing all cases..."
curl -s -X POST "$BASE_URL/cases/bulk" \
  -H "Content-Type: application/json" \
  -d "{
    \"case_ids\": [${CASE_IDS[0]}, ${CASE_IDS[1]}, ${CASE_IDS[2]}],
    \"action\": \"close_all\"
  }" | python3 -m json.tool
echo ""

# Test 11: Invalid action
echo "5️⃣ Testing invalid action (should fail with 400)..."
curl -s -X POST "$BASE_URL/cases/bulk" \
  -H "Content-Type: application/json" \
  -d "{
    \"case_ids\": [${CASE_IDS[0]}],
    \"action\": \"delete_all\"
  }" | python3 -m json.tool
echo ""

# Test 12: Missing case_ids
echo "6️⃣ Testing missing case_ids (should fail with 400)..."
curl -s -X POST "$BASE_URL/cases/bulk" \
  -H "Content-Type: application/json" \
  -d "{
    \"action\": \"close_all\"
  }" | python3 -m json.tool
echo ""

echo "========================================="
echo "✅ All tests completed!"
echo "========================================="
