<?php

/**
 * Flash Message Component
 *
 * Renders standardized session flash messages matching design system alerts.
 *
 * @ref  RC-015
 */

/** @var array<string, string> $flash */
$flash = $flash ?? [];

if (empty($flash)) {
    return; // Nothing to render
}

$icons = [
    'success' => '<i class="fa-solid fa-circle-check"></i>',
    'error'   => '<i class="fa-solid fa-circle-xmark"></i>',
    'warning' => '<i class="fa-solid fa-triangle-exclamation"></i>',
    'info'    => '<i class="fa-solid fa-circle-info"></i>',
];

$alertClasses = [
    'success' => 'alert-success',
    'error'   => 'alert-danger',
    'warning' => 'alert-warning',
    'info'    => 'alert-info',
];

?>
<div class="space-y-3 mb-5" role="region" aria-label="Notifications" aria-live="polite">
    <?php foreach ($flash as $type => $message): ?>
        <?php
        $safeType = in_array($type, ['success', 'error', 'warning', 'info'], true) ? $type : 'info';
        $icon = $icons[$safeType] ?? 'ℹ';
        $alertClass = $alertClasses[$safeType] ?? 'alert-info';
        ?>
        <div class="alert <?= $alertClass ?> shadow-sm flex items-center gap-2" role="alert" data-flash-auto-dismiss="5000">
            <span class="font-bold text-lg" aria-hidden="true"><?= $icon ?></span>
            <span class="font-medium"><?= escape_html($message) ?></span>
        </div>
    <?php endforeach; ?>
</div>
