<?php
// Direct check of bank SMS settings in the database
require_once 'config/database.php';

echo "<h3>Checking Bank Transaction SMS Settings</h3>\n";

// Check if required settings exist
$required_settings = [
    'non_musking_enabled',
    'sms_send_bank_transaction_notifications',
    'non_musking_api_key',
    'non_musking_api_url',
    'non_musking_sender_id'
];

echo "<table border='1'>\n";
echo "<tr><th>Setting Key</th><th>Value</th><th>Status</th></tr>\n";

foreach ($required_settings as $setting) {
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
    $stmt->execute([$setting]);
    $result = $stmt->fetch();
    
    $value = $result ? $result['setting_value'] : 'NOT FOUND';
    $status = ($value !== 'NOT FOUND' && $value !== '') ? '✅ OK' : '❌ MISSING';
    
    echo "<tr><td>{$setting}</td><td>{$value}</td><td>{$status}</td></tr>\n";
}

echo "</table>\n";

// Check specifically for bank transaction notification setting
$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_send_bank_transaction_notifications'");
$stmt->execute();
$result = $stmt->fetch();

if ($result && $result['setting_value'] === '1') {
    echo "<p>✅ Bank transaction notifications are ENABLED</p>\n";
} else {
    echo "<p>❌ Bank transaction notifications are DISABLED or MISSING</p>\n";
    echo "<p>Need to run setup to enable bank transaction notifications...</p>\n";
    
    // Add the missing setting
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('sms_send_bank_transaction_notifications', '1') ON DUPLICATE KEY UPDATE setting_value = '1'");
    $stmt->execute();
    
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('sms_send_bank_transaction_templates_bank_transaction', 'ব্যাংক ট্রানজেকশন পেমেন্ট রিসিভড {paid_amount} টাকা। বকেয়া: {due_amount} টাকা।') ON DUPLICATE KEY UPDATE setting_value = 'ব্যাংক ট্রানজেকশন পেমেন্ট রিসিভড {paid_amount} টাকা। বকেয়া: {due_amount} টাকা।'");
    $stmt->execute();
    
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('sms_send_bank_transaction_templates_bank_transaction_rejection', 'ব্যাংক ট্রানজেকশন প্রত্যাখ্যান করা হয়েছে। পরিমাণ: {amount} টাকা। কারণ: {notes}') ON DUPLICATE KEY UPDATE setting_value = 'ব্যাংক ট্রানজেকশন প্রত্যাখ্যান করা হয়েছে। পরিমাণ: {amount} টাকা। কারণ: {notes}'");
    $stmt->execute();
    
    echo "<p>✅ Bank transaction notification settings have been added/enabled!</p>\n";
}

echo "<h3>Verification:</h3>\n";
$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_send_bank_transaction_notifications'");
$stmt->execute();
$result = $stmt->fetch();

if ($result && $result['setting_value'] === '1') {
    echo "<p>✅ Bank transaction notifications are now ENABLED</p>\n";
} else {
    echo "<p>❌ Failed to enable bank transaction notifications</p>\n";
}

echo "<p><strong>Process completed. Bank transaction SMS notifications should now work when transactions are verified.</strong></p>\n";
?>