<?php
require_once 'config/database.php';

echo "<h2>Fix Sender ID Configuration</h2>";

try {
    // First, let's check what's currently in the database
    echo "<h3>Current Database Settings:</h3>";
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_sender_id'");
    $stmt->execute();
    $result = $stmt->fetch();
    
    if ($result) {
        echo "<p>Current non_musking_sender_id: <strong>" . htmlspecialchars($result['setting_value']) . "</strong></p>";
    } else {
        echo "<p>non_musking_sender_id: <strong>NOT SET</strong></p>";
    }
    
    // Update the sender ID to your correct value
    echo "<h3>Updating Sender ID...</h3>";
    
    $correct_sender_id = '8809601016579';
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    $stmt->execute(['non_musking_sender_id', $correct_sender_id, $correct_sender_id]);
    
    echo "<p>✅ Updated non_musking_sender_id to: <strong>" . $correct_sender_id . "</strong></p>";
    
    // Verify the update
    echo "<h3>Verification:</h3>";
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_sender_id'");
    $stmt->execute();
    $result = $stmt->fetch();
    
    if ($result && $result['setting_value'] === $correct_sender_id) {
        echo "<p style='color: green;'>✅ SUCCESS: Sender ID correctly updated to " . $correct_sender_id . "</p>";
    } else {
        echo "<p style='color: red;'>❌ FAILED: Update verification failed</p>";
    }
    
    // Test the configuration loading
    echo "<h3>Configuration Test:</h3>";
    require_once 'includes/sms_helper.php';
    $config = non_musking_config();
    
    echo "<ul>";
    echo "<li>Enabled: " . ($config['enabled'] ? 'Yes' : 'No') . "</li>";
    echo "<li>Sender ID: " . htmlspecialchars($config['sender_id'] ?? 'NOT SET') . "</li>";
    echo "<li>API Key: " . (!empty($config['api_key']) ? 'SET' : 'NOT SET') . "</li>";
    echo "<li>API URL: " . htmlspecialchars($config['api_url'] ?? 'NOT SET') . "</li>";
    echo "</ul>";
    
    if (($config['sender_id'] ?? '') === $correct_sender_id) {
        echo "<p style='color: green;'>✅ Configuration loading correctly - using your sender ID: " . $correct_sender_id . "</p>";
    } else {
        echo "<p style='color: red;'>❌ Configuration issue - still using: " . htmlspecialchars($config['sender_id'] ?? 'DEFAULT') . "</p>";
    }
    
    echo "<h3>Next Steps:</h3>";
    echo "<ol>";
    echo "<li>Go to <a href='admin/sms_settings.php'>SMS Settings</a> and verify the Sender ID field shows " . $correct_sender_id . "</li>";
    echo "<li>Test sending an SMS to confirm it works with your correct sender ID</li>";
    echo "<li>If fee notifications still don't work, check that both 'Non-Musking SMS' and 'Fee Notifications' are enabled</li>";
    echo "</ol>";
    
} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . htmlspecialchars($e->getMessage()) . "</p>";
}
?>