<?php
// File: /artist/ticket_view.php
include 'header.php'; // Includes security check and DB connection

if (!isset($_GET['id'])) {
    header('Location: tickets.php');
    exit();
}

$ticket_id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);
$user_id = $_SESSION['user_id'];
$ticket = null;
$replies = [];

// Fetch the main ticket details, ensuring it belongs to the current user
$stmt = $conn->prepare("SELECT * FROM support_tickets WHERE id = ? AND user_id = ?");
$stmt->bind_param("ii", $ticket_id, $user_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
    $ticket = $result->fetch_assoc();
} else {
    // Ticket not found or doesn't belong to the user
    header('Location: tickets.php?error=not_found');
    exit();
}
$stmt->close();

// Fetch all replies for this ticket, joining with users to get the replier's name
$stmt_replies = $conn->prepare("
    SELECT tr.*, u.username 
    FROM ticket_replies tr 
    JOIN users u ON tr.user_id = u.id 
    WHERE tr.ticket_id = ? 
    ORDER BY tr.created_at ASC
");
$stmt_replies->bind_param("i", $ticket_id);
$stmt_replies->execute();
$replies_result = $stmt_replies->get_result();
if ($replies_result) {
    $replies = $replies_result->fetch_all(MYSQLI_ASSOC);
}
$stmt_replies->close();
?>

<div class="p-6 md:p-8">
    <div class="max-w-4xl mx-auto">
        <div class="flex items-center mb-6">
            <a href="tickets.php" class="text-ucf-green hover:text-ucf-green-dark mr-4">
                <i class="fas fa-arrow-left fa-lg"></i>
            </a>
            <div>
                <h1 class="text-3xl font-bold text-ucf-charcoal">Ticket #<?php echo $ticket['id']; ?></h1>
                <p class="text-gray-500"><?php echo htmlspecialchars($ticket['subject']); ?></p>
            </div>
        </div>

        <div class="bg-white rounded-lg shadow-md">
            <!-- Original Ticket Message -->
            <div class="p-6 border-b border-gray-200">
                <div class="flex items-start space-x-4">
                    <div class="flex-shrink-0">
                        <i class="fas fa-user-circle fa-2x text-gray-400"></i>
                    </div>
                    <div class="flex-1">
                        <p class="font-bold text-ucf-charcoal"><?php echo htmlspecialchars($_SESSION['username']); ?></p>
                        <p class="text-sm text-gray-500 mb-2">Posted on <?php echo date('M d, Y, h:i A', strtotime($ticket['created_at'])); ?></p>
                        <div class="prose max-w-none">
                            <p><?php echo nl2br(htmlspecialchars($ticket['message'])); ?></p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Replies -->
            <?php foreach ($replies as $reply): ?>
                <div class="p-6 border-b border-gray-200 <?php echo ($reply['user_id'] == $user_id) ? 'bg-gray-50' : ''; ?>">
                    <div class="flex items-start space-x-4">
                        <div class="flex-shrink-0">
                            <i class="fas fa-user-circle fa-2x <?php echo ($reply['user_id'] != $user_id) ? 'text-ucf-green' : 'text-gray-400'; ?>"></i>
                        </div>
                        <div class="flex-1">
                            <p class="font-bold text-ucf-charcoal"><?php echo htmlspecialchars($reply['username']); ?></p>
                            <p class="text-sm text-gray-500 mb-2">Replied on <?php echo date('M d, Y, h:i A', strtotime($reply['created_at'])); ?></p>
                            <div class="prose max-w-none">
                                <p><?php echo nl2br(htmlspecialchars($reply['message'])); ?></p>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>

            <!-- Add Reply Form -->
            <div class="p-6 bg-gray-50 rounded-b-lg">
                <h3 class="text-xl font-bold text-ucf-charcoal mb-4">Add a Reply</h3>
                <form action="ticket_handler.php" method="POST">
                    <input type="hidden" name="ticket_id" value="<?php echo $ticket['id']; ?>">
                    <div>
                        <textarea name="message" rows="5" required class="w-full rounded-md border-gray-300 shadow-sm" placeholder="Type your reply here..."></textarea>
                    </div>
                    <div class="mt-4 flex justify-end">
                        <button type="submit" name="add_reply" class="bg-ucf-green text-white font-bold py-2 px-6 rounded-lg hover:bg-ucf-green-dark">
                            Submit Reply
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
