<?php
require_once './includes/session.php';
require_once './includes/message.php';
require_once './includes/utils.php';

// Initialize session and required classes
$session = SessionManager::getInstance();
$messageObj = new Message();

// Get message ID from URL
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// Redirect if no valid ID
if ($id <= 0) {
    header('Location: messages.php');
    exit;
}

// Get message details
$message = $messageObj->getMessageById($id);

// Redirect if message not found
if (!$message) {
    header('Location: messages.php');
    exit;
}

// Include header
include_once './includes/header.php';
?>

<!-- Page Content -->
<div class="card mb-4">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span>Message Details</span>
        <div>
            <a href="messages.php" class="btn btn-sm btn-secondary">Back to Messages</a>
            <a href="messages.php?action=delete&id=<?php echo $message['id']; ?>" class="btn btn-sm btn-danger confirm-delete">Delete</a>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-4">
            <div class="col-md-6">
                <h5>Basic Information</h5>
                <table class="table table-bordered">
                    <tr>
                        <th width="120px">ID</th>
                        <td><?php echo htmlspecialchars($message['id']); ?></td>
                    </tr>
                     <tr>
                        <th>Type</th>
                        <td><?php echo htmlspecialchars($message['type']); ?></td>
                    </tr>
                    <tr>
                        <th>Title</th>
                        <td><?php echo htmlspecialchars($message['title']); ?></td>
                    </tr>
                    
                    <tr>
                        <th>Mobile</th>
                        <td><?php echo htmlspecialchars($message['mobile']); ?></td>
                    </tr>
                    <tr>
                        <th>Date</th>
                        <td><?php echo Utils::formatDate($message['date'], 'Y-m-d H:i:s'); ?></td>
                    </tr>
                </table>
            </div>

            <?php if (!empty($message['image'])): ?>
            <div class="col-md-6">
                <h5>Image</h5>
                <div class="mb-3">
                    <img src="<?php echo htmlspecialchars($message['image']); ?>" alt="Message Image" class="img-fluid img-thumbnail" style="max-height: 300px;">
                </div>
            </div>
            <?php endif; ?>
        </div>

        <div class="row">
            <div class="col-12">
                <h5>Message Content</h5>
                <div class="p-3 bg-light rounded">
                    <?php echo nl2br(htmlspecialchars($message['subheading'])); ?>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Previous/Next Message Navigation -->
<?php
// Get previous and next message IDs for navigation
$prevMessageId = null;
$nextMessageId = null;

// Optimize query for previous and next by limiting to just ID field
$allMessageIds = $messageObj->getMessages(1, 1000, [], 'id', 'ASC');
if (!empty($allMessageIds)) {
    $currentIndex = -1;

    // Find current message index
    foreach ($allMessageIds as $index => $msgItem) {
        if ($msgItem['id'] == $id) {
            $currentIndex = $index;
            break;
        }
    }

    // Get previous and next IDs if available
    if ($currentIndex > 0) {
        $prevMessageId = $allMessageIds[$currentIndex - 1]['id'];
    }

    if ($currentIndex < count($allMessageIds) - 1) {
        $nextMessageId = $allMessageIds[$currentIndex + 1]['id'];
    }
}
?>

<div class="d-flex justify-content-between mb-4">
    <?php if ($prevMessageId): ?>
        <a href="view_message.php?id=<?php echo $prevMessageId; ?>" class="btn btn-outline-primary">
            <i class="bi bi-arrow-left"></i> Previous Message
        </a>
    <?php else: ?>
        <span></span>
    <?php endif; ?>

    <?php if ($nextMessageId): ?>
        <a href="view_message.php?id=<?php echo $nextMessageId; ?>" class="btn btn-outline-primary">
            Next Message <i class="bi bi-arrow-right"></i>
        </a>
    <?php else: ?>
        <span></span>
    <?php endif; ?>
</div>

<?php
// Include footer
include_once './includes/footer.php';
?>
