<?php
session_start();

// Redirect if not logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
  header('Location: login.php');
  exit;
}

// Get current page name
$pageName = basename($_SERVER['PHP_SELF']);

// Check if user has control over this page
function canControlPage($page = null) {
  if (!isset($_SESSION['role']) || !isset($_SESSION['page_control'])) return false;

  $target = $page ?? basename($_SERVER['PHP_SELF']);
  $allowed = $_SESSION['page_control'];

  // Full access for admins or wildcard
  return $_SESSION['role'] === 'admin' || in_array('*', $allowed) || in_array($target, $allowed);
}

// Check if user is admin
function isAdmin() {
  return ($_SESSION['role'] ?? '') === 'admin';
}

// Check if user is DJ
function isDJ() {
  return ($_SESSION['role'] ?? '') === 'dj';
}

// Optional: Check if user is the station owner
function isOwner() {
  return ($_SESSION['username'] ?? '') === 'Musicman';
}
