<?php
// Direct check of database settings for bank transaction SMS
header('Content-Type: text/html; charset=utf-8');

echo "<h2>Checking Database Settings for Bank Transaction SMS</h2>\n";

// Database configuration (same as in config/database.php)
$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 "<p>✅ Successfully connected to database</p>\n";
    
    // Check if settings table exists
    $stmt = $pdo->prepare("SHOW TABLES LIKE 'settings'");
    $stmt->execute();
    $tableExists = $stmt->fetch();
    
    if (!$tableExists) {
        echo "<p>❌ Settings table does not exist</p>\n";
        
        // Create the settings table
        $pdo->exec("
            CREATE TABLE settings (
                id INT PRIMARY KEY AUTO_INCREMENT,
                setting_key VARCHAR(100) UNIQUE,
                setting_value TEXT,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
            )
        ");
        
        echo "<p>✅ Settings table created</p>\n";
    } else {
        echo "<p>✅ Settings table exists</p>\n";
    }
    
    // Check for required bank transaction SMS settings
    $required_settings = [
        'non_musking_enabled',
        'sms_send_bank_transaction_notifications',
        'non_musking_api_key',
        'non_musking_api_url',
        'non_musking_sender_id'
    ];
    
    echo "<h3>Current Settings Status:</h3>\n";
    echo "<table border='1' cellpadding='5' cellspacing='0'>\n";
    echo "<tr><th>Setting Key</th><th>Value</th><th>Status</th></tr>\n";
    
    $missing_settings = [];
    foreach ($required_settings as $setting) {
        $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
        $stmt->execute([$setting]);
        $result = $stmt->fetch();
        
        if ($result) {
            $value = $result['setting_value'];
            $status = '✅ Found';
            
            // Special handling for boolean values
            if ($setting === 'non_musking_enabled' || $setting === 'sms_send_bank_transaction_notifications') {
                if ($value === '1' || $value === 'true' || $value === 1) {
                    $status = '✅ Enabled';
                } else {
                    $status = '❌ Disabled';
                    if ($setting === 'sms_send_bank_transaction_notifications') {
                        $missing_settings[] = $setting;
                    }
                }
            }
        } else {
            $value = 'NOT FOUND';
            $status = '❌ Missing';
            $missing_settings[] = $setting;
        }
        
        echo "<tr><td>{$setting}</td><td>" . htmlspecialchars($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' || $result['setting_value'] === 'true' || $result['setting_value'] === 1)) {
        echo "<p><strong>✅ Bank transaction notifications are ENABLED</strong></p>\n";
    } else {
        echo "<p><strong>❌ Bank transaction notifications are DISABLED or MISSING</strong></p>\n";
        
        // Add the missing setting to enable bank transaction notifications
        $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();
        
        echo "<p>✅ Bank transaction notification setting has been added/enabled!</p>\n";
    }
    
    // Also add the template settings if they don't exist
    $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><strong>✅ Bank transaction SMS templates have been added!</strong></p>\n";
    
    // Final verification
    echo "<h3>Final 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' || $result['setting_value'] === 'true' || $result['setting_value'] === 1)) {
        echo "<p><strong>✅ SUCCESS: Bank transaction notifications are now ENABLED</strong></p>\n";
        echo "<p><strong>🎉 Bank transaction SMS notifications should now work when transactions are verified!</strong></p>\n";
    } else {
        echo "<p><strong>❌ FAILED: Could not enable bank transaction notifications</strong></p>\n";
    }

} catch (PDOException $e) {
    echo "<p>❌ Database connection error: " . htmlspecialchars($e->getMessage()) . "</p>\n";
}
?>