<?php $attributes ??= new \Illuminate\View\ComponentAttributeBag;

$__newAttributes = [];
$__propNames = \Illuminate\View\ComponentAttributeBag::extractPropNames(([
    'pageType'    => 'home',
    'title'       => null,
    'description' => null,
    'image'       => null,
    'url'         => null,
    'type'        => 'website',
    'noindex'     => false,
    'articleData' => null, // For Article schema: published_at, author, category, image
    'breadcrumbData' => null, // For Breadcrumb schema: array of ['name' => '', 'url' => '']
    'faqData'     => null, // For FAQPage schema: array of ['question' => '', 'answer' => '']
]));

foreach ($attributes->all() as $__key => $__value) {
    if (in_array($__key, $__propNames)) {
        $$__key = $$__key ?? $__value;
    } else {
        $__newAttributes[$__key] = $__value;
    }
}

$attributes = new \Illuminate\View\ComponentAttributeBag($__newAttributes);

unset($__propNames);
unset($__newAttributes);

foreach (array_filter(([
    'pageType'    => 'home',
    'title'       => null,
    'description' => null,
    'image'       => null,
    'url'         => null,
    'type'        => 'website',
    'noindex'     => false,
    'articleData' => null, // For Article schema: published_at, author, category, image
    'breadcrumbData' => null, // For Breadcrumb schema: array of ['name' => '', 'url' => '']
    'faqData'     => null, // For FAQPage schema: array of ['question' => '', 'answer' => '']
]), 'is_string', ARRAY_FILTER_USE_KEY) as $__key => $__value) {
    $$__key = $$__key ?? $__value;
}

$__defined_vars = get_defined_vars();

foreach ($attributes->all() as $__key => $__value) {
    if (array_key_exists($__key, $__defined_vars)) unset($$__key);
}

unset($__defined_vars); ?>

<?php
    use App\Models\SeoSetting;
    use App\Models\AppSetting;

    $seo      = SeoSetting::where('page_type', $pageType)->first();
    $appSettings = AppSetting::getAllCached();

    $siteName    = $appSettings['site_name']        ?? config('app.name', 'AskeriPlatform');
    $siteUrl     = $appSettings['site_url']         ?? config('app.url');
    $siteDesc    = $appSettings['site_description'] ?? 'Askeri kariyer, KPSS hazırlık ve üniversite bilgileri için kapsamlı rehber.';
    $siteLogo    = $appSettings['site_logo']        ?? null;
    $twitterUser = $appSettings['contact_twitter']  ?? null;
    $gaId        = $appSettings['google_analytics_id'] ?? null;
    $gscCode     = $appSettings['google_search_console'] ?? null;
    $contactEmail = $appSettings['contact_email']   ?? null;

    // Priority: prop > seo db record > app setting / fallback
    $metaTitle       = $title       ?? $seo->meta_title       ?? $siteName;
    $metaDescription = $description ?? $seo->meta_description ?? $siteDesc;
    $canonicalUrl    = $url         ?? $seo->canonical_url    ?? request()->url();
    $ogImage         = $image       ?? $seo->og_image         ?? ($siteLogo ? asset($siteLogo) : null);
    $robotsValue     = $noindex     ? 'noindex, nofollow'
                                    : ($seo->robots ?? 'index, follow');
    $schemaMarkup    = $seo->schema_markup ?? null;
    $ogTags          = $seo->og_tags ?? [];

    // Ensure canonical URL is absolute
    if (!str_starts_with($canonicalUrl, 'http')) {
        $canonicalUrl = $siteUrl . '/' . ltrim($canonicalUrl, '/');
    }

    // Twitter handle (@username extraction)
    if ($twitterUser) {
        preg_match('/twitter\.com\/([^\/\?]+)/i', $twitterUser, $m);
        $twitterHandle = isset($m[1]) ? '@' . $m[1] : null;
    } else {
        $twitterHandle = null;
    }

    // Build article schema data
    $articleSchema = null;
    if ($type === 'article' && $articleData) {
        $articleSchema = [
            '@type' => 'Article',
            '@id' => $canonicalUrl . '#article',
            'headline' => $metaTitle,
            'description' => Str::limit($metaDescription, 160),
            'url' => $canonicalUrl,
            'inLanguage' => 'tr-TR',
            'isPartOf' => ['@id' => $siteUrl . '/#website'],
        ];
        
        if (isset($articleData['published_at'])) {
            $articleSchema['datePublished'] = $articleData['published_at'];
        }
        if (isset($articleData['modified_at'])) {
            $articleSchema['dateModified'] = $articleData['modified_at'];
        }
        if (isset($articleData['author'])) {
            $articleSchema['author'] = [
                '@type' => 'Person',
                'name' => $articleData['author']
            ];
        }
        if (isset($articleData['category'])) {
            $articleSchema['articleSection'] = $articleData['category'];
        }
        if ($ogImage) {
            $articleSchema['image'] = [
                '@type' => 'ImageObject',
                'url' => $ogImage
            ];
        }
        if (isset($articleData['word_count'])) {
            $articleSchema['wordCount'] = $articleData['word_count'];
        }
    }

    // Build breadcrumb schema
    $breadcrumbSchema = null;
    if ($breadcrumbData && is_array($breadcrumbData) && count($breadcrumbData) > 1) {
        $items = [];
        $position = 1;
        foreach ($breadcrumbData as $item) {
            $items[] = [
                '@type' => 'ListItem',
                'position' => $position++,
                'name' => $item['name'],
                'item' => str_starts_with($item['url'], 'http') ? $item['url'] : $siteUrl . '/' . ltrim($item['url'], '/')
            ];
        }
        $breadcrumbSchema = [
            '@type' => 'BreadcrumbList',
            '@id' => $canonicalUrl . '#breadcrumb',
            'itemListElement' => $items
        ];
    }

    // Build FAQ schema
    $faqSchema = null;
    if ($faqData && is_array($faqData) && count($faqData) > 0) {
        $questions = [];
        foreach ($faqData as $faq) {
            $questions[] = [
                '@type' => 'Question',
                'name' => $faq['question'],
                'acceptedAnswer' => [
                    '@type' => 'Answer',
                    'text' => $faq['answer']
                ]
            ];
        }
        $faqSchema = [
            '@type' => 'FAQPage',
            '@id' => $canonicalUrl . '#faq',
            'mainEntity' => $questions
        ];
    }
