<?php

require_once __DIR__ . '/../app/auth.php';
require_once __DIR__ . '/../app/layout.php';
require_once __DIR__ . '/../app/contact-messages.php';

$user = sparrow_require_backyard_login();

$filter = trim($_GET['status'] ?? '');

if (!in_array($filter, ['', 'New', 'Read'], true)) {
    $filter = '';
}

$messages = sparrow_get_contact_messages($filter);
$counts = sparrow_get_contact_message_counts();

sparrow_backyard_header('Messages');
?>

<style>
.message-counts,
.message-filters {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.message-count {
    min-width: 90px;
    padding: 10px 14px;
    border: 1px solid var(--sparrow-border);
    border-radius: 14px;
    background: var(--sparrow-soft-pink);
}

.message-count strong {
    display: block;
    color: var(--sparrow-purple-dark);
    font-size: 18px;
}

.message-filters {
    margin-top: 18px;
}

.message-row-new td {
    background: #fff8fc;
    font-weight: bold;
}

.message-subject {
    color: var(--sparrow-purple-dark);
    text-decoration: none;
}

.message-subject:hover {
    text-decoration: underline;
}

.message-status {
    display: inline-block;
    padding: 4px 9px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: bold;
}

.message-status-new {
    color: #864b7b;
    background: #f8e5f3;
    border: 1px solid #ddb6d3;
}

.message-status-read {
    color: #5f6065;
    background: #f2f2f4;
    border: 1px solid #d8d8dc;
}

.message-preview {
    display: block;
    margin-top: 4px;
    color: var(--sparrow-muted);
    font-weight: normal;
}

@media (max-width: 760px) {
    .messages-table thead {
        display: none;
    }

    .messages-table,
    .messages-table tbody,
    .messages-table tr,
    .messages-table td {
        display: block;
        width: 100%;
    }

    .messages-table tr {
        margin-bottom: 14px;
        border: 1px solid var(--sparrow-border);
        border-radius: 14px;
        overflow: hidden;
    }

    .messages-table td {
        padding: 9px 12px;
        border-bottom: 0;
    }
}
</style>

<section class="card">
    <div class="page-heading-row">
        <div>
            <h1>Messages</h1>
            <p class="muted">Questions sent through the website Contact form.</p>
        </div>
    </div>

    <div class="message-counts">
        <div class="message-count"><strong><?php echo $counts['all']; ?></strong>All</div>
        <div class="message-count"><strong><?php echo $counts['new']; ?></strong>New</div>
        <div class="message-count"><strong><?php echo $counts['read']; ?></strong>Read</div>
    </div>

    <div class="message-filters">
        <a class="button <?php echo $filter === '' ? '' : 'button-secondary'; ?>" href="<?php echo sparrow_escape(sparrow_url('/backyard/messages.php')); ?>">All</a>
        <a class="button <?php echo $filter === 'New' ? '' : 'button-secondary'; ?>" href="<?php echo sparrow_escape(sparrow_url('/backyard/messages.php?status=New')); ?>">New</a>
        <a class="button <?php echo $filter === 'Read' ? '' : 'button-secondary'; ?>" href="<?php echo sparrow_escape(sparrow_url('/backyard/messages.php?status=Read')); ?>">Read</a>
    </div>
</section>

<section class="card">
    <?php if (!$messages): ?>
        <p class="muted">No <?php echo $filter !== '' ? strtolower(sparrow_escape($filter)) . ' ' : ''; ?>messages yet.</p>
    <?php else: ?>
        <div class="table-wrap">
            <table class="messages-table">
                <thead>
                    <tr>
                        <th>Status</th>
                        <th>From</th>
                        <th>Subject</th>
                        <th>Preferred Contact</th>
                        <th>Received</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($messages as $message): ?>
                        <?php
                        $is_new = ($message['status'] ?? 'New') === 'New';
                        $preview = trim(preg_replace('/\s+/', ' ', (string) ($message['message'] ?? '')));
                        if (strlen($preview) > 90) {
                            $preview = substr($preview, 0, 87) . '...';
                        }
                        ?>
                        <tr class="<?php echo $is_new ? 'message-row-new' : ''; ?>">
                            <td>
                                <span class="message-status <?php echo $is_new ? 'message-status-new' : 'message-status-read'; ?>">
                                    <?php echo sparrow_escape($message['status'] ?? 'New'); ?>
                                </span>
                            </td>
                            <td>
                                <?php echo sparrow_escape($message['name'] ?? ''); ?>
                                <span class="message-preview"><?php echo sparrow_escape($message['email'] ?? ''); ?></span>
                            </td>
                            <td>
                                <a class="message-subject" href="<?php echo sparrow_escape(sparrow_url('/backyard/message-view.php?id=' . (int) $message['id'])); ?>">
                                    <?php echo sparrow_escape($message['subject'] ?? 'Message'); ?>
                                </a>
                                <span class="message-preview"><?php echo sparrow_escape($preview); ?></span>
                            </td>
                            <td><?php echo sparrow_escape($message['preferred_contact'] ?? 'Email'); ?></td>
                            <td><?php echo sparrow_escape(date('M j, Y g:i A', strtotime((string) $message['created_at']))); ?></td>
                            <td>
                                <a class="button button-secondary" href="<?php echo sparrow_escape(sparrow_url('/backyard/message-view.php?id=' . (int) $message['id'])); ?>">Open</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>

<?php sparrow_backyard_footer(); ?>
