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

echo "<!DOCTYPE html>
<html lang='bn'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Email Validation টেস্ট</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'>
</head>
<body class='bg-light'>
    <div class='container mt-5'>
        <div class='row justify-content-center'>
            <div class='col-md-10'>
                <div class='card'>
                    <div class='card-header bg-info text-white'>
                        <h4><i class='fas fa-envelope'></i> Email Validation টেস্ট</h4>
                    </div>
                    <div class='card-body'>";

try {
    echo "<h5><i class='fas fa-vial'></i> Email Validation Functions টেস্ট:</h5>";
    
    // Test email format validation
    echo "<h6>1. Email Format Validation:</h6>";
    $test_emails = [
        'valid@example.com' => true,
        'invalid-email' => false,
        'test@domain' => false,
        'user@domain.com' => true,
        '' => true, // Empty is allowed
        'test.email@domain.co.uk' => true
    ];
    
    echo "<div class='table-responsive'>";
    echo "<table class='table table-sm'>";
    echo "<thead><tr><th>Email</th><th>Expected</th><th>Result</th><th>Status</th></tr></thead>";
    echo "<tbody>";
    
    foreach ($test_emails as $email => $expected) {
        $result = isValidEmail($email);
        $status = ($result === $expected) ? 'success' : 'danger';
        $status_icon = ($result === $expected) ? 'check' : 'times';
        
        echo "<tr>";
        echo "<td>" . ($email ?: '[Empty]') . "</td>";
        echo "<td>" . ($expected ? 'Valid' : 'Invalid') . "</td>";
        echo "<td>" . ($result ? 'Valid' : 'Invalid') . "</td>";
        echo "<td><i class='fas fa-$status_icon text-$status'></i></td>";
        echo "</tr>";
    }
    
    echo "</tbody></table>";
    echo "</div>";
    
    // Test email existence check
    echo "<hr>";
    echo "<h6>2. Email Existence Check:</h6>";
    
    // Get some existing emails
    $existing_emails = [];
    $tables = ['users', 'students', 'teachers'];
    
    foreach ($tables as $table) {
        try {
            $stmt = $pdo->query("SELECT email FROM $table WHERE email IS NOT NULL AND email != '' LIMIT 2");
            $emails = $stmt->fetchAll(PDO::FETCH_COLUMN);
            foreach ($emails as $email) {
                $existing_emails[$table][] = $email;
            }
        } catch (Exception $e) {
            // Table might not exist
        }
    }
    
    if (!empty($existing_emails)) {
        echo "<div class='alert alert-info'>";
        echo "<h6>বিদ্যমান Emails:</h6>";
        foreach ($existing_emails as $table => $emails) {
            echo "<strong>$table:</strong> " . implode(', ', $emails) . "<br>";
        }
        echo "</div>";
        
        // Test with existing email
        $test_email = reset($existing_emails)[0] ?? null;
        if ($test_email) {
            $check_result = checkEmailExists($pdo, $test_email);
            if ($check_result) {
                echo "<div class='alert alert-success'>";
                echo "<i class='fas fa-check'></i> Email '$test_email' পাওয়া গেছে " . $check_result['table'] . " টেবিলে";
                echo "</div>";
            } else {
                echo "<div class='alert alert-warning'>";
                echo "<i class='fas fa-exclamation-triangle'></i> Email '$test_email' পাওয়া যায়নি (Function error?)";
                echo "</div>";
            }
        }
    } else {
        echo "<div class='alert alert-info'>";
        echo "<i class='fas fa-info-circle'></i> কোন email পাওয়া যায়নি টেস্ট করার জন্য";
        echo "</div>";
    }
    
    // Test unique email generation
    echo "<hr>";
    echo "<h6>3. Unique Email Generation:</h6>";
    
    $base_email = 'test@example.com';
    $unique_email = generateUniqueEmail($pdo, $base_email);
    
    echo "<div class='alert alert-info'>";
    echo "<strong>Base Email:</strong> $base_email<br>";
    echo "<strong>Generated Unique Email:</strong> $unique_email";
    echo "</div>";
    
    // Test with existing email if available
    if (!empty($existing_emails)) {
        $existing_email = reset($existing_emails)[0] ?? null;
        if ($existing_email) {
            $unique_from_existing = generateUniqueEmail($pdo, $existing_email);
            echo "<div class='alert alert-warning'>";
            echo "<strong>Existing Email:</strong> $existing_email<br>";
            echo "<strong>Generated Unique:</strong> $unique_from_existing";
            echo "</div>";
        }
    }
    
    // Database structure check
    echo "<hr>";
    echo "<h5><i class='fas fa-database'></i> Database Structure Check:</h5>";
    
    foreach ($tables as $table) {
        try {
            $table_exists = $pdo->query("SHOW TABLES LIKE '$table'")->fetch();
            if (!$table_exists) {
                echo "<div class='alert alert-warning'>";
                echo "<i class='fas fa-exclamation-triangle'></i> $table টেবিল পাওয়া যায়নি";
                echo "</div>";
                continue;
            }
            
            // Check email column
            $columns = $pdo->query("DESCRIBE $table")->fetchAll();
            $email_column = null;
            foreach ($columns as $col) {
                if ($col['Field'] == 'email') {
                    $email_column = $col;
                    break;
                }
            }
            
            if ($email_column) {
                $nullable = ($email_column['Null'] == 'YES') ? 'YES' : 'NO';
                $default = $email_column['Default'] ?? 'NULL';
                
                echo "<div class='alert alert-success'>";
                echo "<i class='fas fa-check'></i> <strong>$table:</strong> ";
                echo "Type: " . $email_column['Type'] . ", ";
                echo "Nullable: $nullable, ";
                echo "Default: $default";
                echo "</div>";
                
                // Check for unique constraints
                $indexes = $pdo->query("SHOW INDEX FROM $table WHERE Column_name = 'email'")->fetchAll();
                if (!empty($indexes)) {
                    foreach ($indexes as $index) {
                        $unique = ($index['Non_unique'] == 0) ? 'UNIQUE' : 'INDEX';
                        echo "<div class='alert alert-info'>";
                        echo "<i class='fas fa-key'></i> $table has $unique constraint: " . $index['Key_name'];
                        echo "</div>";
                    }
                }
            } else {
                echo "<div class='alert alert-warning'>";
                echo "<i class='fas fa-exclamation-triangle'></i> $table টেবিলে email কলাম নেই";
                echo "</div>";
            }
            
        } catch (Exception $e) {
            echo "<div class='alert alert-danger'>";
            echo "<i class='fas fa-times'></i> $table চেক করতে সমস্যা: " . $e->getMessage();
            echo "</div>";
        }
    }
    
    // Summary
    echo "<hr>";
    echo "<div class='alert alert-success'>";
    echo "<h6><i class='fas fa-thumbs-up'></i> টেস্ট সম্পন্ন!</h6>";
    echo "<p>Email validation functions কাজ করছে। এখন আপনি নিরাপদে email ব্যবহার করতে পারেন।</p>";
    echo "</div>";
    
} catch (Exception $e) {
    echo "<div class='alert alert-danger'>";
    echo "<h5><i class='fas fa-exclamation-triangle'></i> ত্রুটি!</h5>";
    echo "<p>" . $e->getMessage() . "</p>";
    echo "</div>";
}

echo "                    </div>
                    <div class='card-footer'>
                        <div class='row'>
                            <div class='col-md-3'>
                                <a href='students/add.php' class='btn btn-primary w-100'>
                                    <i class='fas fa-user-plus'></i> শিক্ষার্থী যোগ
                                </a>
                            </div>
                            <div class='col-md-3'>
                                <a href='teachers/add.php' class='btn btn-success w-100'>
                                    <i class='fas fa-chalkboard-teacher'></i> শিক্ষক যোগ
                                </a>
                            </div>
                            <div class='col-md-3'>
                                <a href='fix_email_duplicate_issue.php' class='btn btn-warning w-100'>
                                    <i class='fas fa-tools'></i> Email ফিক্স
                                </a>
                            </div>
                            <div class='col-md-3'>
                                <a href='index.php' class='btn btn-secondary w-100'>
                                    <i class='fas fa-home'></i> হোমে ফিরুন
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>";
?>