<?php
// Read the entire file
$content = file_get_contents('fees/verify_bank_transactions.php');

// Define the exact duplicate section to remove (from line 269 to 329)
$startMarker = "                \$smsHelperPath = __DIR__ . '/../includes/sms_helper.php';";
$endMarker = "            } catch (Throwable \$e) {";

// Find the position of the first occurrence of the start marker (this is the original, should remain)
$pos1 = strpos($content, $startMarker);
$pos2 = strpos($content, $startMarker, $pos1 + 1); // This is the duplicate

if ($pos2 !== false) {
    // Find the end of the duplicate section
    // Look for the corresponding end after the second occurrence
    $endPos = strpos($content, "            }", $pos2);
    
    // Continue to find the closing braces and catch block for the duplicate
    // Count the number of closing braces after the duplicate starts
    $tempPos = $pos2;
    $braceLevel = 0;
    $foundBraces = 0;
    $lastBracePos = 0;
    
    // Find the } catch (Throwable $e) { block that belongs to the duplicate
    $catchPos = strpos($content, "            } catch (Throwable \$e) {", $pos2);
    if ($catchPos !== false) {
        // Find the two closing braces after the catch block
        $brace1 = strpos($content, "            }", $catchPos);
        if ($brace1 !== false) {
            $brace2 = strpos($content, "            }", $brace1 + 1);
            if ($brace2 !== false) {
                // The end of our duplicate section is at brace2
                $sectionEnd = $brace2 + strlen("            }");
                
                // Extract the section to remove
                $sectionToRemove = substr($content, $pos2, $sectionEnd - $pos2);
                
                echo "Found duplicate section of " . strlen($sectionToRemove) . " characters\n";
                
                // Remove the duplicate section
                $newContent = str_replace($sectionToRemove, '', $content);
                
                // Write the fixed content back to the file
                file_put_contents('fees/verify_bank_transactions.php', $newContent);
                
                echo "Duplicate section removed successfully!\n";
            } else {
                echo "Could not find the second closing brace\n";
            }
        } else {
            echo "Could not find the first closing brace\n";
        }
    } else {
        echo "Could not find the catch block for the duplicate\n";
    }
} else {
    echo "No duplicate section found\n";
}
?>