<?php
session_start();
require_once __DIR__ . '/db/config.php';

if (!isset($_SESSION['user_id']) || ($_SESSION['role'] ?? '') !== 'admin') {
    header('Location: login.php');
    exit;
}

$pdo = db();

if (isset($_GET['action']) && isset($_GET['id'])) {
    $id = $_GET['id'];
    if ($_GET['action'] === 'read') {
        $pdo->prepare("UPDATE contact_messages SET status = 'read' WHERE id = ?")->execute([$id]);
    } elseif ($_GET['action'] === 'delete') {
        $pdo->prepare("DELETE FROM contact_messages WHERE id = ?")->execute([$id]);
    }
    header('Location: admin_messages.php');
    exit;
}

$messages = $pdo->query("SELECT * FROM contact_messages ORDER BY created_at DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Messages | Admin</title>
    <link rel="stylesheet" href="assets/css/fonts.css">
    <link rel="stylesheet" href="assets/css/style.css?v=<?= time() ?>">
</head>
<body>
    <div class="dashboard-container">
        <!-- Sidebar -->
        <aside class="sidebar">
            <a href="index.php" class="sidebar-brand">AFGCARS</a>
            <ul class="sidebar-menu">
                <li><a href="admin_dashboard.php"><span>Dashboard</span></a></li>
                <li><a href="admin_cars.php"><span>Manage Cars</span></a></li>
                <li><a href="admin_purchases.php"><span>Purchase Requests</span></a></li>
                <li><a href="admin_users.php"><span>Users</span></a></li>
                <li><a href="admin_messages.php" class="active"><span>Messages</span></a></li>
            </ul>
            <div class="sidebar-footer">
                <a href="logout.php" class="btn btn-danger btn-sm" style="width: 100%;">Logout</a>
            </div>
        </aside>

        <!-- Main Content -->
        <main class="main-content">
            <header class="mb-3">
                <h1 class="fw-bold" style="font-size: 2.5rem;">Customer Inquiries</h1>
                <p class="text-secondary">Review and respond to messages from marketplace users and visitors.</p>
            </header>
            
            <div class="grid" style="grid-template-columns: 1fr; gap: 2rem;">
                <?php if (empty($messages)): ?>
                    <div class="glass" style="padding: 4rem; text-align: center;">
                        <p class="text-secondary">No messages found.</p>
                    </div>
                <?php else: ?>
                    <?php foreach ($messages as $msg): ?>
                    <div class="glass" style="padding: 2.5rem; border-left: 5px solid <?= $msg['status'] === 'unread' ? 'var(--primary-color)' : 'transparent' ?>;">
                        <div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 2rem;">
                            <div>
                                <h3 class="fw-bold mb-1" style="font-size: 1.4rem;"><?= htmlspecialchars($msg['subject']) ?></h3>
                                <p class="text-sm">From: <strong class="text-gold"><?= htmlspecialchars($msg['name']) ?></strong> <span class="text-secondary">(<?= htmlspecialchars($msg['email']) ?>)</span></p>
                            </div>
                            <div style="text-align: right;">
                                <div class="text-sm text-secondary mb-1" style="font-weight: 600;"><?= date('M d, Y H:i', strtotime($msg['created_at'])) ?></div>
                                <span class="badge badge-<?= $msg['status'] === 'unread' ? 'warning' : 'success' ?>"><?= ucfirst($msg['status']) ?></span>
                            </div>
                        </div>
                        <div class="mb-2" style="background: rgba(255,255,255,0.03); padding: 2rem; border-radius: 16px; line-height: 1.8; color: var(--text-secondary); font-size: 0.95rem;">
                            <?= nl2br(htmlspecialchars($msg['message'])) ?>
                        </div>
                        <div style="display: flex; gap: 1.2rem;">
                            <?php if ($msg['status'] === 'unread'): ?>
                                <a href="admin_messages.php?action=read&id=<?= $msg['id'] ?>" class="btn btn-primary btn-sm" style="padding: 0.8rem 1.5rem;">Mark as Read</a>
                            <?php endif; ?>
                            <a href="mailto:<?= $msg['email'] ?>" class="btn btn-outline btn-sm" style="padding: 0.8rem 1.5rem; border-color: var(--primary-color); color: var(--primary-color);">Reply via Email</a>
                            <a href="admin_messages.php?action=delete&id=<?= $msg['id'] ?>" onclick="return confirm('Delete this message?')" class="btn btn-outline btn-sm" style="padding: 0.8rem 1.5rem; border-color: var(--danger); color: var(--danger);">Delete Inquiry</a>
                        </div>
                    </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </main>
    </div>
</body>
</html>