<?php
require_once __DIR__ . '/auth_guard.php';
require_once __DIR__ . '/../backend/config/database.php';

$db = getDB();
$adminUserName = $_SESSION['user_name'];
$adminPage     = 'messages';
$adminPageTitle= 'Messages';

$msg = '';

// Mark as read
if(isset($_GET['read'])){
    $db->prepare("UPDATE messages SET is_read=1 WHERE id=?")->execute([$_GET['read']]);
    header('Location: /stile-lpg/admin/messages.php');
    exit;
}
// Delete
if(isset($_GET['delete'])){
    $db->prepare("DELETE FROM messages WHERE id=?")->execute([$_GET['delete']]);
    header('Location: /stile-lpg/admin/messages.php?deleted=1');
    exit;
}
if(isset($_GET['deleted'])) $msg = 'Message deleted.';

$filter = $_GET['filter'] ?? 'all';
if($filter === 'unread'){
    $stmt = $db->query("SELECT * FROM messages WHERE is_read=0 ORDER BY created_at DESC");
} else {
    $stmt = $db->query("SELECT * FROM messages ORDER BY created_at DESC");
}
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
$unreadCount = (int)$db->query("SELECT COUNT(*) FROM messages WHERE is_read=0")->fetchColumn();
?>
<?php require_once __DIR__ . '/layout_top.php'; ?>

<?php if($msg): ?>
<div style="background:#f0fdf4;border:1.5px solid #bbf7d0;color:#16a34a;padding:.85rem 1.1rem;border-radius:12px;font-size:.875rem;font-weight:600;margin-bottom:1.5rem;display:flex;align-items:center;gap:.5rem">
  <svg width="16" height="16" fill="none" stroke="#16a34a" stroke-width="2.5" viewBox="0 0 24 24"><polyline stroke-linecap="round" stroke-linejoin="round" points="20 6 9 17 4 12"/></svg>
  <?= htmlspecialchars($msg) ?>
</div>
<?php endif; ?>

<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:1.75rem;flex-wrap:wrap;gap:1rem">
  <div>
    <h1 style="font-weight:900;font-size:1.4rem;color:#0f172a;margin:0 0 .2rem">Messages</h1>
    <p style="color:#64748b;font-size:.85rem;margin:0"><?= $unreadCount ?> unread of <?= count($messages) ?> total</p>
  </div>
  <div style="display:flex;gap:.5rem">
    <a href="/stile-lpg/admin/messages.php" style="padding:.5rem 1rem;border-radius:50px;font-size:.82rem;font-weight:700;border:1.5px solid <?= $filter==='all'?'transparent':'#e2e8f0' ?>;background:<?= $filter==='all'?'linear-gradient(135deg,#0f172a,#1e293b)':'#fff' ?>;color:<?= $filter==='all'?'#fff':'#475569' ?>;text-decoration:none">All</a>
    <a href="/stile-lpg/admin/messages.php?filter=unread" style="padding:.5rem 1rem;border-radius:50px;font-size:.82rem;font-weight:700;border:1.5px solid <?= $filter==='unread'?'transparent':'#e2e8f0' ?>;background:<?= $filter==='unread'?'linear-gradient(135deg,#0f172a,#1e293b)':'#fff' ?>;color:<?= $filter==='unread'?'#fff':'#475569' ?>;text-decoration:none">
      Unread <?= $unreadCount>0?"($unreadCount)":'' ?>
    </a>
  </div>
</div>

<?php if(empty($messages)): ?>
<div style="background:#fff;border-radius:20px;border:1px solid #e2e8f0;text-align:center;padding:4rem 1rem">
  <div style="width:64px;height:64px;border-radius:50%;background:#f1f5f9;display:flex;align-items:center;justify-content:center;margin:0 auto 1rem;opacity:.4">
    <svg width="28" height="28" fill="none" stroke="#94a3b8" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline stroke-linecap="round" stroke-linejoin="round" points="22,6 12,13 2,6"/></svg>
  </div>
  <p style="color:#94a3b8">No messages yet.</p>
