<?php

/**

 * Resolves the current session and term.

 * Primary: active range (start_date <= CURDATE() <= end_date).

 * Fallback: most recently ended record (end_date < CURDATE() ORDER BY end_date DESC).

 *

 * @param mysqli $con

 * @return array{session_id:int, session_name:string|null, term_id:int, term_name:string|null}

 */

function resolveCurrentSessionTerm(mysqli $con): array
{

    $result = [

        'session_id' => 0,

        'session_name' => null,

        'term_id' => 0,

        'term_name' => null,

    ];



    // --- Session: primary query ---

    $stmt = $con->prepare(

        "SELECT id, session_name FROM school_sessions

         WHERE start_date <= CURDATE() AND end_date >= CURDATE()

         ORDER BY start_date ASC LIMIT 1"

    );

    $stmt->execute();

    $row = $stmt->get_result()->fetch_assoc();

    $stmt->close();



    // --- Session: fallback query ---

    if (!$row) {

        $stmt = $con->prepare(

            "SELECT id, session_name FROM school_sessions

             WHERE end_date < CURDATE()

             ORDER BY end_date DESC LIMIT 1"

        );

        $stmt->execute();

        $row = $stmt->get_result()->fetch_assoc();

        $stmt->close();

    }



    if (!$row) {

        return $result; // no session data at all — caller must handle

    }



    $result['session_id'] = (int) $row['id'];

    $result['session_name'] = $row['session_name'];



    // --- Term: primary query ---

    $stmt = $con->prepare(

        "SELECT id, term_name FROM school_terms

         WHERE session_id = ? AND start_date <= CURDATE() AND end_date >= CURDATE()

         ORDER BY start_date ASC LIMIT 1"

    );

    $stmt->bind_param("i", $result['session_id']);

    $stmt->execute();

    $termRow = $stmt->get_result()->fetch_assoc();

    $stmt->close();



    // --- Term: fallback query ---

    if (!$termRow) {

        $stmt = $con->prepare(

            "SELECT id, term_name FROM school_terms

             WHERE session_id = ? AND end_date < CURDATE()

             ORDER BY end_date DESC LIMIT 1"

        );

        $stmt->bind_param("i", $result['session_id']);

        $stmt->execute();

        $termRow = $stmt->get_result()->fetch_assoc();

        $stmt->close();

    }



    if ($termRow) {

        $result['term_id'] = (int) $termRow['id'];

        $result['term_name'] = $termRow['term_name'];

    }



    return $result;

}


/**
 * Renders the session/term badge block HTML.
 *
 * Returns a <div class="mt-1"> containing:
 *   - A primary badge with the session name (if non-empty)
 *   - A success badge with the term name (if non-empty), OR a warning badge if no term
 *
 * @param string|null $sessionName  The current session name, or null/empty if unavailable.
 * @param string|null $termName     The current term name, or null/empty if unavailable.
 * @return string                   The rendered HTML badge block.
 */
function renderSessionTermBadges(?string $sessionName, ?string $termName): string
{
    $html = '<div class="mt-1">' . "\n";

    if (!empty($sessionName)) {
        $html .= '  <span class="badge badge-primary mr-1">' . "\n";
        $html .= '    <i class="fas fa-calendar-alt"></i>' . "\n";
        $html .= '    ' . htmlspecialchars($sessionName, ENT_QUOTES, 'UTF-8') . "\n";
        $html .= '  </span>' . "\n";
    }

    if (!empty($termName)) {
        $html .= '  <span class="badge badge-success">' . "\n";
        $html .= '    <i class="fas fa-bookmark"></i>' . "\n";
        $html .= '    ' . htmlspecialchars($termName, ENT_QUOTES, 'UTF-8') . "\n";
        $html .= '  </span>' . "\n";
    } else {
        $html .= '  <span class="badge badge-warning">' . "\n";
        $html .= '    <i class="fas fa-exclamation-triangle"></i> No active term' . "\n";
        $html .= '  </span>' . "\n";
    }

    $html .= '</div>';
    return $html;
}
