<?php
echo "<h2>MRAM Developers Portal API Key - CORRECTED</h2>";

// The ACTUAL API key from the MRAM Developers portal as provided by you
$actual_mram_api_key = 'C30027916975f051ed0e01.45424521';

$mram_credentials = [
    'username' => 'C3002791',
    'password' => 'hPOWSnw8rf',
    'api_key' => $actual_mram_api_key,  // This is the REAL key from the portal
    'sender_id' => '8809601016579',
    'api_url' => 'https://sms.mram.com.bd/smsapi',  // Updated API URL based on the portal
    'balance_api_url' => 'https://sms.mram.com.bd/miscapi/' . $actual_mram_api_key . '/getBalance',
    'price_api_url' => 'https://sms.mram.com.bd/miscapi/' . $actual_mram_api_key . '/getPrice',
    'dlr_api_url' => 'https://sms.mram.com.bd/miscapi/' . $actual_mram_api_key . '/getDLR/getAll'
];

echo "<h3>REAL MRAM Developers Portal Credentials (from portal):</h3>";
echo "<p><strong>Portal URL:</strong> <a href='https://sms.mram.com.bd/Developers' target='_blank'>https://sms.mram.com.bd/Developers</a></p>";
echo "<ul>";
foreach ($mram_credentials as $key => $value) {
    if ($key === 'password') {
        echo "<li><strong>$key:</strong> ******** (hidden)</li>";
    } else {
        echo "<li><strong>$key:</strong> $value</li>";
    }
}
echo "</ul>";

echo "<h3>API Key Details:</h3>";
echo "<p><strong>Actual API Key from Portal:</strong> {$mram_credentials['api_key']}</p>";
echo "<p><strong>Length:</strong> " . strlen($mram_credentials['api_key']) . "</p>";
echo "<p><strong>Company Code:</strong> " . substr($mram_credentials['api_key'], 0, 8) . "</p>";
echo "<p><strong>Unique Part:</strong> " . substr($mram_credentials['api_key'], 8) . "</p>";

// Update database with the CORRECT API key
$host = 'localhost';
$dbname = 'school_management';
$username = 'root';
$password = 'root';

try {
    $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 "<h3>Updating Database with CORRECT API Key:</h3>";
    
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    
    // Update with the ACTUAL API key from the portal
    $stmt->execute(['non_musking_api_key', $actual_mram_api_key, $actual_mram_api_key]);
    $stmt->execute(['sms_api_key', $actual_mram_api_key, $actual_mram_api_key]);
    
    // Also update the API URL to match portal specification
    $stmt->execute(['non_musking_api_url', 'https://sms.mram.com.bd/smsapi', 'https://sms.mram.com.bd/smsapi']);
    $stmt->execute(['sms_api_url', 'https://sms.mram.com.bd/smsapi', 'https://sms.mram.com.bd/smsapi']);
    
    // Update other credentials
    $stmt->execute(['non_musking_username', 'C3002791', 'C3002791']);
    $stmt->execute(['non_musking_password', 'hPOWSnw8rf', 'hPOWSnw8rf']);
    $stmt->execute(['non_musking_sender_id', '8809601016579', '8809601016579']);
    $stmt->execute(['sms_username', 'C3002791', 'C3002791']);
    $stmt->execute(['sms_password', 'hPOWSnw8rf', 'hPOWSnw8rf']);
    $stmt->execute(['sms_sender_id', '8809601016579', '8809601016579']);
    
    echo "<p style='color:green; font-size:18px;'><strong>✅ Database updated with CORRECT API key from MRAM portal!</strong></p>";
    
    // Verify the update
    $verify_stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('non_musking_api_key', 'sms_api_key')");
    $verify_stmt->execute();
    $results = $verify_stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h3>Verification After Update:</h3>";
    foreach ($results as $result) {
        $key = $result['setting_key'];
        $value = $result['setting_value'];
        $is_correct = ($value === $actual_mram_api_key) ? '✅ CORRECT' : '❌ INCORRECT';
        echo "<p><strong>$key:</strong> $value - $is_correct</p>";
    }

} catch (Exception $e) {
    echo "<p style='color: red;'>Database error: " . $e->getMessage() . "</p>";
}

echo "<h3>MRAM API Information (from portal):</h3>";
echo "<ul>";
echo "<li><strong>API URL:</strong> https://sms.mram.com.bd/smsapi?api_key=(APIKEY)&type=text&contacts=(NUMBER)&senderid=(Approved Sender ID)&msg=(Message Content)</li>";
echo "<li><strong>Type:</strong> text/unicode (text for normal SMS, unicode for Bangla SMS)</li>";
echo "<li><strong>Balance API:</strong> https://sms.mram.com.bd/miscapi/{$actual_mram_api_key}/getBalance</li>";
echo "<li><strong>Price API:</strong> https://sms.mram.com.bd/miscapi/{$actual_mram_api_key}/getPrice</li>";
echo "</ul>";

echo "<h3>Next Steps:</h3>";
echo "<ul>";
echo "<li>Test sending SMS with the correct API key</li>";
echo "<li>Visit <a href='simple_sms_test.php'>Simple SMS Test</a> to verify</li>";
echo "<li>Try sending a test SMS</li>";
echo "</ul>";
?>