<?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/style.css?v=<?= time() ?>">
</head>
<body style="background: #050505;">
    <div class="dashboard-container">
        <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" style="color: var(--danger); font-size: 0.9rem; text-decoration: none; font-weight: 600;">Logout</a>
            </div>
        </aside>

        <main class="main-content">
            <h1 style="margin-bottom: 2rem; font-weight: 900;">Customer Inquiries</h1>
            
            <div class="grid" style="grid-template-columns: 1fr;">
                <?php if (empty($messages)): ?>
                    <div class="glass" style="padding: 4rem; text-align: center;">
                        <p style="color: var(--text-secondary);">No messages found.</p>
                    </div>
                <?php else: ?>
                    <?php foreach ($messages as $msg): ?>
                    <div class="glass" style="padding: 2.5rem; border-left: 4px solid <?= $msg['status'] === 'unread' ? 'var(--primary-color)' : 'transparent' ?>;">
                        <div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 1.5rem;">
                            <div>
                                <h3 style="margin-bottom: 0.3rem;"><?= htmlspecialchars($msg['subject']) ?></h3>
                                <p style="font-size: 0.85rem; color: var(--text-secondary);">From: <strong><?= htmlspecialchars($msg['name']) ?></strong> (<?= htmlspecialchars($msg['email']) ?>)</p>
                            </div>
                            <div style="text-align: right;">
                                <div style="font-size: 0.75rem; color: var(--text-secondary); margin-bottom: 0.5rem;"><?= 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 style="background: rgba(255,255,255,0.02); padding: 2rem; border-radius: 12px; margin-bottom: 2rem; line-height: 1.8; color: var(--text-secondary);">
                            <?= nl2br(htmlspecialchars($msg['message'])) ?>
                        </div>
                        <div style="display: flex; gap: 1rem;">
                            <?php if ($msg['status'] === 'unread'): ?>
                                <a href="admin_messages.php?action=read&id=<?= $msg['id'] ?>" class="btn-auth">Mark as Read</a>
                            <?php endif; ?>
                            <a href="mailto:<?= $msg['email'] ?>" class="btn-auth" style="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-auth" style="border-color: var(--danger); color: var(--danger);">Delete</a>
                        </div>
                    </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </main>
    </div>
</body>
</html>