<!DOCTYPE html>
<html>
<head>
    <title>QuickBooks OAuth Callback</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 40px auto; padding: 20px; }
        .code { background: #f5f5f5; padding: 15px; border-radius: 5px; font-family: monospace; word-break: break-all; }
        .success { color: green; font-weight: bold; }
        .error { color: red; }
    </style>
</head>
<body>
    <h1>QuickBooks OAuth Callback</h1>
    
    <?php
    if (isset($_GET['code']) && isset($_GET['realmId'])) {
        $code = htmlspecialchars($_GET['code']);
        $realmId = htmlspecialchars($_GET['realmId']);
        $state = isset($_GET['state']) ? htmlspecialchars($_GET['state']) : '';
        
        echo '<p class="success">✅ OAuth authorization successful!</p>';
        echo '<p>Copy these values and provide them to the MCP server:</p>';
        
        echo '<h3>Authorization Code:</h3>';
        echo '<div class="code">' . $code . '</div>';
        
        echo '<h3>Realm ID:</h3>';
        echo '<div class="code">' . $realmId . '</div>';
        
        if ($state) {
            echo '<h3>State (for reference):</h3>';
            echo '<div class="code">' . $state . '</div>';
        }
        
        echo '<p><strong>Next step:</strong> Call the <code>qbo_authenticate</code> tool again with these values.</p>';
        
    } elseif (isset($_GET['error'])) {
        $error = htmlspecialchars($_GET['error']);
        $error_description = isset($_GET['error_description']) ? htmlspecialchars($_GET['error_description']) : '';
        
        echo '<p class="error">❌ OAuth Error: ' . $error . '</p>';
        if ($error_description) {
            echo '<p>Description: ' . $error_description . '</p>';
        }
    } else {
        echo '<p class="error">❌ Missing required parameters. Expected: code and realmId</p>';
        echo '<p>Received parameters:</p>';
        echo '<ul>';
        foreach ($_GET as $key => $value) {
            echo '<li>' . htmlspecialchars($key) . ' = ' . htmlspecialchars($value) . '</li>';
        }
        echo '</ul>';
    }
    ?>
    
    <hr>
    <p><small>This is the OAuth callback endpoint for QuickBooks MCP server integration.</small></p>
</body>
</html>