#!/bin/bash

# Test script for regenerate-webhook endpoint

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

# Configuration
BASE_URL="http://localhost:8000"
EMAIL="onur@onur.com"
PASSWORD="password123"

echo -e "${YELLOW}=== Testing Regenerate Webhook Endpoint ===${NC}\n"

# Step 1: Login to get authentication token
echo "Step 1: Logging in..."
LOGIN_RESPONSE=$(curl -s -X POST "${BASE_URL}/api/login" \
  -H "Content-Type: application/json" \
  -d "{
    \"email\": \"${EMAIL}\",
    \"password\": \"${PASSWORD}\"
  }")

echo "Login Response: $LOGIN_RESPONSE"

# Extract token from response (assuming JSON response with access_token field)
TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)

if [ -z "$TOKEN" ]; then
  echo -e "${RED}Failed to get authentication token. Please check credentials.${NC}"
  exit 1
fi

echo -e "${GREEN}✓ Successfully authenticated${NC}\n"

# Step 2: Get a valid topic_id (list topics)
echo "Step 2: Fetching topics to get a valid topic_id..."
TOPICS_RESPONSE=$(curl -s -X GET "${BASE_URL}/api/topics" \
  -H "Authorization: Bearer ${TOKEN}" \
  -H "Content-Type: application/json")

echo "Topics Response: $TOPICS_RESPONSE"

TOPIC_ID=$(echo $TOPICS_RESPONSE | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)

if [ -z "$TOPIC_ID" ]; then
  echo -e "${YELLOW}No topics found. Let's use topic_id=999999 (for temporary storage test)${NC}"
  TOPIC_ID=999999
else
  echo -e "${GREEN}✓ Found topic_id: ${TOPIC_ID}${NC}"
fi

# Step 3: Create a simple base64 encoded test image
echo -e "\nStep 3: Creating base64 encoded test image..."
# Creating a simple 1x1 red PNG image in base64
TEST_IMAGE_BASE64="iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="

# Full data URI format
IMAGE_DATA="data:image/png;base64,${TEST_IMAGE_BASE64}"

echo "Image data created (length: ${#IMAGE_DATA} characters)"

# Step 4: Send regenerate-webhook request
echo -e "\nStep 4: Sending regenerate-webhook request..."
WEBHOOK_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/api/regenerate-webhook" \
  -H "Authorization: Bearer ${TOKEN}" \
  -H "Content-Type: application/json" \
  -d "{
    \"topic_id\": ${TOPIC_ID},
    \"image_data\": \"${IMAGE_DATA}\"
  }")

# Extract HTTP status code (last line)
HTTP_CODE=$(echo "$WEBHOOK_RESPONSE" | tail -n1)
# Extract response body (all lines except last)
RESPONSE_BODY=$(echo "$WEBHOOK_RESPONSE" | head -n-1)

echo -e "\nHTTP Status Code: ${HTTP_CODE}"
echo "Response Body: ${RESPONSE_BODY}"

if [ "$HTTP_CODE" -eq 200 ] || [ "$HTTP_CODE" -eq 201 ]; then
  echo -e "${GREEN}✓ Regenerate webhook request successful!${NC}"
else
  echo -e "${RED}✗ Regenerate webhook request failed with status code: ${HTTP_CODE}${NC}"
fi

# Step 5: Display the complete curl command for manual use
echo -e "\n${YELLOW}=== Manual curl command: ===${NC}"
echo "
curl -X POST \"${BASE_URL}/api/regenerate-webhook\" \\
  -H \"Authorization: Bearer ${TOKEN}\" \\
  -H \"Content-Type: application/json\" \\
  -d '{
    \"topic_id\": ${TOPIC_ID},
    \"image_data\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==\"
  }'
"

echo -e "\n${GREEN}=== Test Complete ===${NC}"
