<?php

namespace App\Providers;

use App\Models\Setting;
use App\Services\GcmsUpdateService;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        if (file_exists(base_path('config/installed.lock'))) {
            try {
                if (Schema::hasTable('settings')) {
                    $p = Setting::get('reservation_public_prefix');
                    if (in_array($p, ['reservation', 'events'], true)) {
                        config(['reservation.public_prefix' => $p]);
                    }
                }
            } catch (\Throwable) {
                // installation / migrations en cours
            }
        }

        $customThemesPath = config('cms.custom_themes_path');
        if ($customThemesPath && is_dir($customThemesPath)) {
            View::prependLocation(dirname($customThemesPath));
        }

        View::composer('admin.layout', function ($view) {
            $banner = null;
            $user = Auth::user();
            if ($user && $user->canManageUsers()) {
                $banner = app(GcmsUpdateService::class)->getCachedStatusForBanner();
            }
            $view->with('gcmsUpdateBanner', $banner);
        });
    }
}
