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

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

// Test 1: Check if database connection is working
echo "<h2>Test 1: Database Connection</h2>";
if ($pdo) {
    echo "<p style='color: green;'>✓ Database connection successful</p>";
} else {
    echo "<p style='color: red;'>✗ Database connection failed</p>";
    exit;
}

// Test 2: Check if product_comments table exists
echo "<h2>Test 2: product_comments Table</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>";
        echo "<p>Please run create_comments_table_web.php first</p>";
        exit;
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
    exit;
}

// Test 3: Check if required functions exist
echo "<h2>Test 3: Required Functions</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>";
    exit;
}

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>";
    exit;
}

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>";
    exit;
}

// Test 4: Test adding a comment (simulated)
echo "<h2>Test 4: Add Comment Functionality</h2>";
try {
    // Check if we have at least one user and one product
    $stmt = $pdo->query("SELECT id FROM users LIMIT 1");
    $user = $stmt->fetch();
    
    $stmt = $pdo->query("SELECT id FROM products LIMIT 1");
    $product = $stmt->fetch();
    
    if ($user && $product) {
        echo "<p style='color: green;'>✓ Found user and product for testing</p>";
        
        // Simulate adding a comment
        $result = addProductComment($pdo, $product['id'], $user['id'], "This is a test comment for functionality testing.");
        
        if ($result) {
            echo "<p style='color: green;'>✓ addProductComment function works correctly</p>";
        } else {
            echo "<p style='color: red;'>✗ addProductComment function failed</p>";
        }
    } else {
        echo "<p style='color: red;'>✗ No user or product found for testing</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>Error testing addProductComment: " . $e->getMessage() . "</p>";
}

// Test 5: Test retrieving comments
echo "<h2>Test 5: Retrieve Comments</h2>";
try {
    if (isset($product)) {
        $comments = getProductComments($pdo, $product['id']);
        echo "<p style='color: green;'>✓ getProductComments function works correctly, found " . count($comments) . " comments</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>Error testing getProductComments: " . $e->getMessage() . "</p>";
}

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