<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/flash.php';
require_admin();
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$id = (int)($_GET['id'] ?? 0);
$row = null;
if ($id > 0) {
    $st = db()->prepare('SELECT * FROM enquiries WHERE id = ?');
    $st->execute([$id]);
    $row = $st->fetch();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrf_verify($_POST['csrf'] ?? null) && $row) {
    $newStatus = (string)($_POST['status'] ?? '');
    if (in_array($newStatus, ['new', 'read', 'closed'], true)) {
        db()->prepare('UPDATE enquiries SET status = ? WHERE id = ?')->execute([$newStatus, $id]);
        admin_flash_set('success', 'Status updated.');
        header('Location: enquiry-detail.php?id=' . $id);
        exit;
    }
}

if (!$row) {
    admin_flash_set('danger', 'Enquiry not found.');
    header('Location: enquiries.php');
    exit;
}

$page = 'enquiries';
$pageTitle = 'Enquiry #' . $row['id'];
$pageSubtitle = 'Contact form submission • ' . (string)$row['created_at'];
$breadcrumbs = [
    ['label' => 'Dashboard', 'href' => 'index.php'],
    ['label' => 'Enquiries', 'href' => 'enquiries.php'],
    ['label' => 'Enquiry #' . $row['id'], 'href' => null],
];
$headerActions = '<a href="enquiries.php" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left me-1"></i>All enquiries</a>'
    . '<a href="enquiries-export.php" class="btn btn-outline-primary btn-sm"><i class="bi bi-download me-1"></i>Export CSV</a>';

require __DIR__ . '/includes/layout-header.php';
?>
<div class="row g-4">
  <div class="col-lg-8">
    <div class="admin-card rounded mb-4">
      <div class="card-header">Message</div>
      <div class="card-body">
        <pre class="mb-0" style="white-space:pre-wrap;font-family:inherit;font-size:0.95rem;"><?= h((string)($row['message'] ?? '')) ?: '—' ?></pre>
      </div>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="admin-card rounded mb-4">
      <div class="card-header">Contact details</div>
      <div class="card-body">
        <dl class="admin-dl mb-0">
          <dt>Name</dt>
          <dd class="fw-semibold"><?= h((string)$row['name']) ?></dd>
          <dt>Email</dt>
          <dd><a href="mailto:<?= h((string)$row['email']) ?>"><?= h((string)$row['email']) ?></a></dd>
          <dt>Phone</dt>
          <dd><?= h((string)($row['phone'] ?? '')) ?: '—' ?></dd>
          <dt>Subject</dt>
          <dd><?= h((string)($row['subject'] ?? '')) ?: '—' ?></dd>
          <dt>Received</dt>
          <dd class="small text-muted"><?= h((string)$row['created_at']) ?></dd>
        </dl>
      </div>
    </div>
    <div class="admin-card rounded">
      <div class="card-header">Workflow</div>
      <div class="card-body">
        <form method="post">
          <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
          <label class="form-label fw-semibold">Status</label>
          <select name="status" class="form-select mb-3">
            <option value="new" <?= $row['status'] === 'new' ? 'selected' : '' ?>>New</option>
            <option value="read" <?= $row['status'] === 'read' ? 'selected' : '' ?>>Read</option>
            <option value="closed" <?= $row['status'] === 'closed' ? 'selected' : '' ?>>Closed</option>
          </select>
          <button type="submit" class="btn btn-primary w-100">Update status</button>
        </form>
      </div>
    </div>
  </div>
</div>
<?php require __DIR__ . '/includes/layout-footer.php'; ?>
