<?php
// Flash message helper functions

function setFlashMessage($message, $type = 'success') {
    $_SESSION['flash_message'] = $message;
    $_SESSION['flash_type'] = $type;
}

function hasFlashMessage() {
    return isset($_SESSION['flash_message']);
}

function showFlashMessage() {
    if (!hasFlashMessage()) return '';
    $message = $_SESSION['flash_message'];
    $type = $_SESSION['flash_type'] ?? 'success';
    $icon = [
        'success' => 'fa-check-circle',
        'danger'  => 'fa-exclamation-circle',
        'warning' => 'fa-exclamation-triangle',
        'info'    => 'fa-info-circle',
    ];
    $iconClass = $icon[$type] ?? 'fa-info-circle';
    $html = '<div class="alert alert-' . $type . ' alert-dismissible fade show" role="alert">';
    $html .= '<i class="fas ' . $iconClass . ' me-2"></i>' . htmlspecialchars($message);
    $html .= '<button type="button" class="btn-close" data-bs-dismiss="alert"></button>';
    $html .= '</div>';
    unset($_SESSION['flash_message'], $_SESSION['flash_type']);
    return $html;
}

function showSessionMessages() {
    $output = '';
    if (isset($_SESSION['success'])) {
        $output .= '<div class="alert alert-success alert-dismissible fade show"><i class="fas fa-check-circle me-2"></i>' . htmlspecialchars($_SESSION['success']) . '<button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>';
        unset($_SESSION['success']);
    }
    if (isset($_SESSION['error'])) {
        $output .= '<div class="alert alert-danger alert-dismissible fade show"><i class="fas fa-exclamation-circle me-2"></i>' . htmlspecialchars($_SESSION['error']) . '<button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>';
        unset($_SESSION['error']);
    }
    if (isset($_SESSION['warning'])) {
        $output .= '<div class="alert alert-warning alert-dismissible fade show"><i class="fas fa-exclamation-triangle me-2"></i>' . htmlspecialchars($_SESSION['warning']) . '<button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>';
        unset($_SESSION['warning']);
    }
    if (isset($_SESSION['info'])) {
        $output .= '<div class="alert alert-info alert-dismissible fade show"><i class="fas fa-info-circle me-2"></i>' . htmlspecialchars($_SESSION['info']) . '<button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>';
        unset($_SESSION['info']);
    }
    return $output;
}
