<?php
/**
 * Test Registration SMS - Debug Tool
 * This file helps debug the registration SMS functionality
 */

// Include the system files
require_once 'init.php';

// Simple admin check - just check if config exists
if (!file_exists('config.php')) {
    die("Configuration file not found");
}

// Get configuration
$config = ORM::for_table('tbl_appconfig')->find_many();
foreach ($config as $c) {
    $config[$c['setting']] = $c['value'];
}

echo "<h2>Registration SMS Debug Information</h2>";

// Check configuration
echo "<h3>Configuration Check:</h3>";
echo "<strong>user_notification_payment:</strong> " . ($config['user_notification_payment'] ?? 'Not set') . "<br>";
echo "<strong>SMS URL configured:</strong> " . (!empty($config['sms_url']) ? 'Yes' : 'No') . "<br>";
echo "<strong>Company Name:</strong> " . ($config['CompanyName'] ?? 'Not set') . "<br>";

// Check template
echo "<h3>Template Check:</h3>";
$template = Lang::getNotifText('user_registration');
if ($template) {
    echo "<strong>Template loaded:</strong> Yes<br>";
    echo "<strong>Template preview:</strong><br>";
    echo "<pre>" . htmlspecialchars(substr($template, 0, 200)) . "...</pre>";
} else {
    echo "<strong>Template loaded:</strong> No<br>";
}

// Test SMS function
echo "<h3>Test SMS Function:</h3>";
if (isset($_POST['test_sms'])) {
    $test_phone = $_POST['test_phone'];
    $test_name = $_POST['test_name'];
    
    if (!empty($test_phone) && !empty($test_name)) {
        $testCustomerData = [
            'fullname' => $test_name,
            'username' => 'testuser',
            'password' => 'testpass',
            'phonenumber' => $test_phone,
            'service_type' => 'Hotspot'
        ];
        
        echo "<strong>Testing SMS for:</strong> " . $test_name . " (" . $test_phone . ")<br>";
        
        $result = Message::sendRegistrationNotification($testCustomerData);
        
        if ($result) {
            echo "<strong>Result:</strong> SMS sent successfully<br>";
            echo "<strong>Message:</strong><br>";
            echo "<pre>" . htmlspecialchars($result) . "</pre>";
        } else {
            echo "<strong>Result:</strong> SMS failed or no result returned<br>";
        }
    } else {
        echo "<strong>Error:</strong> Please fill in both phone number and name<br>";
    }
}

// Test form
echo "<h3>Test SMS Form:</h3>";
echo "<form method='post'>";
echo "Phone Number: <input type='text' name='test_phone' placeholder='254712345678' required><br><br>";
echo "Full Name: <input type='text' name='test_name' placeholder='John Doe' required><br><br>";
echo "<input type='submit' name='test_sms' value='Send Test SMS'>";
echo "</form>";

// Check recent logs
echo "<h3>Recent Logs:</h3>";
$logFile = $root_path . 'system/logs/Registration SMS.log';
if (file_exists($logFile)) {
    $logs = file_get_contents($logFile);
    $recentLogs = array_slice(explode("\n", $logs), -10);
    echo "<pre>" . htmlspecialchars(implode("\n", $recentLogs)) . "</pre>";
} else {
    echo "No log file found at: " . $logFile;
}

echo "<br><br><a href='index.php?_route=customers/add'>Go to Add Customer</a>";
?>
