@props([
    'markClass' => 'brand-mark',
    'textClass' => 'brand-text',
    'imageClass' => 'block object-contain',
    'showText' => false,
])

@php
    $brandName = setting('general.application_name', config('app.name', 'RemoteMatch'));
    $logoPath = setting('branding.logo');
    $logoUrl = \App\Support\Branding::assetUrl($logoPath);
    $initials = \App\Support\Branding::initials($brandName);

    $logoWidthSetting = setting('branding.logo_width_px', 156);
    $logoHeightSetting = setting('branding.logo_height_px', 44);

    $logoWidth = is_numeric($logoWidthSetting) ? (int) $logoWidthSetting : 156;
    $logoHeight = is_numeric($logoHeightSetting) ? (int) $logoHeightSetting : 44;

    $logoWidth = max(24, min(960, $logoWidth));
    $logoHeight = max(20, min(360, $logoHeight));
    $logoDisplayWidth = $logoWidth;

    if (is_string($logoPath) && $logoPath !== '' && ! str_starts_with($logoPath, 'http')) {
        try {
            $absoluteLogoPath = \Illuminate\Support\Facades\Storage::disk('public')->path($logoPath);

            if (is_file($absoluteLogoPath)) {
                $dimensions = @getimagesize($absoluteLogoPath);

                if (is_array($dimensions) && ($dimensions[0] ?? 0) > 0 && ($dimensions[1] ?? 0) > 0) {
                    $aspectRatio = $dimensions[0] / $dimensions[1];
                    $logoDisplayWidth = max($logoDisplayWidth, (int) ceil($logoHeight * $aspectRatio));
                }
            }
        } catch (Throwable) {
            $logoDisplayWidth = $logoWidth;
        }
    }

    $logoDisplayWidth = min(960, $logoDisplayWidth);
    $logoStyle = "width: {$logoDisplayWidth}px; height: {$logoHeight}px; max-width: min({$logoDisplayWidth}px, 82vw); object-fit: contain;";
@endphp

@if ($logoUrl)
    <img src="{{ $logoUrl }}" alt="{{ $brandName }}" class="{{ $imageClass }}" style="{{ $logoStyle }}">
@else
    <span class="{{ $markClass }}" style="{{ $logoStyle }}">{{ $initials }}</span>
@endif

@if ($showText)
    <span class="{{ $textClass }}">{{ $brandName }}</span>
@endif
