@php
    $resolvedSeo = ($seo ?? null) instanceof \App\Support\Seo\SeoData
        ? $seo->toArray()
        : (is_array($seo ?? null)
            ? $seo
            : app(\App\Support\Seo\SeoFactory::class)->fromViewData(get_defined_vars()));

    $jsonLdEntries = collect($resolvedSeo['json_ld'] ?? [])->filter(fn ($schema) => is_array($schema) && $schema !== [])->values();
@endphp
<title>{{ $resolvedSeo['title'] ?? config('seo.default_title', 'Skinbase') }}</title>
@if(!empty($resolvedSeo['description']))
    <meta name="description" content="{{ $resolvedSeo['description'] }}" />
@endif
@if(config('seo.keywords_enabled', true) && !empty($resolvedSeo['keywords']))
    <meta name="keywords" content="{{ $resolvedSeo['keywords'] }}" />
@endif
@if(!empty($resolvedSeo['robots']))
    <meta name="robots" content="{{ $resolvedSeo['robots'] }}" />
@endif
@if(!empty($resolvedSeo['canonical']))
    <link rel="canonical" href="{{ $resolvedSeo['canonical'] }}" />
@endif
@if(!empty($resolvedSeo['prev']))
    <link rel="prev" href="{{ $resolvedSeo['prev'] }}" />
@endif
@if(!empty($resolvedSeo['next']))
    <link rel="next" href="{{ $resolvedSeo['next'] }}" />
@endif

<meta property="og:site_name" content="{{ $resolvedSeo['og_site_name'] ?? config('seo.site_name', 'Skinbase') }}" />
<meta property="og:type" content="{{ $resolvedSeo['og_type'] ?? 'website' }}" />
@if(!empty($resolvedSeo['og_title']))
    <meta property="og:title" content="{{ $resolvedSeo['og_title'] }}" />
@endif
@if(!empty($resolvedSeo['og_description']))
    <meta property="og:description" content="{{ $resolvedSeo['og_description'] }}" />
@endif
@if(!empty($resolvedSeo['og_url']))
    <meta property="og:url" content="{{ $resolvedSeo['og_url'] }}" />
@endif
@if(!empty($resolvedSeo['og_image']))
    <meta property="og:image" content="{{ $resolvedSeo['og_image'] }}" />
@endif
@if(!empty($resolvedSeo['og_image_alt']))
    <meta property="og:image:alt" content="{{ $resolvedSeo['og_image_alt'] }}" />
@endif

<meta name="twitter:card" content="{{ $resolvedSeo['twitter_card'] ?? config('seo.twitter_card', 'summary_large_image') }}" />
@if(!empty($resolvedSeo['twitter_title']))
    <meta name="twitter:title" content="{{ $resolvedSeo['twitter_title'] }}" />
@endif
@if(!empty($resolvedSeo['twitter_description']))
    <meta name="twitter:description" content="{{ $resolvedSeo['twitter_description'] }}" />
@endif
@if(!empty($resolvedSeo['twitter_image']))
    <meta name="twitter:image" content="{{ $resolvedSeo['twitter_image'] }}" />
@endif

@foreach($jsonLdEntries as $schema)
    <script type="application/ld+json">{!! json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG) !!}</script>
@endforeach