@php
    $company = auth()->user()->company ?? session('company');
    $logoPath = null;

    if ($company && $company->logo) {
        $path = $company->logo;
        
        // Ensure path starts with companies/logos/ if it's just a filename
        if (!str_starts_with($path, 'companies/logos/') && !str_contains($path, '/')) {
            $path = 'companies/logos/' . $path;
        }

        if (Storage::disk('public')->exists($path)) {
            $logoPath = asset('storage/' . $path);
        }
    }

    // Use system logo as fallback
    if (!$logoPath) {
        $systemLogo = \App\Models\SystemSetting::get('system_logo');
        if ($systemLogo && Storage::disk('public')->exists($systemLogo)) {
            $logoPath = route('storage', $systemLogo);
        }
    }
@endphp

@if ($logoPath)
    <img src="{{ $logoPath }}" alt="{{ $company->name ?? 'System Logo' }}" {{ $attributes }}
        style="max-height: 100%; max-width: 100%; object-fit: contain;" />
@else
    <img src="{{ asset('images/logo.png') }}" alt="Logo" {{ $attributes }} 
        style="max-height: 100%; max-width: 100%; object-fit: contain;"
        onerror="this.style.display='none'; this.nextElementSibling.style.display='block';">
    <svg viewBox="0 0 50 50" xmlns="http://www.w3.org/2000/svg" {{ $attributes }} style="display: none;">
        <rect x="5" y="5" width="40" height="40" rx="5" fill="#3b82f6" />
        <text x="25" y="32" font-family="Arial" font-size="24" font-weight="bold" fill="white"
            text-anchor="middle">S</text>
    </svg>
@endif
