<?php
// includes/flash.php
if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); }

/**
 * Renders and clears flash messages from the session.
 * Supports: success_message, error_message, info_message, warning_message
 * Returns: HTML (Bootstrap 5 alerts)
 */
function render_flash(): string {
    $out = '';
    $keys = [
        'success_message' => 'success',
        'error_message'   => 'danger',
        'info_message'    => 'info',
        'warning_message' => 'warning',
    ];
    foreach ($keys as $key => $bsType) {
        if (!empty($_SESSION[$key])) {
            $msg = htmlspecialchars((string)$_SESSION[$key], ENT_QUOTES, 'UTF-8');
            $out .= '<div class="alert alert-' . $bsType . ' alert-dismissible fade show" role="alert">'
                 .  $msg
                 .  '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>'
                 .  '</div>';
            unset($_SESSION[$key]); // clear after showing
        }
    }
    return $out;
}