?>


<title><?php echo e($metaTitle); ?></title>
<meta name="title" content="<?php echo e($metaTitle); ?>">


<meta name="description" content="<?php echo e(Str::limit($metaDescription, 160)); ?>">


<meta name="robots" content="<?php echo e($robotsValue); ?>">


<link rel="canonical" href="<?php echo e($canonicalUrl); ?>">







<?php if($gscCode): ?>
    <meta name="google-site-verification" content="<?php echo e($gscCode); ?>">
<?php endif; ?>


<meta property="og:type"        content="<?php echo e($type); ?>">
<meta property="og:site_name"   content="<?php echo e($siteName); ?>">
<meta property="og:title"       content="<?php echo e($metaTitle); ?>">
<meta property="og:description" content="<?php echo e(Str::limit($metaDescription, 300)); ?>">
<meta property="og:url"         content="<?php echo e($canonicalUrl); ?>">
<meta property="og:locale"      content="tr_TR">
<?php if($type === 'article'): ?>
    <meta property="article:published_time" content="<?php echo e($articleData['published_at'] ?? now()->toAtomString()); ?>">
    <?php if(isset($articleData['modified_at'])): ?>
    <meta property="article:modified_time" content="<?php echo e($articleData['modified_at']); ?>">
    <?php endif; ?>
    <?php if(isset($articleData['author'])): ?>
    <meta property="article:author" content="<?php echo e($articleData['author']); ?>">
    <?php endif; ?>
    <?php if(isset($articleData['category'])): ?>
    <meta property="article:section" content="<?php echo e($articleData['category']); ?>">
    <?php endif; ?>
<?php endif; ?>
<?php if($ogImage): ?>
    <meta property="og:image"       content="<?php echo e($ogImage); ?>">
    <meta property="og:image:alt"   content="<?php echo e($metaTitle); ?>">
    <meta property="og:image:width" content="1200">
    <meta property="og:image:height"content="630">
    <meta property="og:image:secure_url" content="<?php echo e($ogImage); ?>">
    <meta property="og:image:type" content="image/jpeg">
<?php endif; ?>


<?php if(is_array($ogTags) && count($ogTags) > 0): ?>
    <?php $__currentLoopData = $ogTags; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <meta property="<?php echo e($key); ?>" content="<?php echo e($value); ?>">
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
<?php endif; ?>


