<?php
// Christo Lochenberg Developments | info@christodev.co.za | +27 61 523 9126

// Flash message helper
// EXAMPLE - flash('register_success', 'You are now registered');
// DISPLAY IN VIEW - echo flash('register_success');
function flash(string $name = '', string $message = '', string $class = 'success'): void {
    if (!empty($name)) {
        if (!empty($message) && empty($_SESSION[$name])) {
            if (!empty($_SESSION[$name])) {
                unset($_SESSION[$name]);
            }
            if (!empty($_SESSION[$name . '_class'])) {
                unset($_SESSION[$name . '_class']);
            }
            $_SESSION[$name] = $message;
            $_SESSION[$name . '_class'] = $class;
            
        } elseif (empty($message) && !empty($_SESSION[$name])) {
            $class = !empty($_SESSION[$name . '_class']) ? $_SESSION[$name . '_class'] : 'success';
            
            // Determine Color and Icon based on success/error state
            if ($class === 'error' || $class === 'danger' || $class === 'alert-danger') {
                $bg_color = 'bg-red-600 dark:bg-red-500';
                $icon = '<svg class="w-5 h-5 mr-2.5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>';
            } else {
                $bg_color = 'bg-green-600 dark:bg-green-500';
                $icon = '<svg class="w-5 h-5 mr-2.5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>';
            }

            // Output the Toast UI (Moved to top-28 to clear header completely)
            echo '<div id="toast-msg" class="fixed top-28 left-1/2 -translate-x-1/2 ' . $bg_color . ' text-white font-bold px-5 py-3.5 rounded-xl shadow-2xl border border-white/20 z-[9999] flex items-center transform transition-all duration-500 translate-y-0 opacity-100 min-w-max max-w-[90vw]">
                    ' . $icon . '
                    <span class="text-sm">' . $_SESSION[$name] . '</span>
                  </div>';
            
            // Add vanilla JS to auto-hide and gracefully remove the toast after 4 seconds
            echo '<script>
                    setTimeout(() => {
                        const toast = document.getElementById("toast-msg");
                        if(toast) {
                            toast.classList.remove("translate-y-0", "opacity-100");
                            toast.classList.add("-translate-y-10", "opacity-0"); // Slide up smoothly
                            setTimeout(() => { toast.remove(); }, 500); // Remove from DOM after animation
                        }
                    }, 4000);
                  </script>';
            
            unset($_SESSION[$name]);
            unset($_SESSION[$name . '_class']);
        }
    }
}