<?php
// Test script to verify the comments feature is working
require_once 'includes/db.php';
require_once 'includes/functions.php';

echo "<h1>Testing Comments Feature</h1>";

// Test 1: Check if the product_comments table exists
echo "<h2>Test 1: Checking if product_comments table exists</h2>";
try {
    $stmt = $pdo->query("SHOW TABLES LIKE 'product_comments'");
    $table_exists = $stmt->fetch();
    
    if ($table_exists) {
        echo "<p style='color: green;'>✓ product_comments table exists</p>";
    } else {
        echo "<p style='color: red;'>✗ product_comments table does not exist</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}

// Test 2: Check if required functions exist
echo "<h2>Test 2: Checking if required functions exist</h2>";

if (function_exists('addProductComment')) {
    echo "<p style='color: green;'>✓ addProductComment function exists</p>";
} else {
    echo "<p style='color: red;'>✗ addProductComment function does not exist</p>";
}

if (function_exists('getProductComments')) {
    echo "<p style='color: green;'>✓ getProductComments function exists</p>";
} else {
    echo "<p style='color: red;'>✗ getProductComments function does not exist</p>";
}

if (function_exists('formatTimeAgo')) {
    echo "<p style='color: green;'>✓ formatTimeAgo function exists</p>";
} else {
    echo "<p style='color: red;'>✗ formatTimeAgo function does not exist</p>";
}

// Test 3: Check if AJAX endpoint exists
echo "<h2>Test 3: Checking if AJAX endpoint exists</h2>";
if (file_exists('products/add_comment.php')) {
    echo "<p style='color: green;'>✓ add_comment.php endpoint exists</p>";
} else {
    echo "<p style='color: red;'>✗ add_comment.php endpoint does not exist</p>";
}

// Test 4: Check if CSS styles were added
echo "<h2>Test 4: Checking if CSS styles were added</h2>";
$css_content = file_get_contents('assets/css/style.css');
if (strpos($css_content, 'Product Comments Section') !== false) {
    echo "<p style='color: green;'>✓ Comments CSS styles were added</p>";
} else {
    echo "<p style='color: red;'>✗ Comments CSS styles were not found</p>";
}

echo "<h2>Next Steps:</h2>";
echo "<ol>";
echo "<li>Create a test user by visiting <a href='create_test_user.php'>create_test_user.php</a></li>";
echo "<li>Login with the test user credentials</li>";
echo "<li>Visit <a href='products/view.php?id=2'>Product View Page</a> to test the comments feature</li>";
echo "</ol>";

echo "<p><a href='index.php'>Back to Home</a></p>";
?>