#!/bin/bash

# Script de test des APIs o2switch
# Ange Technologies - Octobre 2025

BASE_URL="https://zoneange.paydiaa.com"
TXN_ID="TEST_$(date +%s)"

echo "🧪 Test des APIs o2switch"
echo "=========================="
echo ""

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

# Test 1: get-transaction-api.php
echo -e "${YELLOW}Test 1: get-transaction-api.php${NC}"
echo "GET $BASE_URL/get-transaction-api.php?transaction_id=$TXN_ID"
RESPONSE=$(curl -s "$BASE_URL/get-transaction-api.php?transaction_id=$TXN_ID")
echo "$RESPONSE" | jq '.'
if echo "$RESPONSE" | grep -q '"success":false'; then
    echo -e "${GREEN}✅ Test 1 PASSED${NC} (Transaction non trouvée comme attendu)"
else
    echo -e "${RED}❌ Test 1 FAILED${NC}"
fi
echo ""

# Test 2: check-ticket-exists-api.php
echo -e "${YELLOW}Test 2: check-ticket-exists-api.php${NC}"
echo "GET $BASE_URL/check-ticket-exists-api.php?transaction_id=$TXN_ID"
RESPONSE=$(curl -s "$BASE_URL/check-ticket-exists-api.php?transaction_id=$TXN_ID")
echo "$RESPONSE" | jq '.'
if echo "$RESPONSE" | grep -q '"exists":false'; then
    echo -e "${GREEN}✅ Test 2 PASSED${NC}"
else
    echo -e "${RED}❌ Test 2 FAILED${NC}"
fi
echo ""

# Test 3: check-username-exists-api.php
echo -e "${YELLOW}Test 3: check-username-exists-api.php${NC}"
echo "GET $BASE_URL/check-username-exists-api.php?username=testuser123"
RESPONSE=$(curl -s "$BASE_URL/check-username-exists-api.php?username=testuser123")
echo "$RESPONSE" | jq '.'
if echo "$RESPONSE" | grep -q '"success":true'; then
    echo -e "${GREEN}✅ Test 3 PASSED${NC}"
else
    echo -e "${RED}❌ Test 3 FAILED${NC}"
fi
echo ""

# Test 4: create-ticket-api.php
echo -e "${YELLOW}Test 4: create-ticket-api.php${NC}"
echo "POST $BASE_URL/create-ticket-api.php"
RESPONSE=$(curl -s -X POST "$BASE_URL/create-ticket-api.php" \
    -H "Content-Type: application/json" \
    -d "{
        \"username\": \"test_${TXN_ID}\",
        \"password\": \"test_${TXN_ID}\",
        \"profile\": \"24H\",
        \"time_limit\": \"1d\",
        \"comment\": \"Test ticket\",
        \"transaction_id\": \"$TXN_ID\",
        \"customer_phone\": \"+22997000000\"
    }")
echo "$RESPONSE" | jq '.'
if echo "$RESPONSE" | grep -q '"success":true'; then
    echo -e "${GREEN}✅ Test 4 PASSED${NC} (Ticket créé)"
    TICKET_CREATED=1
else
    echo -e "${RED}❌ Test 4 FAILED${NC}"
    TICKET_CREATED=0
fi
echo ""

# Test 5: update-transaction-api.php
if [ $TICKET_CREATED -eq 1 ]; then
    echo -e "${YELLOW}Test 5: update-transaction-api.php${NC}"
    echo "POST $BASE_URL/update-transaction-api.php"
    
    # D'abord créer une transaction dans la BDD
    # (Note: Cela nécessite que le webhook ait déjà créé la transaction)
    
    RESPONSE=$(curl -s -X POST "$BASE_URL/update-transaction-api.php" \
        -H "Content-Type: application/json" \
        -d "{
            \"transaction_id\": \"$TXN_ID\",
            \"status\": \"processed\",
            \"ticket_username\": \"test_${TXN_ID}\"
        }")
    echo "$RESPONSE" | jq '.'
    if echo "$RESPONSE" | grep -q '"success":true'; then
        echo -e "${GREEN}✅ Test 5 PASSED${NC}"
    else
        echo -e "${YELLOW}⚠️ Test 5 WARNING${NC} (Transaction peut ne pas exister)"
    fi
    echo ""
fi

# Test 6: Vérifier que le ticket existe maintenant
if [ $TICKET_CREATED -eq 1 ]; then
    echo -e "${YELLOW}Test 6: Vérification ticket créé${NC}"
    echo "GET $BASE_URL/check-ticket-exists-api.php?transaction_id=$TXN_ID"
    RESPONSE=$(curl -s "$BASE_URL/check-ticket-exists-api.php?transaction_id=$TXN_ID")
    echo "$RESPONSE" | jq '.'
    if echo "$RESPONSE" | grep -q '"exists":true'; then
        echo -e "${GREEN}✅ Test 6 PASSED${NC} (Ticket trouvé)"
    else
        echo -e "${RED}❌ Test 6 FAILED${NC}"
    fi
    echo ""
fi

echo "=========================="
echo "🎉 Tests terminés !"
echo ""
echo "⚠️  Note: Pensez à nettoyer les données de test dans la BDD"
echo "   DELETE FROM tickets_pool WHERE comment LIKE '%Test%';"

