<?php
/**
 * Authentication System Test Script
 */

require_once('../config/auth_config.php');
require_once('../otp_service.php');
require_once('../auth_middleware.php');

// Test OTP Service
function testOTPService() {
    $otpService = new OTPService();
    $email = 'test@example.com';
    
    echo "Testing OTP Service...\n";
    
    // Test OTP Generation
    list($success, $message, $otp) = $otpService->generateAndSendOTP($email);
    echo "Generate OTP: " . ($success ? "PASS" : "FAIL") . "\n";
    echo "Message: $message\n";
    
    if ($success) {
        // Test OTP Verification
        list($success, $message, $token) = $otpService->verifyOTP($email, $otp);
        echo "Verify OTP: " . ($success ? "PASS" : "FAIL") . "\n";
        echo "Message: $message\n";
        
        if ($success) {
            // Test Token Validation
            $auth = new AuthMiddleware();
            $headers = ['Authorization' => 'Bearer ' . $token];
            list($success, $user) = $auth->validateRequest();
            echo "Validate Token: " . ($success ? "PASS" : "FAIL") . "\n";
        }
    }
}

// Test Rate Limiting
function testRateLimiting() {
    $otpService = new OTPService();
    $email = 'test@example.com';
    
    echo "\nTesting Rate Limiting...\n";
    
    for ($i = 0; $i < 4; $i++) {
        list($success, $message) = $otpService->generateAndSendOTP($email);
        echo "Request $i: " . ($success ? "PASS" : "FAIL") . "\n";
        echo "Message: $message\n";
    }
}

// Run Tests
echo "Starting Authentication System Tests...\n\n";

testOTPService();
testRateLimiting();

echo "\nTests completed.\n"; 