<?php
session_start();
require_once '../config/database.php';
require_once '../includes/sms_helper.php';

// Check if user is logged in and is admin
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'admin') {
    header('Location: ../login.php');
    exit();
}

// Get site settings
$settings = get_all_settings($pdo);
$site_name = $settings['site_name'] ?? 'বিদ্যা নিকেতন';

$message_sent = false;
$test_result = null;

// Handle test SMS submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['send_test_sms'])) {
    $phone_number = trim($_POST['phone_number']);
    $test_message = trim($_POST['test_message']);
    
    if (!empty($phone_number) && !empty($test_message)) {
        // Send test SMS
        $test_result = non_musking_send($pdo, [$phone_number], $test_message, [
            'message_type' => 'test',
            'recipient_name' => 'Test User'
        ]);
        
        $message_sent = true;
    }
}

// Load current non-masking configuration
$config = non_musking_config();
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>নন-মাস্কিং SMS টেস্ট - <?php echo htmlspecialchars($site_name); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body { 
            font-family: 'Hind Siliguri', sans-serif;
            background: #f5f7fa; 
        }
        
        .admin-header {
            background: linear-gradient(135deg, #006747 0%, #004d35 100%);
            color: #fff;
            padding: 1.5rem 0;
            margin-bottom: 2rem;
            border-bottom: 3px solid #F42A41;
        }
        
        .test-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,103,71,0.15);
            border: 1px solid #e0e0e0;
            margin-bottom: 2rem;
        }
        
        .form-label {
            font-weight: 600;
            color: #006747;
        }
        
        .btn-test {
            background: linear-gradient(135deg, #006747 0%, #004d35 100%);
            border: none;
            font-weight: 600;
            padding: 12px 30px;
        }
        
        .btn-test:hover {
            background: linear-gradient(135deg, #004d35 0%, #006747 100%);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 103, 71, 0.4);
        }
        
        .section-title {
            color: #006747;
            border-bottom: 2px solid #F42A41;
            padding-bottom: 10px;
            margin-bottom: 20px;
            font-weight: 700;
        }
        
        .config-status {
            background: #e8f5e8;
            border-left: 4px solid #006747;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 0 5px 5px 0;
        }
        
        .config-warning {
            background: #fff3cd;
            border-left: 4px solid #ffc107;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 0 5px 5px 0;
        }
        
        .result-success {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            padding: 15px;
            border-radius: 5px;
            margin-top: 15px;
        }
        
        .result-error {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            padding: 15px;
            border-radius: 5px;
            margin-top: 15px;
        }
        
        .form-control:focus,
        .form-select:focus {
            border-color: #006747;
            box-shadow: 0 0 0 0.25rem rgba(0, 103, 71, 0.15);
        }
    </style>
</head>
<body>
    <!-- Header -->
    <div class="admin-header">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <h1><i class="fas fa-comment-alt"></i> নন-মাস্কিং SMS টেস্ট</h1>
                </div>
                <div class="col-md-4 text-end">
                    <a href="dashboard.php" class="btn btn-outline-light">
                        <i class="fas fa-arrow-left"></i> ড্যাশবোর্ডে ফিরে যান
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="container">
        <!-- Configuration Status -->
        <div class="test-card">
            <div class="card-body p-4">
                <h4 class="section-title"><i class="fas fa-cog"></i> কনফিগারেশন স্ট্যাটাস</h4>
                
                <?php if ($config['enabled']): ?>
                    <div class="config-status">
                        <i class="fas fa-check-circle text-success"></i> 
                        <strong>নন-মাস্কিং SMS সক্রিয় রয়েছে</strong>
                    </div>
                <?php else: ?>
                    <div class="config-warning">
                        <i class="fas fa-exclamation-triangle text-warning"></i> 
                        <strong>নন-মাস্কিং SMS নিষ্ক্রিয় রয়েছে</strong> - সেটিংস থেকে সক্রিয় করুন
                    </div>
                <?php endif; ?>
                
                <div class="row">
                    <div class="col-md-6">
                        <p><strong>API URL:</strong> <?php echo !empty($config['api_url']) ? htmlspecialchars($config['api_url']) : '<span class="text-danger">কনফিগার করা হয়নি</span>'; ?></p>
                        <p><strong>সেন্ডার ID:</strong> <?php echo htmlspecialchars($config['sender_id'] ?? 'SCHOOL'); ?></p>
                    </div>
                    <div class="col-md-6">
                        <p><strong>ইউজারনেম:</strong> <?php echo !empty($config['username']) ? 'SET' : '<span class="text-danger">কনফিগার করা হয়নি</span>'; ?></p>
                        <p><strong>পাসওয়ার্ড:</strong> <?php echo !empty($config['password']) ? 'SET' : '<span class="text-danger">কনফিগার করা হয়নি</span>'; ?></p>
                    </div>
                </div>
                
                <a href="sms_settings.php" class="btn btn-sm btn-outline-primary">
                    <i class="fas fa-cog"></i> সেটিংস সম্পাদনা করুন
                </a>
            </div>
        </div>

        <!-- Test SMS Form -->
        <div class="test-card">
            <div class="card-body p-4">
                <h4 class="section-title"><i class="fas fa-paper-plane"></i> টেস্ট SMS পাঠান</h4>
                
                <form method="POST" action="">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label for="phone_number" class="form-label">ফোন নম্বর *</label>
                            <input type="tel" class="form-control" id="phone_number" name="phone_number" 
                                   placeholder="01712345678 বা +8801712345678" required>
                            <div class="form-text">ফোন নম্বর বাংলাদেশি ফরম্যাটে লিখুন</div>
                        </div>
                        
                        <div class="col-md-6">
                            <label for="test_message" class="form-label">টেস্ট মেসেজ *</label>
                            <textarea class="form-control" id="test_message" name="test_message" rows="4" 
                                      placeholder="টেস্ট মেসেজ লিখুন..." required>এটি একটি টেস্ট মেসেজ। নন-মাস্কিং SMS সফলভাবে কাজ করছে।</textarea>
                        </div>
                    </div>
                    
                    <div class="mt-4">
                        <button type="submit" name="send_test_sms" class="btn btn-test">
                            <i class="fas fa-paper-plane"></i> টেস্ট SMS পাঠান
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Test Results -->
        <?php if ($message_sent && $test_result): ?>
            <div class="test-card">
                <div class="card-body p-4">
                    <h4 class="section-title"><i class="fas fa-chart-bar"></i> টেস্ট রেজাল্ট</h4>
                    
                    <?php if ($test_result['success']): ?>
                        <div class="result-success">
                            <h5><i class="fas fa-check-circle text-success"></i> সফল!</h5>
                            <p><strong>মেসেজ পাঠানো হয়েছে:</strong> <?php echo htmlspecialchars(implode(', ', $test_result['to'])); ?></p>
                            <p><strong>মোট প্রাপক:</strong> <?php echo $test_result['total_count']; ?></p>
                            <?php if (isset($test_result['raw_response'])): ?>
                                <p><strong>API রেসপন্স:</strong> <?php echo htmlspecialchars(substr(json_encode($test_result['raw_response'], JSON_UNESCAPED_UNICODE), 0, 200)) . '...'; ?></p>
                            <?php endif; ?>
                        </div>
                    <?php else: ?>
                        <div class="result-error">
                            <h5><i class="fas fa-times-circle text-danger"></i> ব্যর্থ!</h5>
                            <p><strong>ত্রুটি:</strong> <?php echo htmlspecialchars($test_result['message']); ?></p>
                            <?php if (isset($test_result['raw_response'])): ?>
                                <p><strong>API রেসপন্স:</strong> <?php echo htmlspecialchars(json_encode($test_result['raw_response'], JSON_UNESCAPED_UNICODE)); ?></p>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        <?php endif; ?>

        <!-- Quick Links -->
        <div class="test-card">
            <div class="card-body p-4">
                <h4 class="section-title"><i class="fas fa-link"></i> দ্রুত লিঙ্ক</h4>
                <div class="row">
                    <div class="col-md-4">
                        <a href="sms_settings.php" class="btn btn-outline-primary w-100 mb-2">
                            <i class="fas fa-cog"></i> SMS সেটিংস
                        </a>
                    </div>
                    <div class="col-md-4">
                        <a href="send_non_musking_message.php" class="btn btn-outline-success w-100 mb-2">
                            <i class="fas fa-comment-alt"></i> মেসেজ পাঠান
                        </a>
                    </div>
                    <div class="col-md-4">
                        <a href="message_logs.php" class="btn btn-outline-info w-100 mb-2">
                            <i class="fas fa-history"></i> মেসেজ লগ
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Auto-format phone number
        document.getElementById('phone_number').addEventListener('input', function(e) {
            let value = e.target.value.replace(/\D/g, '');
            if (value.length > 11) value = value.substring(0, 11);
            e.target.value = value;
        });
    </script>
</body>
</html>