<?php
require_once 'bulk_upload/encoding_helper.php';

echo "<!DOCTYPE html>";
echo "<html lang='bn'>";
echo "<head>";
echo "<meta charset='UTF-8'>";
echo "<title>বাংলা এনকোডিং টেস্ট</title>";
echo "<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css' rel='stylesheet'>";
echo "<link href='assets/css/style.css' rel='stylesheet'>";
echo "</head>";
echo "<body class='bengali-text bg-light'>";

echo "<div class='container mt-4'>";
echo "<div class='card'>";
echo "<div class='card-header bg-primary text-white'>";
echo "<h3><i class='fas fa-language'></i> বাংলা এনকোডিং টেস্ট</h3>";
echo "</div>";
echo "<div class='card-body'>";

// Test Bengali text display
echo "<h4>বাংলা ফন্ট টেস্ট:</h4>";
echo "<div class='alert alert-info'>";
echo "<h5>নমুনা বাংলা টেক্সট:</h5>";
echo "<p class='bengali-text'>এটি একটি বাংলা টেক্সট টেস্ট। আমরা দেখছি বাংলা ফন্ট সঠিকভাবে প্রদর্শিত হচ্ছে কিনা।</p>";
echo "<p class='bengali-text'><strong>নাম:</strong> মোহাম্মদ রহিম উদ্দিন</p>";
echo "<p class='bengali-text'><strong>ঠিকানা:</strong> ঢাকা, বাংলাদেশ</p>";
echo "<p class='bengali-text'><strong>অভিভাবক:</strong> মোহাম্মদ করিম উদ্দিন</p>";
echo "</div>";

// Test encoding functions
echo "<h4>এনকোডিং ফাংশন টেস্ট:</h4>";

$testTexts = [
    'মোহাম্মদ রহিম উদ্দিন',
    'ফাতিমা আক্তার খাতুন',
    'ঢাকা, বাংলাদেশ',
    'আব্দুল করিম চৌধুরী'
];

echo "<div class='table-responsive'>";
echo "<table class='table table-bordered'>";
echo "<thead class='table-dark'>";
echo "<tr><th>মূল টেক্সট</th><th>ক্লিন টেক্সট</th><th>বাংলা আছে?</th><th>UTF-8?</th></tr>";
echo "</thead>";
echo "<tbody>";

foreach ($testTexts as $text) {
    $cleanText = EncodingHelper::cleanBengaliText($text);
    $hasBengali = EncodingHelper::validateBengaliText($text) ? 'হ্যাঁ' : 'না';
    $isUtf8 = mb_check_encoding($text, 'UTF-8') ? 'হ্যাঁ' : 'না';
    
    echo "<tr>";
    echo "<td class='bengali-text'>$text</td>";
    echo "<td class='bengali-text'>$cleanText</td>";
    echo "<td>$hasBengali</td>";
    echo "<td>$isUtf8</td>";
    echo "</tr>";
}

echo "</tbody>";
echo "</table>";
echo "</div>";

// Test CSV template generation
echo "<h4>CSV টেমপ্লেট টেস্ট:</h4>";

$studentTemplate = generateBengaliCsvTemplate('students');
if ($studentTemplate) {
    echo "<div class='alert alert-success'>";
    echo "<h6>শিক্ষার্থী টেমপ্লেট:</h6>";
    echo "<p><strong>হেডার:</strong> " . implode(', ', $studentTemplate['headers']) . "</p>";
    echo "<p class='bengali-text'><strong>নমুনা ডেটা:</strong> " . implode(', ', $studentTemplate['sample_data'][0]) . "</p>";
    echo "</div>";
}

// Test file encoding detection
echo "<h4>ফাইল এনকোডিং টেস্ট:</h4>";

$testFiles = [
    'bulk_upload/templates/students_template.csv',
    'bulk_upload/templates/teachers_template.csv'
];

echo "<div class='table-responsive'>";
echo "<table class='table table-bordered'>";
echo "<thead class='table-dark'>";
echo "<tr><th>ফাইল</th><th>এনকোডিং</th><th>BOM</th><th>বাংলা</th><th>সাইজ</th></tr>";
echo "</thead>";
echo "<tbody>";

foreach ($testFiles as $file) {
    if (file_exists($file)) {
        $info = EncodingHelper::getFileEncodingInfo($file);
        echo "<tr>";
        echo "<td>" . basename($file) . "</td>";
        echo "<td>" . ($info['detected_encoding'] ?? 'অজানা') . "</td>";
        echo "<td>" . ($info['has_bom'] ? 'হ্যাঁ' : 'না') . "</td>";
        echo "<td>" . ($info['has_bengali'] ? 'হ্যাঁ' : 'না') . "</td>";
        echo "<td>" . round($info['file_size'] / 1024, 2) . " KB</td>";
        echo "</tr>";
    }
}

echo "</tbody>";
echo "</table>";
echo "</div>";

// PHP configuration check
echo "<h4>PHP কনফিগারেশন চেক:</h4>";

$phpInfo = [
    'mbstring extension' => extension_loaded('mbstring') ? 'সক্রিয়' : 'নিষ্ক্রিয়',
    'iconv extension' => extension_loaded('iconv') ? 'সক্রিয়' : 'নিষ্ক্রিয়',
    'default_charset' => ini_get('default_charset'),
    'internal_encoding' => mb_internal_encoding(),
    'detect_order' => mb_detect_order()
];

echo "<div class='table-responsive'>";
echo "<table class='table table-bordered'>";
echo "<thead class='table-dark'>";
echo "<tr><th>সেটিং</th><th>মান</th></tr>";
echo "</thead>";
echo "<tbody>";

foreach ($phpInfo as $setting => $value) {
    echo "<tr>";
    echo "<td>$setting</td>";
    echo "<td>" . (is_array($value) ? implode(', ', $value) : $value) . "</td>";
    echo "</tr>";
}

echo "</tbody>";
echo "</table>";
echo "</div>";

echo "<div class='text-center mt-4'>";
echo "<a href='bulk_upload/index.php' class='btn btn-primary'>";
echo "<i class='fas fa-arrow-left'></i> বাল্ক আপলোড সিস্টেম";
echo "</a>";
echo "</div>";

echo "</div>"; // card-body
echo "</div>"; // card
echo "</div>"; // container

echo "</body>";
echo "</html>";
?>

<style>
.bengali-text {
    font-family: 'Noto Sans Bengali', 'Hind Siliguri', 'SolaimanLipi', 'Kalpurush', sans-serif;
    line-height: 1.7;
}
</style>