@php
    $viewErrors = isset($errors) ? $errors : session('errors');

    $successMessages = array_filter([
        session('success'),
        session('success_message'),
        session('status'),
    ]);

    $infoMessages = array_filter([
        session('info'),
    ]);

    $warningMessages = array_filter([
        session('warning'),
        session('warning_message'),
    ]);

    $rawMessage = session('message');
    $messageLooksLikeError = false;
    if ($rawMessage) {
        $lowerMessage = strtolower((string) $rawMessage);
        foreach (['could not', 'failed', 'failure', 'invalid', 'not allowed', 'not authorised', 'not authorized', 'duplicate', 'error', 'rejected', 'no partial'] as $needle) {
            if (strpos($lowerMessage, $needle) !== false) {
                $messageLooksLikeError = true;
                break;
            }
        }
    }

    $errorMessages = array_filter([
        session('error'),
        session('error_message'),
        session('danger'),
        session('failure'),
        $messageLooksLikeError ? $rawMessage : null,
    ]);

    if ($rawMessage && !$messageLooksLikeError) {
        $successMessages[] = $rawMessage;
    }
@endphp

<style>
    .ngbvd-feedback-stack { margin-bottom: 1rem; }
    .ngbvd-feedback-alert {
        border-radius: 16px;
        border: 1px solid transparent;
        padding: 14px 18px;
        box-shadow: 0 12px 30px rgba(15, 23, 42, .08);
        font-size: 14px;
    }
    .ngbvd-feedback-alert strong { display: block; margin-bottom: 4px; }
    .ngbvd-feedback-alert ul { padding-left: 1.25rem; }
    .ngbvd-field-error-anchor { scroll-margin-top: 110px; }
    .server-feedback { display: block; font-size: 12px; margin-top: 6px; }
    .is-invalid, .selectize-input.is-invalid {
        border-color: #dc3545 !important;
        box-shadow: 0 0 0 .15rem rgba(220, 53, 69, .15) !important;
    }
</style>

@if(!empty($successMessages) || !empty($errorMessages) || !empty($warningMessages) || !empty($infoMessages) || ($viewErrors && method_exists($viewErrors, 'any') && $viewErrors->any()))
    <div class="ngbvd-feedback-stack" id="ngbvd-feedback-stack" role="status" aria-live="polite">
        @foreach($successMessages as $message)
            <div class="alert alert-success ngbvd-feedback-alert">
                <strong>Success</strong>
                <span>{{ $message }}</span>
            </div>
        @endforeach

        @foreach($errorMessages as $message)
            <div class="alert alert-danger ngbvd-feedback-alert">
                <strong>Action Required</strong>
                <span>{{ $message }}</span>
            </div>
        @endforeach

        @foreach($warningMessages as $message)
            <div class="alert alert-warning ngbvd-feedback-alert">
                <strong>Warning</strong>
                <span>{{ $message }}</span>
            </div>
        @endforeach

        @foreach($infoMessages as $message)
            <div class="alert alert-info ngbvd-feedback-alert">
                <strong>Information</strong>
                <span>{{ $message }}</span>
            </div>
        @endforeach

        @if($viewErrors && method_exists($viewErrors, 'any') && $viewErrors->any())
            <div class="alert alert-danger ngbvd-feedback-alert" id="ngbvd-validation-summary">
                <strong>Please review the following:</strong>
                <ul class="mb-0">
                    @foreach($viewErrors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
    </div>
@endif
