<?php
require_once 'config/database.php';

echo "Checking SMS and Fee Notification Settings:\n";
echo "==========================================\n\n";

// Check if settings table exists
$stmt = $pdo->prepare("SHOW TABLES LIKE 'settings'");
$stmt->execute();
$tableExists = $stmt->fetch();

if (!$tableExists) {
    echo "ERROR: Settings table does not exist!\n";
    exit(1);
}

// Check required settings
$requiredSettings = [
    'non_musking_enabled' => 'Non-Musking SMS Service',
    'sms_send_fee_notifications' => 'Fee Notifications',
    'sms_send_application_confirmation' => 'Application Notifications',
    'sms_send_result_notifications' => 'Result Notifications'
];

foreach ($requiredSettings as $key => $description) {
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
    $stmt->execute([$key]);
    $result = $stmt->fetch();
    
    if ($result) {
        $value = $result['setting_value'];
        $status = $value === '1' ? 'ENABLED' : 'DISABLED';
        echo "$description ($key): $status ($value)\n";
    } else {
        echo "$description ($key): NOT SET\n";
    }
}

echo "\nNon-Musking Configuration:\n";
echo "=========================\n";

$nonMuskingSettings = [
    'non_musking_api_key' => 'API Key',
    'non_musking_sender_id' => 'Sender ID',
    'non_musking_method' => 'Method',
    'non_musking_url' => 'API URL'
];

foreach ($nonMuskingSettings as $key => $description) {
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
    $stmt->execute([$key]);
    $result = $stmt->fetch();
    
    if ($result) {
        $value = $result['setting_value'];
        if ($key === 'non_musking_api_key' && !empty($value)) {
            $value = substr($value, 0, 5) . '...' . substr($value, -3);
        }
        echo "$description ($key): $value\n";
    } else {
        echo "$description ($key): NOT SET\n";
    }
}

echo "\nFee Payment Template:\n";
echo "===================\n";

$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_templates_fee_payment'");
$stmt->execute();
$result = $stmt->fetch();

if ($result) {
    echo "Template: " . $result['setting_value'] . "\n";
} else {
    echo "Template: NOT SET\n";
}

echo "\nSummary:\n";
echo "=======\n";

// Check if fee notifications should work
$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_enabled'");
$stmt->execute();
$nonMuskingEnabled = $stmt->fetch();
$nonMuskingEnabled = $nonMuskingEnabled && $nonMuskingEnabled['setting_value'] === '1';

$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_send_fee_notifications'");
$stmt->execute();
$feeNotificationsEnabled = $stmt->fetch();
$feeNotificationsEnabled = $feeNotificationsEnabled && $feeNotificationsEnabled['setting_value'] === '1';

if ($nonMuskingEnabled && $feeNotificationsEnabled) {
    echo "✅ Fee notifications should be working!\n";
} else {
    echo "❌ Fee notifications are NOT working because:\n";
    if (!$nonMuskingEnabled) {
        echo "   - Non-Musking SMS service is disabled\n";
    }
    if (!$feeNotificationsEnabled) {
        echo "   - Fee notifications are disabled\n";
    }
}
?>