@props(['page' => null, 'item' => null])

@php
    use App\Models\SeoData;
    use App\Models\SiteSetting;

    $settings = SiteSetting::pluck('setting_value', 'setting_key');
    $siteName = $settings['site_name'] ?? 'ECS';
    
    // Default fallback values
    $title = $siteName;
    $description = $settings['welcome_message'] ?? 'Extensive Creativity Solutions';

    $keywords = 'innovation, creativity, IT, construction, logistics';
    $ogImage = asset('ecs-logo.png');

    // If a specific page identifier is provided (home, about, etc.)
    if ($page) {
        $seo = SeoData::where('page_identifier', $page)->first();
        if ($seo) {
            $title = $seo->title;
            $description = $seo->description;
            $keywords = $seo->keywords;
            if ($seo->og_image_path) {
                $ogImage = asset($seo->og_image_path);
            }
        }
    }

    // If a dynamic item is provided (like a Blog Post) - future proofing
    if ($item) {
        $title = $item->title . ' | ' . $siteName;
        // Strip tags for description
        $description = Str::limit(strip_tags($item->content), 160);
        if (isset($item->image_path)) {
            $ogImage = asset($item->image_path);
        }
    }
@endphp

<!-- Primary Meta Tags -->
<title>{{ $title }}</title>
<meta name="title" content="{{ $title }}">
<meta name="description" content="{{ $description }}">
@if($keywords)
<meta name="keywords" content="{{ $keywords }}">
@endif

<!-- Open Graph / Facebook -->
<meta property="og:type" content="website">
<meta property="og:url" content="{{ url()->current() }}">
<meta property="og:title" content="{{ $title }}">
<meta property="og:description" content="{{ $description }}">
<meta property="og:image" content="{{ $ogImage }}">

<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:url" content="{{ url()->current() }}">
<meta property="twitter:title" content="{{ $title }}">
<meta property="twitter:description" content="{{ $description }}">
<meta property="twitter:image" content="{{ $ogImage }}">

<!-- Canonical Link -->
<link rel="canonical" href="{{ url()->current() }}">
