<?php

/**
 * Missed Attendance Remark — Access Control Helper
 *
 * Provides checkMissedAttendanceAccess() which evaluates the three-level
 * feature gate before any teacher-facing page or AJAX handler proceeds.
 *
 * Three levels (all must be enabled simultaneously):
 *   Level 1 — Global toggle  : portal_feature_settings.feature_key = 'missed_attendance_remark'
 *   Level 2 — Teacher toggle : missed_attendance_permissions scope='teacher' for this teacher_id
 *   Level 3 — Class toggle   : missed_attendance_permissions scope='class'   for this class/section
 *
 * A missing row at Level 2 or Level 3 is treated as is_enabled = 0 (deny by default).
 */

/**
 * Check whether a class teacher has effective access to the Missed Attendance
 * Remark feature for a specific class/section.
 *
 * @param mysqli $con        Active MySQLi connection.
 * @param int    $teacher_id The authenticated teacher's user ID.
 * @param string $class      The class name (e.g. "JSS 1").
 * @param string $section    The section name (e.g. "A").
 *
 * @return bool  true only when all three toggle levels are enabled.
 */
function checkMissedAttendanceAccess(mysqli $con, int $teacher_id, string $class, string $section): bool
{
    // ------------------------------------------------------------------
    // Level 1: Global toggle
    // ------------------------------------------------------------------
    $stmt = $con->prepare(
        "SELECT is_enabled
         FROM portal_feature_settings
         WHERE feature_key = 'missed_attendance_remark'
         LIMIT 1"
    );
    if (!$stmt) {
        return false;
    }
    $stmt->execute();
    $row = $stmt->get_result()->fetch_assoc();
    $stmt->close();

    if (!$row || !(int)$row['is_enabled']) {
        return false;
    }

    // ------------------------------------------------------------------
    // Level 2: Per-teacher toggle
    // ------------------------------------------------------------------
    $stmt = $con->prepare(
        "SELECT is_enabled
         FROM missed_attendance_permissions
         WHERE scope = 'teacher' AND teacher_id = ?
         LIMIT 1"
    );
    if (!$stmt) {
        return false;
    }
    $stmt->bind_param('i', $teacher_id);
    $stmt->execute();
    $row = $stmt->get_result()->fetch_assoc();
    $stmt->close();

    // Missing row = deny by default
    if (!$row || !(int)$row['is_enabled']) {
        return false;
    }

    // ------------------------------------------------------------------
    // Level 3: Per-class/section toggle
    // ------------------------------------------------------------------
    $stmt = $con->prepare(
        "SELECT is_enabled
         FROM missed_attendance_permissions
         WHERE scope = 'class' AND class = ? AND section = ?
         LIMIT 1"
    );
    if (!$stmt) {
        return false;
    }
    $stmt->bind_param('ss', $class, $section);
    $stmt->execute();
    $row = $stmt->get_result()->fetch_assoc();
    $stmt->close();

    // Missing row = deny by default
    if (!$row || !(int)$row['is_enabled']) {
        return false;
    }

    return true;
}
