<?php

/**
 * Check if an invoice should be marked as paid based on the transaction amount
 * Considers a threshold of 1% below invoice amount to account for payment fees
 * 
 * @param float $txAmount The actual transaction/payment amount
 * @param float $invAmount The invoice amount
 * @return bool True if the payment meets the threshold criteria
 */
function checkInvoicePayed($txAmount, $invAmount) {
    $threshold = $invAmount * 0.99; // 1% threshold
    return $txAmount >= $threshold;
}

/**
 * Calculate the invoice amount for which a given transaction amount
 * would be at exactly the 1% threshold boundary
 * 
 * @param float $txAmount The transaction amount to test
 * @return float The calculated invoice amount
 */
function challengeCheckInvoicePayed($txAmount) {
    // If txAmount = invAmount * 0.99
    // Then invAmount = txAmount / 0.99
    return $txAmount / 0.99;
}

/**
 * Test the payment checking functions with various edge cases
 */
function runTests() {
    $testCases = [
        ['tx' => 100, 'expected_inv' => 101.01],
        ['tx' => 99, 'expected_inv' => 100],
        ['tx' => 0.99, 'expected_inv' => 1],
        ['tx' => 1000, 'expected_inv' => 1010.10],
    ];
    
    foreach ($testCases as $test) {
        $calculatedInv = challengeCheckInvoicePayed($test['tx']);
        $isPassed = checkInvoicePayed($test['tx'], $calculatedInv);
        
        echo sprintf(
            "Test case: TX=%.2f, Calculated INV=%.2f, Expected INV=%.2f, Is at threshold: %s\n",
            $test['tx'],
            $calculatedInv,
            $test['expected_inv'],
            $isPassed ? 'Yes' : 'No'
        );
        
        // Verify the boundary condition
        $slightlyMore = checkInvoicePayed($test['tx'], $calculatedInv - 0.01);
        $slightlyLess = checkInvoicePayed($test['tx'], $calculatedInv + 0.01);
        
        if ($slightlyMore !== true || $slightlyLess !== false) {
            echo "WARNING: Boundary condition failed for TX amount " . $test['tx'] . "\n";
        }
    }
}

// Run the tests
runTests();