<meta name="twitter:card"        content="<?php echo e($ogImage ? 'summary_large_image' : 'summary'); ?>">
<meta name="twitter:site"        content="<?php echo e($twitterHandle ?? '@' . parse_url($siteUrl, PHP_URL_HOST)); ?>">
<meta name="twitter:creator"     content="<?php echo e($twitterHandle ?? '@' . parse_url($siteUrl, PHP_URL_HOST)); ?>">
<meta name="twitter:title"       content="<?php echo e($metaTitle); ?>">
<meta name="twitter:description" content="<?php echo e(Str::limit($metaDescription, 200)); ?>">
<meta name="twitter:url"         content="<?php echo e($canonicalUrl); ?>">
<?php if($ogImage): ?>
    <meta name="twitter:image" content="<?php echo e($ogImage); ?>">
    <meta name="twitter:image:alt" content="<?php echo e($metaTitle); ?>">
<?php endif; ?>


<script type="application/ld+json">
{
    "@context": "https://schema.org",
    "@graph": [
        {
            "@type": "WebSite",
            "@id": "<?php echo e($siteUrl); ?>/#website",
            "url": "<?php echo e($siteUrl); ?>/",
            "name": "<?php echo e($siteName); ?>",
            "description": "<?php echo e($siteDesc); ?>",
            "inLanguage": "tr-TR",
            "publisher": { "@id": "<?php echo e($siteUrl); ?>/#organization" },
            "potentialAction": {
                "@type": "SearchAction",
                "target": {
                    "@type": "EntryPoint",
                    "urlTemplate": "<?php echo e($siteUrl); ?>/search?q={search_term_string}"
                },
                "query-input": "required name=search_term_string"
            }
        },
        {
            "@type": "Organization",
            "@id": "<?php echo e($siteUrl); ?>/#organization",
            "name": "<?php echo e($siteName); ?>",
            "url": "<?php echo e($siteUrl); ?>/",
            "description": "<?php echo e($siteDesc); ?>"
            <?php if($siteLogo): ?>
            ,"logo": {
                "@type": "ImageObject",
                "url": "<?php echo e(asset($siteLogo)); ?>"
            }
            <?php endif; ?>
            <?php if($contactEmail): ?>
            ,"email": "<?php echo e($contactEmail); ?>"
            <?php endif; ?>
            <?php if(!empty($appSettings['contact_phone'])): ?>
            ,"telephone": "<?php echo e($appSettings['contact_phone']); ?>"
            <?php endif; ?>
            <?php if(!empty($appSettings['contact_address'])): ?>
            ,"address": {
                "@type": "PostalAddress",
                "streetAddress": "<?php echo e($appSettings['contact_address']); ?>"
            }
            <?php endif; ?>
            ,"sameAs": [
                <?php if($twitterUser): ?>
                "<?php echo e($twitterUser); ?>"
                <?php endif; ?>
            ]
        }
        <?php if($type === 'article' && $canonicalUrl !== ($siteUrl . '/')): ?>
        ,{
            "@type": "WebPage",
            "@id": "<?php echo e($canonicalUrl); ?>/#webpage",
            "url": "<?php echo e($canonicalUrl); ?>",
            "name": "<?php echo e($metaTitle); ?>",
            "description": "<?php echo e(Str::limit($metaDescription, 160)); ?>",
            "isPartOf": { "@id": "<?php echo e($siteUrl); ?>/#website" },
            "inLanguage": "tr-TR",
            "about": { "@id": "<?php echo e($siteUrl); ?>/#organization" }
        }
        <?php endif; ?>
        <?php if($articleSchema): ?>
        ,<?php echo json_encode($articleSchema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>

        <?php endif; ?>
        <?php if($breadcrumbSchema): ?>
        ,<?php echo json_encode($breadcrumbSchema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>

        <?php endif; ?>
        <?php if($faqSchema): ?>
        ,<?php echo json_encode($faqSchema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>

        <?php endif; ?>
    ]
}
</script>


<?php if($schemaMarkup && is_array($schemaMarkup) && count($schemaMarkup) > 0): ?>
    <script type="application/ld+json">
        <?php echo json_encode($schemaMarkup, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); ?>

    </script>
<?php endif; ?>


<?php if($gaId): ?>
    <script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo e($gaId); ?>"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', '<?php echo e($gaId); ?>', {
            'anonymize_ip': true,
            'cookie_flags': 'SameSite=None;Secure',
            'send_page_view': true
        });
    </script>
<?php endif; ?>


<meta name="theme-color" content="#2563eb">
<meta name="msapplication-TileColor" content="#2563eb">
<meta name="format-detection" content="telephone=no">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<?php /**PATH C:\xampp\htdocs\askeribilgiler\resources\views\components\seo-meta.blade.php ENDPATH**/ ?>