<?php
require_once 'includes/config.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';

echo "<h1>Testing Emoji Support in Comments</h1>";

// Test 1: Check if database connection supports UTF-8
echo "<h2>Test 1: Database Connection Charset</h2>";
try {
    $stmt = $pdo->query("SELECT @@character_set_database, @@collation_database");
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    
    echo "<p>Database Character Set: " . $result['@@character_set_database'] . "</p>";
    echo "<p>Database Collation: " . $result['@@collation_database'] . "</p>";
    
    if ($result['@@character_set_database'] === 'utf8mb4') {
        echo "<p style='color: green;'>✓ Database supports UTF-8 emojis</p>";
    } else {
        echo "<p style='color: red;'>✗ Database does not support UTF-8 emojis</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}

// Test 2: Check if product_comments table supports UTF-8
echo "<h2>Test 2: product_comments Table Charset</h2>";
try {
    $stmt = $pdo->query("SHOW CREATE TABLE product_comments");
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (strpos($result['Create Table'], 'utf8mb4') !== false) {
        echo "<p style='color: green;'>✓ product_comments table supports UTF-8 emojis</p>";
    } else {
        echo "<p style='color: red;'>✗ product_comments table does not support UTF-8 emojis</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}

// Test 3: Add a comment with emojis
echo "<h2>Test 3: Add Comment with Emojis</h2>";
if (isLoggedIn()) {
    try {
        $test_comment = "This is a test comment with emojis: 😊👍🎉🔥💯";
        $result = addProductComment($pdo, 2, $_SESSION['user_id'], $test_comment);
        
        if ($result) {
            echo "<p style='color: green;'>✓ Comment with emojis added successfully</p>";
            
            // Retrieve the comment to verify it was stored correctly
            $stmt = $pdo->prepare("SELECT comment FROM product_comments WHERE product_id = ? AND user_id = ? ORDER BY created_at DESC LIMIT 1");
            $stmt->execute([2, $_SESSION['user_id']]);
            $comment = $stmt->fetch(PDO::FETCH_ASSOC);
            
            if ($comment && $comment['comment'] === $test_comment) {
                echo "<p style='color: green;'>✓ Comment retrieved correctly with emojis: " . htmlspecialchars($comment['comment']) . "</p>";
            } else {
                echo "<p style='color: red;'>✗ Comment not retrieved correctly</p>";
            }
        } else {
            echo "<p style='color: red;'>✗ Failed to add comment with emojis</p>";
        }
    } catch (Exception $e) {
        echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
    }
} else {
    echo "<p>Please <a href='auth/login.php'>login</a> to test adding comments with emojis.</p>";
}

echo "<h2>Test Results Summary</h2>";
echo "<p>If all tests passed, emoji support should work correctly in comments.</p>";
echo "<p><a href='products/view.php?id=2'>View Product with Comments</a></p>";
?>