<?php
/**
 * API Test Script
 * 
 * This script tests the basic functionality of the Sender ID API
 */

// Load configuration
require_once __DIR__ . '/../config/config.php';

// Define test data
$baseUrl = 'http://localhost:8001';
$apiKey = DEFAULT_API_KEY;
$testSenderId = 'TESTAPI' . rand(100, 999);
$testUsername = 'npontutest';
$testPassword = 'npontutest';

// Helper function to make API requests
function make_api_request($url, $method = 'GET', $data = null, $apiKey) {
    $ch = curl_init();
    
    $headers = [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json'
    ];
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    if ($method == 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
        if ($data) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        }
    } else if ($method == 'DELETE') {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
        if ($data) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        }
    }
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error = curl_error($ch);
    curl_close($ch);
    
    return [
        'code' => $httpCode,
        'response' => $response ? json_decode($response, true) : null,
        'error' => $error
    ];
}

// Test the whitelist endpoint
echo "Testing whitelist endpoint...\n";
$result = make_api_request($baseUrl . '/whitelist', 'GET', null, $apiKey);
echo "Status code: " . $result['code'] . "\n";
if ($result['code'] == 200) {
    echo "Success! Current whitelisted IPs:\n";
    if (!empty($result['response']['data'])) {
        foreach ($result['response']['data'] as $ip) {
            echo "- " . $ip['ip_address'] . " (" . $ip['description'] . ")\n";
        }
    } else {
        echo "No IP addresses found in whitelist.\n";
    }
} else {
    echo "Failed to get whitelist: " . ($result['error'] ?? print_r($result['response'], true)) . "\n";
}
echo "\n";

// Test creating a sender ID
echo "Testing sender ID creation with ID: $testSenderId...\n";
$data = [
    'sender_id' => $testSenderId,
    'user_name' => $testUsername,
    'password' => $testPassword,
    'IP' => '127.0.0.1'
];
$result = make_api_request($baseUrl . '/sender-id', 'POST', $data, $apiKey);
echo "Status code: " . $result['code'] . "\n";
if ($result['code'] == 201) {
    echo "Success! Sender ID created.\n";
    echo "Request ID: " . $result['response']['data']['request_id'] . "\n";
    $requestId = $result['response']['data']['request_id'];
} else {
    echo "Failed to create sender ID: " . ($result['error'] ?? print_r($result['response'], true)) . "\n";
}
echo "\n";

// Test getting all sender IDs
echo "Testing get all sender IDs...\n";
$result = make_api_request($baseUrl . '/sender-id?page=1&limit=10', 'GET', null, $apiKey);
echo "Status code: " . $result['code'] . "\n";
if ($result['code'] == 200) {
    echo "Success! Found " . count($result['response']['data']['requests'] ?? []) . " sender IDs.\n";
    if (!empty($result['response']['data']['requests'])) {
        foreach ($result['response']['data']['requests'] as $request) {
            echo "- " . $request['sender_id'] . " (Status: " . $request['status'] . ")\n";
        }
    }
} else {
    echo "Failed to get sender IDs: " . ($result['error'] ?? print_r($result['response'], true)) . "\n";
}
echo "\n";

// Print summary
echo "=========================\n";
echo "API Test Summary\n";
echo "=========================\n";
echo "Base URL: $baseUrl\n";
echo "Test Sender ID: $testSenderId\n";
echo "Test completed at: " . date('Y-m-d H:i:s') . "\n";
echo "=========================\n"; 