<?php
// Setup script to ensure bank transaction SMS notifications are enabled
// This script bypasses the HTTP_HOST requirement

// Define minimal constants to avoid errors
if (!defined('BASE_URL')) {
    define('BASE_URL', 'http://localhost/bidda');
}

// Database configuration
$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,
        PDO::ATTR_TIMEOUT => 30
    ]);

    echo "<h3>Setting up Bank Transaction SMS Notifications</h3>\n";

    // 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 enabled</p>\n";

    // Add/update bank transaction template (using non-musking template key format)
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('non_musking_templates_bank_transaction', 'ব্যাংক ট্রানজেকশন পেমেন্ট রিসিভড {paid_amount} টাকা। বকেয়া: {due_amount} টাকা।') ON DUPLICATE KEY UPDATE setting_value = 'ব্যাংক ট্রানজেকশন পেমেন্ট রিসিভড {paid_amount} টাকা। বকেয়া: {due_amount} টাকা।'");
    $stmt->execute();
    echo "<p>✅ Bank transaction template added</p>\n";

    // Add/update bank transaction rejection template (using non-musking template key format)
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('non_musking_templates_bank_transaction_rejection', 'ব্যাংক ট্রানজেকশন প্রত্যাখ্যান করা হয়েছে। পরিমাণ: {amount} টাকা। কারণ: {notes}') ON DUPLICATE KEY UPDATE setting_value = 'ব্যাংক ট্রানজেকশন প্রত্যাখ্যান করা হয়েছে। পরিমাণ: {amount} টাকা। কারণ: {notes}'");
    $stmt->execute();
    echo "<p>✅ Bank transaction rejection template added</p>\n";

    // Also ensure non-musking is enabled
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('non_musking_enabled', '1') ON DUPLICATE KEY UPDATE setting_value = '1'");
    $stmt->execute();
    echo "<p>✅ Non-musking service enabled</p>\n";

    // Update the fee payment template to ensure it exists
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('non_musking_templates_fee_payment', 'প্রিয় {name}, আপনার ফি পেমেন্ট অফ {paid_amount} টাকা গ্রহণ করা হয়েছে। বকেয়া: {due_amount} টাকা। ধন্যবাদ।') ON DUPLICATE KEY UPDATE setting_value = 'প্রিয় {name}, আপনার ফি পেমেন্ট অফ {paid_amount} টাকা গ্রহণ করা হয়েছে। বকেয়া: {due_amount} টাকা। ধন্যবাদ।'");
    $stmt->execute();
    echo "<p>✅ Fee payment template updated</p>\n";

    // Verify the settings were added
    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>Setup completed. Bank transaction SMS notifications should now work when transactions are verified.</strong></p>\n";

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