@php
    $websiteSettingsService = app(\App\Services\WebsiteSettings::class);
    $websiteSettings = $websiteSettingsService->settings();
    $metaTitle = $title ?? $websiteSettings['seo_title'] ?? config('app.name', 'urless');
    $metaDescription = $description ?? $websiteSettings['seo_description'] ?? $websiteSettings['site_description'] ?? null;
    $canonicalUrl = $canonical ?? $websiteSettings['canonical_url'] ?? url()->current();
    $ogTitle = $ogTitle ?? $websiteSettings['og_title'] ?? $metaTitle;
    $ogDescription = $ogDescription ?? $websiteSettings['og_description'] ?? $metaDescription;
    $ogImageUrl = $ogImageUrl ?? $websiteSettingsService->ogImageUrl();
    $faviconUrl = $websiteSettingsService->faviconUrl();
    $twitterHandle = $websiteSettings['twitter_handle'] ?? null;
    $latitude = $websiteSettings['geo_latitude'] ?? null;
    $longitude = $websiteSettings['geo_longitude'] ?? null;
@endphp

<title>{{ $metaTitle }}</title>
@if ($metaDescription)
    <meta name="description" content="{{ $metaDescription }}">
@endif
@if ($websiteSettings['seo_keywords'] ?? null)
    <meta name="keywords" content="{{ $websiteSettings['seo_keywords'] }}">
@endif
@if ($websiteSettings['robots'] ?? null)
    <meta name="robots" content="{{ $websiteSettings['robots'] }}">
@endif
@if ($canonicalUrl)
    <link rel="canonical" href="{{ $canonicalUrl }}">
@endif
@if ($faviconUrl)
    <link rel="icon" href="{{ $faviconUrl }}">
@endif
@if ($websiteSettings['google_site_verification'] ?? null)
    <meta name="google-site-verification" content="{{ $websiteSettings['google_site_verification'] }}">
@endif
@if ($websiteSettings['bing_site_verification'] ?? null)
    <meta name="msvalidate.01" content="{{ $websiteSettings['bing_site_verification'] }}">
@endif
@if ($websiteSettings['default_country'] ?? null)
    <meta name="geo.country" content="{{ $websiteSettings['default_country'] }}">
@endif
@if ($websiteSettings['default_region'] ?? null)
    <meta name="geo.region" content="{{ $websiteSettings['default_region'] }}">
@endif
@if ($websiteSettings['default_city'] ?? null)
    <meta name="geo.placename" content="{{ $websiteSettings['default_city'] }}">
@endif
@if ($latitude && $longitude)
    <meta name="geo.position" content="{{ $latitude }};{{ $longitude }}">
    <meta name="ICBM" content="{{ $latitude }}, {{ $longitude }}">
@endif

<meta property="og:type" content="website">
<meta property="og:title" content="{{ $ogTitle }}">
@if ($ogDescription)
    <meta property="og:description" content="{{ $ogDescription }}">
@endif
@if ($canonicalUrl)
    <meta property="og:url" content="{{ $canonicalUrl }}">
@endif
@if ($ogImageUrl)
    <meta property="og:image" content="{{ $ogImageUrl }}">
@endif
<meta name="twitter:card" content="{{ $ogImageUrl ? 'summary_large_image' : 'summary' }}">
<meta name="twitter:title" content="{{ $ogTitle }}">
@if ($ogDescription)
    <meta name="twitter:description" content="{{ $ogDescription }}">
@endif
@if ($twitterHandle)
    <meta name="twitter:site" content="{{ $twitterHandle }}">
@endif
@include('partials.website-theme', ['websiteSettings' => $websiteSettings])
@if ($websiteSettings['google_analytics_id'] ?? null)
    <script async src="https://www.googletagmanager.com/gtag/js?id={{ $websiteSettings['google_analytics_id'] }}"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', '{{ $websiteSettings['google_analytics_id'] }}');
    </script>
@endif
@if ($websiteSettings['facebook_pixel_id'] ?? null)
    <script>
        !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
        n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
        n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
        t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, document,'script',
        'https://connect.facebook.net/en_US/fbevents.js');
        fbq('init', '{{ $websiteSettings['facebook_pixel_id'] }}');
        fbq('track', 'PageView');
    </script>
@endif
@if ($websiteSettings['custom_head_scripts'] ?? null)
    {!! $websiteSettings['custom_head_scripts'] !!}
@endif
