#!/bin/bash

# FCM Notification API Test Script
# This script tests all FCM notification endpoints

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

# Configuration
BASE_URL="http://localhost/api"
USER_TOKEN="YOUR_USER_JWT_TOKEN"
ADMIN_TOKEN="YOUR_ADMIN_JWT_TOKEN"
TEST_USER_ID=1

echo -e "${YELLOW}=== FCM Notification API Test Suite ===${NC}\n"

# Test 1: Test FCM Configuration
echo -e "${YELLOW}Test 1: Testing FCM Configuration${NC}"
echo "GET $BASE_URL/notifications/test-fcm.php"
response=$(curl -s -w "\n%{http_code}" -X GET "$BASE_URL/notifications/test-fcm.php" \
  -H "Authorization: Bearer $USER_TOKEN")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n-1)

if [ "$http_code" = "200" ]; then
    echo -e "${GREEN}✓ Pass${NC}"
    echo "$body" | jq '.'
else
    echo -e "${RED}✗ Fail (HTTP $http_code)${NC}"
    echo "$body"
fi
echo ""

# Test 2: Send to Single User
echo -e "${YELLOW}Test 2: Send Notification to Single User${NC}"
echo "POST $BASE_URL/notifications/send.php"
response=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/notifications/send.php" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $USER_TOKEN" \
  -d '{
    "title": "Test Notification",
    "body": "This is a test from the API test script",
    "targetUserId": '$TEST_USER_ID',
    "type": "test",
    "data": {
      "testId": 123,
      "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'"
    }
  }')
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n-1)

if [ "$http_code" = "201" ]; then
    echo -e "${GREEN}✓ Pass${NC}"
    echo "$body" | jq '.'
else
    echo -e "${RED}✗ Fail (HTTP $http_code)${NC}"
    echo "$body"
fi
echo ""

# Test 3: Send to Multiple Users
echo -e "${YELLOW}Test 3: Send Notification to Multiple Users${NC}"
echo "POST $BASE_URL/notifications/send-bulk.php"
response=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/notifications/send-bulk.php" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $USER_TOKEN" \
  -d '{
    "title": "Bulk Test Notification",
    "body": "This is sent to multiple users",
    "userIds": [1, 2, 3],
    "type": "test"
  }')
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n-1)

if [ "$http_code" = "201" ]; then
    echo -e "${GREEN}✓ Pass${NC}"
    echo "$body" | jq '.'
else
    echo -e "${RED}✗ Fail (HTTP $http_code)${NC}"
    echo "$body"
fi
echo ""

# Test 4: Admin Test FCM
echo -e "${YELLOW}Test 4: Admin Test FCM (requires admin token)${NC}"
echo "POST $BASE_URL/admin/notifications/test-fcm.php"
response=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/admin/notifications/test-fcm.php" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ADMIN_TOKEN" \
  -d '{
    "userId": '$TEST_USER_ID'
  }')
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n-1)

if [ "$http_code" = "200" ]; then
    echo -e "${GREEN}✓ Pass${NC}"
    echo "$body" | jq '.'
else
    echo -e "${RED}✗ Fail (HTTP $http_code)${NC}"
    echo "$body"
fi
echo ""

# Test 5: Admin Broadcast
echo -e "${YELLOW}Test 5: Admin Broadcast to All Users${NC}"
echo "POST $BASE_URL/admin/notifications/send.php"
response=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/admin/notifications/send.php" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ADMIN_TOKEN" \
  -d '{
    "title": "Test Broadcast",
    "body": "This is a test broadcast to all users",
    "type": "announcement"
  }')
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n-1)

if [ "$http_code" = "201" ]; then
    echo -e "${GREEN}✓ Pass${NC}"
    echo "$body" | jq '.'
else
    echo -e "${RED}✗ Fail (HTTP $http_code)${NC}"
    echo "$body"
fi
echo ""

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

# Instructions
echo -e "${YELLOW}Note: Before running this script:${NC}"
echo "1. Update USER_TOKEN with a valid user JWT token"
echo "2. Update ADMIN_TOKEN with a valid admin JWT token"
echo "3. Update TEST_USER_ID with a valid user ID"
echo "4. Update BASE_URL if your API is not at localhost"
echo "5. Make sure jq is installed (for JSON formatting)"
echo ""
echo "To run: bash test-fcm-api.sh"
