<?php
// Simple script to update SMS credentials directly with error handling
echo "Attempting to update SMS credentials...\n";

// Database configuration
$host = 'localhost';
$dbname = 'school_management';
$username = 'root';
$password = 'root';

try {
    echo "Connecting to database...\n";
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false,
    ]);

    echo "Connected successfully!\n";
    
    // Update the non-masking SMS settings with provided credentials
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    
    // Update the credentials with your provided information
    $stmt->execute(['non_musking_username', 'C3002791', 'C3002791']);
    echo "Updated non_musking_username\n";
    
    $stmt->execute(['non_musking_api_key', 'C30027916975f051ed0e01.45424521', 'C30027916975f051ed0e01.45424521']);
    echo "Updated non_musking_api_key\n";
    
    $stmt->execute(['non_musking_sender_id', '8809601016579', '8809601016579']);
    echo "Updated non_musking_sender_id\n";
    
    // Make sure non-musking is enabled
    $stmt->execute(['non_musking_enabled', '1', '1']);
    echo "Enabled non_musking\n";
    
    // Also update the general SMS settings
    $stmt->execute(['sms_username', 'C3002791', 'C3002791']);
    echo "Updated sms_username\n";
    
    $stmt->execute(['sms_api_key', 'C30027916975f051ed0e01.45424521', 'C30027916975f051ed0e01.45424521']);
    echo "Updated sms_api_key\n";
    
    $stmt->execute(['sms_sender_id', '8809601016579', '8809601016579']);
    echo "Updated sms_sender_id\n";
    
    // Enable general SMS
    $stmt->execute(['sms_enabled', '1', '1']);
    echo "Enabled sms\n";
    
    // Also update the API URL if needed (using mram.com.bd API)
    $stmt->execute(['non_musking_api_url', 'https://mram.com.bd/sms-api/sendsms.php', 'https://mram.com.bd/sms-api/sendsms.php']);
    echo "Updated non_musking_api_url\n";
    
    $stmt->execute(['sms_api_url', 'https://mram.com.bd/sms-api/sendsms.php', 'https://mram.com.bd/sms-api/sendsms.php']);
    echo "Updated sms_api_url\n";
    
    echo "\nSMS credentials updated successfully!\n";
    echo "Username: C3002791\n";
    echo "API Key: C30027916975f051ed0e01.45424521\n";
    echo "Sender ID: 8809601016579\n";
    echo "Non-masking SMS: Enabled\n";
    echo "General SMS: Enabled\n";

} catch (PDOException $e) {
    echo "Database connection error: " . $e->getMessage() . "\n";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>