<?php

namespace App\Providers;

use App\Models\Setting;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Vite; 
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        //
    }

    public function boot(): void
    {

        if (!Schema::hasTable('settings')) {
            return;
        }

        $seo = Cache::rememberForever('seo_settings', function () {
            return Setting::where('group', 'seo')->pluck('value', 'key')->toArray();
        });
        
        $scripts = Cache::rememberForever('global_scripts', function () {
            return Setting::where('group', 'scripts')->pluck('value', 'key')->toArray();
        });

        $pwa = Cache::rememberForever('pwa_settings', function () {
            return Setting::where('group', 'pwa')->pluck('value', 'key')->toArray();
        });

        $appearance = Cache::rememberForever('global_appearance_css', function () {
            return Setting::where('group', 'appearance')->pluck('value', 'key')->toArray();
        });

        View::share('seo', $seo);
        View::share('scripts', $scripts);
        View::share('pwa', $pwa);
        View::share('appearance', $appearance); 
    }
}