<?php declare(strict_types=1);
if (session_status() === PHP_SESSION_NONE) { session_start(); }
require_once __DIR__ . '/../../includes/auth.php';
require_role(['student','super_admin','branch_admin']);
require_once __DIR__ . '/../../includes/db.php';
require_once __DIR__ . '/../../includes/helpers.php';
require_once __DIR__ . '/../../includes/layout_top.php';
require_once __DIR__ . '/../../includes/scope.php';

$course_id = (int)($_GET['course_id'] ?? 0);
$batch_id  = (int)($_GET['batch_id'] ?? 0);
$courses   = elms_all_courses($mysqli);
$batches   = $course_id ? elms_batches_for_course($mysqli, $course_id) : [];

// Build SQL with dynamic optional filters
$where = "(publish_at <= NOW() AND (expire_at IS NULL OR expire_at >= NOW())) AND (scope_type='system'";
$types = '';
$args  = [];

if ($course_id) { $where .= " OR (scope_type='course' AND scope_id=?)"; $types .= 'i'; $args[] = $course_id; }
if ($batch_id)  { $where .= " OR (scope_type='batch' AND scope_id=?)";  $types .= 'i'; $args[] = $batch_id; }
$where .= ')';

$sql = "SELECT id, scope_type, scope_id, title, body, publish_at, expire_at, pinned
        FROM lms_announcements
        WHERE $where
        ORDER BY pinned DESC, publish_at DESC, id DESC
        LIMIT 100";

$stmt = $mysqli->prepare($sql);
if ($types !== '') { $stmt->bind_param($types, ...$args); }
$stmt->execute();
$rows = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
?>
<h1 class="h5 mb-3">Announcements</h1>

<form class="row g-2 mb-3" method="get">
  <div class="col-auto">
    <select name="course_id" class="form-select" onchange="this.form.submit()">
      <option value="">All courses</option>
      <?php foreach ($courses as $cid => $ctitle): ?>
      <option value="<?= (int)$cid ?>" <?= $cid===$course_id?'selected':'' ?>><?= esc($ctitle) ?></option>
      <?php endforeach; ?>
    </select>
  </div>
  <div class="col-auto">
    <select name="batch_id" class="form-select">
      <option value="">All batches</option>
      <?php foreach ($batches as $bid => $bcode): ?>
      <option value="<?= (int)$bid ?>" <?= $bid===$batch_id?'selected':'' ?>><?= esc($bcode) ?></option>
      <?php endforeach; ?>
    </select>
  </div>
  <div class="col-auto"><button class="btn btn-primary">Filter</button></div>
</form>

<div class="list-group">
  <?php foreach ($rows as $r): ?>
    <div class="list-group-item">
      <div class="d-flex justify-content-between">
        <h6 class="mb-1">
          <?php if ((int)$r['pinned']): ?><span class="badge bg-warning text-dark me-1">Pinned</span><?php endif; ?>
          <?= esc($r['title']) ?>
        </h6>
        <small><?= esc(date('Y-m-d H:i', strtotime($r['publish_at']))) ?></small>
      </div>
      <div class="small text-muted mb-1">
        Scope: <?= esc($r['scope_type']) ?><?= $r['scope_id'] ? ' #'.(int)$r['scope_id'] : '' ?>
      </div>
      <p class="mb-1"><?= nl2br(esc($r['body'])) ?></p>
    </div>
  <?php endforeach; ?>
  <?php if (!$rows): ?>
    <div class="list-group-item text-muted">No announcements.</div>
  <?php endif; ?>
</div>

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