#!/bin/bash
# Quick script to test all deletion endpoints
# Usage: ./test_deletion_endpoints.sh <tenant_id>

set -e

TENANT_ID=${1:-"test-tenant-123"}
BASE_URL=${BASE_URL:-"http://localhost:8000"}
TOKEN=${AUTH_TOKEN:-"test-token"}

echo "================================"
echo "Testing Deletion Endpoints"
echo "Tenant ID: $TENANT_ID"
echo "Base URL: $BASE_URL"
echo "================================"
echo ""

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Function to test endpoint
test_endpoint() {
    local service=$1
    local method=$2
    local path=$3
    local expected_status=${4:-200}

    echo -n "Testing $service ($method $path)... "

    response=$(curl -s -w "\n%{http_code}" \
        -X $method \
        -H "Authorization: Bearer $TOKEN" \
        -H "X-Internal-Service: test-script" \
        "$BASE_URL/api/v1/$path" 2>&1)

    status_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | head -n-1)

    if [ "$status_code" == "$expected_status" ] || [ "$status_code" == "404" ]; then
        if [ "$status_code" == "404" ]; then
            echo -e "${YELLOW}NOT IMPLEMENTED${NC} (404)"
        else
            echo -e "${GREEN}✓ PASSED${NC} ($status_code)"
            if [ "$method" == "GET" ]; then
                # Show preview counts
                total=$(echo "$body" | jq -r '.total_items // 0' 2>/dev/null || echo "N/A")
                if [ "$total" != "N/A" ]; then
                    echo "    → Preview: $total items would be deleted"
                fi
            elif [ "$method" == "DELETE" ]; then
                # Show deletion summary
                deleted=$(echo "$body" | jq -r '.summary.total_deleted // 0' 2>/dev/null || echo "N/A")
                if [ "$deleted" != "N/A" ]; then
                    echo "    → Deleted: $deleted items"
                fi
            fi
        fi
    else
        echo -e "${RED}✗ FAILED${NC} ($status_code)"
        echo "    Response: $body"
    fi
}

echo "=== COMPLETED SERVICES ==="
echo ""

echo "1. Tenant Service:"
test_endpoint "tenant" "GET" "tenants/$TENANT_ID"
test_endpoint "tenant" "DELETE" "tenants/$TENANT_ID"
echo ""

echo "2. Orders Service:"
test_endpoint "orders" "GET" "orders/tenant/$TENANT_ID/deletion-preview"
test_endpoint "orders" "DELETE" "orders/tenant/$TENANT_ID"
echo ""

echo "3. Inventory Service:"
test_endpoint "inventory" "GET" "inventory/tenant/$TENANT_ID/deletion-preview"
test_endpoint "inventory" "DELETE" "inventory/tenant/$TENANT_ID"
echo ""

echo "4. Recipes Service:"
test_endpoint "recipes" "GET" "recipes/tenant/$TENANT_ID/deletion-preview"
test_endpoint "recipes" "DELETE" "recipes/tenant/$TENANT_ID"
echo ""

echo "5. Sales Service:"
test_endpoint "sales" "GET" "sales/tenant/$TENANT_ID/deletion-preview"
test_endpoint "sales" "DELETE" "sales/tenant/$TENANT_ID"
echo ""

echo "6. Production Service:"
test_endpoint "production" "GET" "production/tenant/$TENANT_ID/deletion-preview"
test_endpoint "production" "DELETE" "production/tenant/$TENANT_ID"
echo ""

echo "7. Suppliers Service:"
test_endpoint "suppliers" "GET" "suppliers/tenant/$TENANT_ID/deletion-preview"
test_endpoint "suppliers" "DELETE" "suppliers/tenant/$TENANT_ID"
echo ""

echo "=== PENDING SERVICES ==="
echo ""

echo "8. POS Service:"
test_endpoint "pos" "GET" "pos/tenant/$TENANT_ID/deletion-preview"
test_endpoint "pos" "DELETE" "pos/tenant/$TENANT_ID"
echo ""

echo "9. External Service:"
test_endpoint "external" "GET" "external/tenant/$TENANT_ID/deletion-preview"
test_endpoint "external" "DELETE" "external/tenant/$TENANT_ID"
echo ""

echo "10. Alert Processor Service:"
test_endpoint "alert_processor" "GET" "alerts/tenant/$TENANT_ID/deletion-preview"
test_endpoint "alert_processor" "DELETE" "alerts/tenant/$TENANT_ID"
echo ""

echo "11. Forecasting Service:"
test_endpoint "forecasting" "GET" "forecasts/tenant/$TENANT_ID/deletion-preview"
test_endpoint "forecasting" "DELETE" "forecasts/tenant/$TENANT_ID"
echo ""

echo "12. Training Service:"
test_endpoint "training" "GET" "models/tenant/$TENANT_ID/deletion-preview"
test_endpoint "training" "DELETE" "models/tenant/$TENANT_ID"
echo ""

echo "13. Notification Service:"
test_endpoint "notification" "GET" "notifications/tenant/$TENANT_ID/deletion-preview"
test_endpoint "notification" "DELETE" "notifications/tenant/$TENANT_ID"
echo ""

echo "================================"
echo "Testing Complete!"
echo "================================"
