<?php
/**
 * Test script for the ticketing system
 * This script tests basic functionality of the ticketing system
 */

require_once '../includes/config.php';
require_once '../includes/db.php';
require_once '../includes/ticket_functions.php';

echo "<h1>Ticketing System Test</h1>\n";

// Test 1: Get ticket types
echo "<h2>Test 1: Get Ticket Types</h2>\n";
$ticket_types = getTicketTypes($pdo);
if (!empty($ticket_types)) {
    echo "<p>✓ Successfully retrieved " . count($ticket_types) . " ticket types</p>\n";
    foreach ($ticket_types as $type) {
        echo "<p>- " . htmlspecialchars($type['name']) . "</p>\n";
    }
} else {
    echo "<p>✗ Failed to retrieve ticket types</p>\n";
}

// Test 2: Create a test ticket (if we have a user)
echo "<h2>Test 2: Create Test Ticket</h2>\n";
$stmt = $pdo->query("SELECT id FROM users LIMIT 1");
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if ($user) {
    $ticket_id = createTicket($pdo, $user['id'], null, 1, "Test Ticket", "This is a test ticket for system verification.", "Medium");
    if ($ticket_id) {
        echo "<p>✓ Successfully created test ticket #{$ticket_id}</p>\n";
        
        // Test 3: Retrieve the ticket
        echo "<h2>Test 3: Retrieve Test Ticket</h2>\n";
        $ticket = getTicketById($pdo, $ticket_id);
        if ($ticket) {
            echo "<p>✓ Successfully retrieved ticket #{$ticket_id}</p>\n";
            echo "<p>Subject: " . htmlspecialchars($ticket['subject']) . "</p>\n";
            echo "<p>Status: " . htmlspecialchars($ticket['status']) . "</p>\n";
        } else {
            echo "<p>✗ Failed to retrieve ticket #{$ticket_id}</p>\n";
        }
        
        // Test 4: Update ticket status
        echo "<h2>Test 4: Update Ticket Status</h2>\n";
        if (updateTicketStatus($pdo, $ticket_id, 'In Review')) {
            echo "<p>✓ Successfully updated ticket status to 'In Review'</p>\n";
            
            // Verify status update
            $updated_ticket = getTicketById($pdo, $ticket_id);
            if ($updated_ticket && $updated_ticket['status'] === 'In Review') {
                echo "<p>✓ Status update verified</p>\n";
            } else {
                echo "<p>✗ Status update verification failed</p>\n";
            }
        } else {
            echo "<p>✗ Failed to update ticket status</p>\n";
        }
        
        // Test 5: Add a reply
        echo "<h2>Test 5: Add Ticket Reply</h2>\n";
        if (addTicketReply($pdo, $ticket_id, $user['id'], "This is a test reply.", 0)) {
            echo "<p>✓ Successfully added ticket reply</p>\n";
            
            // Verify reply
            $replies = getTicketReplies($pdo, $ticket_id);
            if (!empty($replies)) {
                echo "<p>✓ Reply verification successful</p>\n";
            } else {
                echo "<p>✗ Reply verification failed</p>\n";
            }
        } else {
            echo "<p>✗ Failed to add ticket reply</p>\n";
        }
    } else {
        echo "<p>✗ Failed to create test ticket</p>\n";
    }
} else {
    echo "<p>✗ No users found in database. Cannot create test ticket.</p>\n";
}

// Test 6: Get ticket statistics
echo "<h2>Test 6: Get Ticket Statistics</h2>\n";
$stats = getTicketStats($pdo);
if ($stats) {
    echo "<p>✓ Successfully retrieved ticket statistics</p>\n";
    echo "<p>Total tickets: " . $stats['total_tickets'] . "</p>\n";
    echo "<p>Average resolution time: " . $stats['avg_resolution_time'] . " hours</p>\n";
} else {
    echo "<p>✗ Failed to retrieve ticket statistics</p>\n";
}

echo "<h2>Test Complete</h2>\n";
echo "<p>Basic functionality tests completed. Check the database to verify all operations.</p>\n";
?>