<?php
// Test the getRecentTickets function directly
require_once 'includes/config.php';
require_once 'includes/db.php';
require_once 'includes/ticket_functions.php';

if (!$pdo) {
    die("Database connection failed");
}

echo "Testing getRecentTickets function:\n";

try {
    $recent_tickets = getRecentTickets($pdo, 10);
    echo "Number of recent tickets returned: " . count($recent_tickets) . "\n";
    
    if (!empty($recent_tickets)) {
        echo "Recent tickets:\n";
        foreach ($recent_tickets as $ticket) {
            echo "- Ticket #" . $ticket['id'] . ": " . $ticket['subject'] . " (" . $ticket['status'] . ")\n";
            echo "  Customer: " . ($ticket['customer_name'] ?? 'N/A') . "\n";
            echo "  Type: " . ($ticket['type_name'] ?? 'N/A') . "\n";
        }
    } else {
        echo "No recent tickets returned by function\n";
        
        // Let's try a direct query to compare
        echo "\nTrying direct query:\n";
        $stmt = $pdo->query("SELECT t.*, tt.name as type_name, u.username as customer_name FROM tickets t LEFT JOIN ticket_types tt ON t.type_id = tt.id LEFT JOIN users u ON t.user_id = u.id ORDER BY t.created_at DESC LIMIT 10");
        $direct_tickets = $stmt->fetchAll(PDO::FETCH_ASSOC);
        echo "Direct query returned: " . count($direct_tickets) . " tickets\n";
        
        if (!empty($direct_tickets)) {
            foreach ($direct_tickets as $ticket) {
                echo "- Ticket #" . $ticket['id'] . ": " . $ticket['subject'] . " (" . $ticket['status'] . ")\n";
                echo "  Customer: " . ($ticket['customer_name'] ?? 'N/A') . "\n";
                echo "  Type: " . ($ticket['type_name'] ?? 'N/A') . "\n";
            }
        }
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>