<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Cache;

class GlobalViewServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        View::composer('*', function ($view) {

            $siteSettings = Cache::remember(
                'site_settings',
                86400,
                fn() =>
                \App\Models\SiteSetting::first()
            );

            $socialLinks = Cache::remember(
                'social_links',
                86400,
                fn() =>
                \App\Models\SocialMediaLink::where('status', 1)
                    ->orderBy('sort_order')
                    ->get()
            );

            $latestPosts = Cache::remember(
                'latest_posts',
                600,
                fn() =>
                \App\Models\BlogPost::where('status', 1)
                    ->latest('published_at')
                    ->take(5)
                    ->get()
            );
            $breakingNews = \App\Models\BlogPost::where('status', 1)
                ->where('is_featured', 1)
                ->latest('published_at')
                ->take(5)
                ->get();

            $view->with(compact('siteSettings', 'socialLinks', 'latestPosts', 'breakingNews'));
        });
    }
}
