<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'db.php';

$date = $_GET['date'] ?? null;
if (!$date) die("Invalid date");

/* ===============================
   DAY OF WEEK (0=Sun ... 6=Sat)
================================ */
$dayOfWeek = date('w', strtotime($date));

/* ===============================
   FETCH DAILY MASSES
================================ */
$massStmt = $pdo->prepare("
  SELECT title, start_time, end_time, location
  FROM mass_schedule
  WHERE day_of_week = ?
    AND is_active = 1
  ORDER BY start_time
");
$massStmt->execute([$dayOfWeek]);
$dailyMasses = $massStmt->fetchAll(PDO::FETCH_ASSOC);

/* ===============================
   FETCH ADMIN EVENTS
================================ */
$stmt = $pdo->prepare("SELECT * FROM events WHERE event_date = ?");
$stmt->execute([$date]);
$events = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Parish Events</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@600;700&family=Inter:wght@400;500;600&display=swap" rel="stylesheet">

<style>
:root {
  --blue: #2c5aa0;
  --gold: #c9a24d;
  --bg: #f8f5f0;
  --text: #1f2937;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Inter, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* HEADER */
.event-header {
  background: linear-gradient(
    to bottom,
    rgba(44,90,160,0.9),
    rgba(44,90,160,0.75)
  );
  padding: 40px 20px 30px;
  text-align: center;
  color: #fff;
}

.event-header img {
  width: 70px;
  margin-bottom: 12px;
}

.event-header h1 {
  font-family: "Playfair Display", serif;
  font-size: 1.6rem;
  margin: 0;
}

.event-header p {
  margin-top: 6px;
  font-size: 0.95rem;
  letter-spacing: 0.5px;
  opacity: 0.9;
}

/* CONTENT */
.container {
  max-width: 800px;
  margin: auto;
  padding: 30px 16px 60px;
}

/* EVENT CARD */
.event-card {
  background: #fff;
  border-radius: 14px;
  padding: 22px;
  margin-bottom: 20px;
  box-shadow: 0 12px 30px rgba(0,0,0,0.08);
  border-left: 5px solid var(--gold);
}

.event-card h3 {
  font-family: "Playfair Display", serif;
  font-size: 1.2rem;
  margin-bottom: 8px;
  color: var(--blue);
}

.event-meta {
  font-size: 0.9rem;
  margin-bottom: 12px;
  color: #374151;
}

.event-meta span {
  display: block;
  margin-bottom: 4px;
}

.event-desc {
  font-size: 0.95rem;
  line-height: 1.55;
  color: #374151;
}

/* MASS STYLING */
.mass-card {
  background: #f8fbff;
  border-left-color: var(--blue);
}

.mass-badge {
  display: inline-block;
  font-size: 0.7rem;
  background: var(--blue);
  color: #fff;
  padding: 3px 8px;
  border-radius: 10px;
  margin-bottom: 8px;
}

/* EMPTY STATE */
.empty {
  background: #fff;
  border-radius: 14px;
  padding: 30px;
  text-align: center;
  box-shadow: 0 10px 24px rgba(0,0,0,0.06);
}

/* BACK BUTTON */
.back-btn {
  display: inline-block;
  margin-top: 30px;
  padding: 12px 26px;
  background: var(--blue);
  color: #fff;
  border-radius: 30px;
  text-decoration: none;
  font-weight: 500;
}

/* MOBILE */
@media (max-width: 600px) {
  .event-header h1 { font-size: 1.35rem; }
  .event-card { padding: 18px; }
}
</style>
</head>

<?php
require_once 'header.php';
?>

<body>

<div class="page-content2">

<!-- HEADER -->
<div class="event-header">
  <img src="./images/our-lady1.png" alt="Cathedral Logo">
  <h1>Parish Events</h1>
  <p><?= date('F j, Y', strtotime($date)) ?></p>
</div>

<!-- CONTENT -->
<div class="container">

<?php if (!$dailyMasses && !$events): ?>
  <div class="empty">
    <p>No events scheduled for this date.</p>
  </div>
<?php endif; ?>

<!-- DAILY MASSES -->
<?php foreach ($dailyMasses as $m): ?>
  <div class="event-card mass-card">
    <span class="mass-badge">Holy Mass</span>

    <h3><?= htmlspecialchars($m['title']) ?></h3>

    <div class="event-meta">
      <span>🕒 <?= substr($m['start_time'],0,5) ?> – <?= substr($m['end_time'],0,5) ?></span>
      <span>📍 <?= htmlspecialchars($m['location']) ?></span>
    </div>
  </div>
<?php endforeach; ?>

<!-- ADMIN EVENTS -->
<?php foreach ($events as $e): ?>
  <div class="event-card">
    <h3><?= htmlspecialchars($e['title']) ?></h3>

    <div class="event-meta">
      <span>🕒 <?= $e['start_time'] ?> – <?= $e['end_time'] ?></span>
      <span>📍 <?= htmlspecialchars($e['location']) ?></span>
    </div>

    <div class="event-desc">
      <?= nl2br(htmlspecialchars($e['description'])) ?>
    </div>
  </div>
<?php endforeach; ?>

<?php
$backUrl =
  $_GET['return']
  ?? $_SERVER['HTTP_REFERER']
  ?? 'calendar.php';
?>

<a href="<?= htmlspecialchars($backUrl) ?>" class="back-btn">
  ← Back to Calendar
</a>

</div>
</div>
</body>
</html>
