@php
    $brandVersion = $settings?->updated_at?->timestamp ?? $settings?->logo_media_id ?? $settings?->favicon_media_id ?? '1';
    $siteLogo = ($settings?->logo_url ?? asset('uploads/placeholders/default-logo.png')) . '?v=' . $brandVersion;
    $siteFavicon = ($settings?->favicon_url ?? asset('uploads/placeholders/saleem-sial-favicon.png')) . '?v=' . $brandVersion;
    $metaTitle = trim($__env->yieldContent('title', $settings->site_name ?? config('app.name', 'Saleem Sial Analytics')));
    $metaDescription = trim($__env->yieldContent('meta_description', $settings->footer_text ?? 'Saleem Sial Analytics builds reliable tracking, analytics, and reporting systems.'));
    $metaImage = trim($__env->yieldContent('meta_image', $siteLogo));
    $canonicalUrl = trim($__env->yieldContent('canonical', url()->current()));
    $frontendCssVersion = file_exists(public_path('assets/css/frontend.css')) ? filemtime(public_path('assets/css/frontend.css')) : '1';
    $frontendJsVersion = file_exists(public_path('assets/js/frontend.js')) ? filemtime(public_path('assets/js/frontend.js')) : '1';
    $schema = [
        '@context' => 'https://schema.org',
        '@type' => 'ProfessionalService',
        'name' => $settings->site_name ?? config('app.name', 'Saleem Sial Analytics'),
        'url' => url('/'),
        'logo' => $siteLogo,
        'image' => $metaImage,
        'description' => $metaDescription,
        'email' => $settings?->contact_email,
        'telephone' => $settings?->contact_phone,
        'sameAs' => array_values($settings?->social_links ?? []),
    ];
@endphp
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ $metaTitle }}</title>
    <meta name="description" content="{{ $metaDescription }}" />
    <meta name="robots" content="@yield('robots', 'index,follow')" />
    <link rel="canonical" href="{{ $canonicalUrl }}">
    <meta property="og:type" content="@yield('og_type', 'website')">
    <meta property="og:title" content="{{ $metaTitle }}">
    <meta property="og:description" content="{{ $metaDescription }}">
    <meta property="og:url" content="{{ $canonicalUrl }}">
    <meta property="og:image" content="{{ $metaImage }}">
    <meta property="og:site_name" content="{{ $settings->site_name ?? config('app.name', 'Saleem Sial Analytics') }}">
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="{{ $metaTitle }}">
    <meta name="twitter:description" content="{{ $metaDescription }}">
    <meta name="twitter:image" content="{{ $metaImage }}">
    <link rel="icon" href="{{ $siteFavicon }}">
    <script type="application/ld+json">{!! json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) !!}</script>

    @if(filled($settings?->gtm_container_id))
        <script>
            (function(w, d, s, l, i) {
                w[l] = w[l] || [];
                w[l].push({
                    'gtm.start': new Date().getTime(),
                    event: 'gtm.js'
                });
                var f = d.getElementsByTagName(s)[0],
                    j = d.createElement(s),
                    dl = l != 'dataLayer' ? '&l=' + l : '';
                j.async = true;
                j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
                f.parentNode.insertBefore(j, f);
            })(window, document, 'script', 'dataLayer', @js($settings->gtm_container_id));
        </script>
    @elseif(filled($settings?->ga_measurement_id))
        <script async src="https://www.googletagmanager.com/gtag/js?id={{ $settings->ga_measurement_id }}"></script>
        <script>
            window.dataLayer = window.dataLayer || [];
            function gtag() {
                dataLayer.push(arguments);
            }
            gtag('js', new Date());
            gtag('config', @js($settings->ga_measurement_id));
        </script>
    @endif

    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js" defer></script>

    <link rel="stylesheet" href="{{ asset('assets/themes/prespa/assets/css/core-add.css') }}">
    <link rel="stylesheet" href="{{ asset('assets/themes/prespa/build/css/main.css') }}">
    <link rel="stylesheet" href="{{ asset('assets/themes/prespa-saas/style.css') }}">
    <link rel="stylesheet" href="{{ asset('assets/css/frontend.css') . '?v=' . $frontendCssVersion }}">

    @stack('styles')
</head>
<body class="frontend-site {{ trim($__env->yieldContent('body_class')) }}">
    @if(filled($settings?->gtm_container_id))
        <noscript>
            <iframe src="https://www.googletagmanager.com/ns.html?id={{ $settings->gtm_container_id }}" height="0" width="0" style="display:none;visibility:hidden"></iframe>
        </noscript>
    @endif

    <div id="page" class="site">
        @include('partials.header')

        <main id="primary" class="site-main">
            @yield('content')
        </main>

        @include('partials.footer')

        @include('partials.support-chat')
    </div>

    <script src="{{ asset('assets/js/frontend.js') . '?v=' . $frontendJsVersion }}"></script>
    @stack('scripts')
</body>
</html>