</div>
<?php else: ?>
<div style="display:flex;flex-direction:column;gap:1rem">
  <?php foreach($messages as $m): ?>
  <div style="background:#fff;border-radius:16px;border:1.5px solid <?= $m['is_read']?'#e2e8f0':'#bfdbfe' ?>;box-shadow:0 2px 8px rgba(0,0,0,.04);padding:1.5rem;display:flex;flex-direction:column;gap:.85rem;<?= !$m['is_read']?'background:linear-gradient(to right,#eff6ff,#fff)':'' ?>">
    <div style="display:flex;align-items:flex-start;justify-content:space-between;flex-wrap:wrap;gap:.75rem">
      <div style="display:flex;align-items:center;gap:.85rem">
        <div style="width:42px;height:42px;border-radius:50%;background:linear-gradient(135deg,#0f172a,#1e293b);display:flex;align-items:center;justify-content:center;color:#f97316;font-weight:800;flex-shrink:0"><?= strtoupper(substr($m['name'],0,1)) ?></div>
        <div>
          <div style="display:flex;align-items:center;gap:.5rem">
            <span style="font-weight:800;color:#0f172a;font-size:.9rem"><?= htmlspecialchars($m['name']) ?></span>
            <?php if(!$m['is_read']): ?><span style="background:#dbeafe;color:#2563eb;font-size:.68rem;font-weight:700;padding:.15rem .55rem;border-radius:20px">New</span><?php endif; ?>
          </div>
          <div style="color:#94a3b8;font-size:.78rem"><?= htmlspecialchars($m['email']) ?> <?= $m['phone']?' · '.htmlspecialchars($m['phone']):'' ?></div>
        </div>
      </div>
      <div style="display:flex;align-items:center;gap:.5rem;flex-wrap:wrap">
        <span style="color:#94a3b8;font-size:.78rem"><?= date('M j, Y g:ia', strtotime($m['created_at'])) ?></span>
        <?php if(!$m['is_read']): ?>
        <a href="/stile-lpg/admin/messages.php?read=<?= urlencode($m['id']) ?>" style="background:#f0fdf4;color:#16a34a;border:1px solid #bbf7d0;font-size:.75rem;font-weight:700;padding:.25rem .75rem;border-radius:8px;text-decoration:none;display:inline-flex;align-items:center;gap:.35rem">
          <svg width="11" height="11" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24"><polyline stroke-linecap="round" stroke-linejoin="round" points="20 6 9 17 4 12"/></svg>
          Mark Read
        </a>
        <?php endif; ?>
        <a href="/stile-lpg/admin/messages.php?delete=<?= urlencode($m['id']) ?>" onclick="return confirm('Delete this message?')" style="background:#fef2f2;color:#dc2626;border:1px solid #fecaca;font-size:.75rem;font-weight:700;padding:.25rem .75rem;border-radius:8px;text-decoration:none;display:inline-flex;align-items:center;gap:.35rem">
          <svg width="11" height="11" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24"><polyline stroke-linecap="round" stroke-linejoin="round" points="3 6 5 6 21 6"/><path stroke-linecap="round" stroke-linejoin="round" d="M19 6l-1 14H6L5 6"/><path stroke-linecap="round" stroke-linejoin="round" d="M10 11v6M14 11v6"/><path stroke-linecap="round" stroke-linejoin="round" d="M9 6V4h6v2"/></svg>
          Delete
        </a>
      </div>
    </div>
    <?php if($m['subject']): ?><div style="font-weight:700;color:#475569;font-size:.82rem">Subject: <?= htmlspecialchars($m['subject']) ?></div><?php endif; ?>
    <p style="color:#475569;font-size:.875rem;line-height:1.6;margin:0;background:#f8f9fb;border-radius:10px;padding:.85rem 1rem"><?= nl2br(htmlspecialchars($m['message'])) ?></p>
    <div style="display:flex;gap:.5rem">
      <a href="mailto:<?= htmlspecialchars($m['email']) ?>" style="display:inline-flex;align-items:center;gap:.5rem;background:linear-gradient(135deg,#f97316,#ea580c);color:#fff;font-size:.78rem;font-weight:700;padding:.45rem 1rem;border-radius:10px;text-decoration:none;box-shadow:0 3px 10px rgba(249,115,22,.3)">
        <svg width="13" height="13" fill="none" stroke="#fff" stroke-width="2" viewBox="0 0 24 24"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>
        Reply by Email
      </a>
    </div>
  </div>
  <?php endforeach; ?>
</div>
<?php endif; ?>

<?php require_once __DIR__ . '/layout_bottom.php'; ?>
