<?php
session_start();
include __DIR__ . '/auth.php'; // sets $CAN_EDIT

include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';

$contactFile = __DIR__ . '/../data/contact_messages.txt';

$messages = [];

/* ------------------------------------------------------------
   LOAD CONTACT MESSAGES
------------------------------------------------------------ */
if (file_exists($contactFile)) {
    $lines = file($contactFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    foreach ($lines as $line) {
        $parts = explode('|', $line);

        if (count($parts) >= 5) {
            $messages[] = [
                'id'        => $parts[0],
                'name'      => $parts[1],
                'email'     => $parts[2],
                'message'   => $parts[3],
                'timestamp' => $parts[4]
            ];
        }
    }
}

/* ------------------------------------------------------------
   DELETE MESSAGE (EDIT ACTION)
------------------------------------------------------------ */
if (isset($_GET['delete'])) {

    if (!$CAN_EDIT) {
        die("<h1>Access Denied</h1><p>You cannot delete contact messages.</p>");
    }

    $deleteId = $_GET['delete'];
    $newList = [];

    foreach ($messages as $msg) {
        if ($msg['id'] !== $deleteId) {
            $newList[] = implode('|', [
                $msg['id'],
                $msg['name'],
                $msg['email'],
                $msg['message'],
                $msg['timestamp']
            ]);
        }
    }

    file_put_contents($contactFile, implode("\n", $newList));
    header("Location: contact.php");
    exit;
}
?>

<style>
.contact-card {
    padding: 30px;
    margin-bottom: 30px;
    text-align: left;
}

.contact-card h2 {
    color: #4ff;
    margin-bottom: 10px;
    text-shadow: 0 0 10px rgba(0,255,255,0.45);
}

.contact-field {
    margin-bottom: 12px;
    line-height: 1.6;
}

.contact-label {
    font-weight: bold;
    color: #4ff;
    text-shadow: 0 0 6px rgba(0,255,255,0.45);
}
</style>

<div class="main-wrap">
    <div class="content-panel">

        <h1 class="section-title center-text">📨 Contact Messages</h1>

        <div class="vinyl-divider center-divider"></div>

        <?php if (!empty($messages)): ?>

            <?php foreach ($messages as $msg): ?>
                <div class="neon-card contact-card">

                    <h2><?= htmlspecialchars($msg['name']) ?></h2>

                    <div class="contact-field">
                        <span class="contact-label">Email:</span>
                        <?= htmlspecialchars($msg['email']) ?>
                    </div>

                    <div class="contact-field">
                        <span class="contact-label">Message:</span><br>
                        <?= nl2br(htmlspecialchars($msg['message'])) ?>
                    </div>

                    <div class="contact-field">
                        <span class="contact-label">Sent:</span>
                        <?= htmlspecialchars($msg['timestamp']) ?>
                    </div>

                    <?php if ($CAN_EDIT): ?>
                        <a href="contact.php?delete=<?= $msg['id'] ?>"
                           class="button"
                           style="background:#900;color:#fff;margin-top:15px;">
                            Delete Message
                        </a>
                    <?php endif; ?>

                </div>

                <div class="vinyl-divider center-divider"></div>

            <?php endforeach; ?>

        <?php else: ?>

            <div class="content-box center-text">
                <p>No messages yet.</p>
            </div>

        <?php endif; ?>

    </div>
</div>

<?php include __DIR__ . '/includes/footer.php'; ?>
