@php
    use App\Models\SiteSetting;
    use Illuminate\Support\Facades\Storage;

    $siteSettings = SiteSetting::current();
    $defaultSiteName = filled($siteSettings->site_name) ? $siteSettings->site_name : config('app.name');
    $defaultSiteDescription = filled($siteSettings->site_description)
        ? $siteSettings->site_description
        : $defaultSiteName;
    $defaultOgImage = filled($siteSettings->og_image_path)
        ? Storage::disk('public')->url($siteSettings->og_image_path)
        : null;
    $defaultSeoTitle = filled($siteSettings->seo_title) ? $siteSettings->seo_title : $defaultSiteName;
    $defaultSeoDescription = filled($siteSettings->seo_description)
        ? $siteSettings->seo_description
        : $defaultSiteDescription;
    $defaultSeoKeywords = filled($siteSettings->seo_keywords) ? $siteSettings->seo_keywords : null;
    $defaultOgImageAlt = filled($siteSettings->og_image_alt) ? $siteSettings->og_image_alt : $defaultSiteName;
    $defaultTwitterSite = filled($siteSettings->twitter_site) ? $siteSettings->twitter_site : null;

    $seoTitle = trim($__env->yieldContent('seo_title')) ?: ($title ?? $defaultSeoTitle);
    $seoDescription = trim($__env->yieldContent('seo_description')) ?: $defaultSeoDescription;
    $seoKeywords = trim($__env->yieldContent('seo_keywords')) ?: ($defaultSeoKeywords ?? '');

    $explicitCanonical = trim($__env->yieldContent('canonical'));
    $canonical = $explicitCanonical !== '' ? $explicitCanonical : request()->url();

    $ogTitle = trim($__env->yieldContent('og_title')) ?: $seoTitle;
    $ogDescription = trim($__env->yieldContent('og_description')) ?: $seoDescription;
    $ogType = trim($__env->yieldContent('og_type')) ?: 'website';
    $ogImage = trim($__env->yieldContent('og_image')) ?: $defaultOgImage;
    $ogImageAlt = trim($__env->yieldContent('og_image_alt')) ?: $defaultOgImageAlt;

    $robots = trim($__env->yieldContent('seo_robots'));
    if ($robots === '') {
        $robots = app()->isLocal() ? 'noindex, nofollow' : 'index, follow';
    }

    $author = trim($__env->yieldContent('seo_author')) ?: '';
    $articlePublishedTime = trim($__env->yieldContent('article_published_time')) ?: '';
    $articleModifiedTime = trim($__env->yieldContent('article_modified_time')) ?: '';
    $twitterSite = trim($__env->yieldContent('twitter_site')) ?: ($defaultTwitterSite ?? '');
    $locale = str_replace('-', '_', app()->getLocale());
@endphp

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">

<title>{{ $seoTitle }}</title>
<meta name="description" content="{{ $seoDescription }}">
@if (filled($seoKeywords))
<meta name="keywords" content="{{ $seoKeywords }}">
@endif
@if (filled($author))
<meta name="author" content="{{ $author }}">
@endif
<meta name="robots" content="{{ $robots }}">
<link rel="canonical" href="{{ $canonical }}">

<link rel="icon" href="{{ asset('favicon.ico') }}">

<meta property="og:type" content="{{ $ogType }}">
<meta property="og:locale" content="{{ $locale }}">
<meta property="og:site_name" content="{{ $defaultSiteName }}">
<meta property="og:title" content="{{ $ogTitle }}">
<meta property="og:description" content="{{ $ogDescription }}">
<meta property="og:url" content="{{ $canonical }}">

@if (filled($ogImage))
<meta property="og:image" content="{{ $ogImage }}">
<meta property="og:image:alt" content="{{ $ogImageAlt }}">
@endif
@if (filled($articlePublishedTime))
<meta property="article:published_time" content="{{ $articlePublishedTime }}">
@endif
@if (filled($articleModifiedTime))
<meta property="article:modified_time" content="{{ $articleModifiedTime }}">
<meta property="og:updated_time" content="{{ $articleModifiedTime }}">
@endif

<meta name="twitter:card" content="{{ filled($ogImage) ? 'summary_large_image' : 'summary' }}">
<meta name="twitter:title" content="{{ $ogTitle }}">
<meta name="twitter:description" content="{{ $ogDescription }}">
@if (filled($twitterSite))
<meta name="twitter:site" content="{{ $twitterSite }}">
@endif
@if (filled($ogImage))
<meta name="twitter:image" content="{{ $ogImage }}">
@endif

@php
    $defaultSchema = [
        '@context' => 'https://schema.org',
        '@type' => 'WebPage',
        'name' => $seoTitle,
        'description' => $seoDescription,
        'url' => $canonical,
        'inLanguage' => app()->getLocale(),
    ];
@endphp
<script type="application/ld+json">{!! json_encode($defaultSchema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) !!}</script>

@yield('advanced_seo')
@stack('advanced_seo')
@yield('structured_data')
@stack('structured_data')
