<?php
$content = file_get_contents('fees/verify_bank_transactions.php');

// Find the positions of both occurrences of the SMS helper path
$pos1 = strpos($content, '$smsHelperPath = __DIR__ . \'../includes/sms_helper.php\';);
$pos2 = strpos($content, '$smsHelperPath = __DIR__ . \'../includes/sms_helper.php\';, $pos1 + 1);

echo "First occurrence at position: $pos1\n";
echo "Second occurrence at position: $pos2\n";

if ($pos2 !== false) {
    // Find the complete duplicate block by looking for the full pattern
    // Find the start of the if block containing the second occurrence
    $startSearch = $pos2;
    
    // Look backwards for the opening brace of the if statement that handles rejection
    $contextStart = strrpos(substr($content, 0, $startSearch), 'if ($action === \'reject\')');
    if ($contextStart === false) {
        // If not found, look for the beginning of the SMS block
        $contextStart = $pos2;
    }

    // Look for the complete block from the second occurrence to its corresponding end
    // We'll look for the corresponding closing braces and catch block
    
    // Find the end of the duplicate section by looking for the end of the catch block
    // Find the closing braces after pos2
    $sectionStart = $pos2;
    
    // Count braces to find the matching end of the code block
    $braceCount = 0;
    $inString = false;
    $stringChar = '';
    $pos = $sectionStart;
    
    while ($pos < strlen($content)) {
        $char = $content[$pos];
        
        // Handle string literals to avoid counting braces inside strings
        if ($char === '"' || $char === "'") {
            if (!$inString) {
                $inString = true;
                $stringChar = $char;
            } elseif ($stringChar === $char && $content[$pos - 1] !== '\\') {
                $inString = false;
            }
        }
        
        if (!$inString) {
            if ($char === '{') {
                $braceCount++;
            } elseif ($char === '}') {
                $braceCount--;
                if ($braceCount === 0) {
                    // We found the end of the outermost block
                    // Now look for the catch block that follows
                    $catchPos = strpos($content, '            } catch (Throwable $e) {', $pos);
                    if ($catchPos !== false) {
                        // Find the end of the catch block (should have 2 more closing braces)
                        $endPos = strpos($content, '            }', $catchPos + 1);
                        if ($endPos !== false) {
                            $endPos = strpos($content, '            }', $endPos + 1);
                            if ($endPos !== false) {
                                // Include the complete duplicate section
                                $sectionEnd = $endPos;
                                
                                // Extract the duplicate section
                                $duplicateSection = substr($content, $sectionStart, $sectionEnd - $sectionStart + 1);
                                
                                echo "Found duplicate section from position $sectionStart to $sectionEnd (length: " . strlen($duplicateSection) . ")\n";
                                
                                // Remove the duplicate section
                                $newContent = substr($content, 0, $sectionStart) . substr($content, $sectionEnd + 1);
                                file_put_contents('fees/verify_bank_transactions.php', $newContent);
                                
                                echo "Duplicate section removed successfully!\n";
                                break;
                            }
                        }
                    }
                    break;
                }
            }
        }
        $pos++;
    }
} else {
    echo "No duplicate found.\n";
}
?>