<?php

namespace App\Providers;

use App\Services\SplashAdService;
use App\Support\SiteBranding;
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
    {
        $this->app->scoped(SiteBranding::class, function () {
            return SiteBranding::fromDatabase();
        });
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Schema::defaultStringLength(191);

        View::composer('*', function ($view) {
            $view->with('siteBrand', app(SiteBranding::class));
        });

        View::composer('patient.layouts.app', function ($view) {
            $pid = session('patient_id');
            $splash = $pid
                ? app(SplashAdService::class)->resolveForPatient((int) $pid)
                : null;
            $view->with('splashAd', $splash);
        });

        View::composer('doctor.layouts.app', function ($view) {
            $doc = $view->offsetExists('currentDoctor')
                ? $view->offsetGet('currentDoctor')
                : null;
            $splash = $doc
                ? app(SplashAdService::class)->resolveForDoctor((int) $doc->id)
                : null;
            $view->with('splashAd', $splash);
        });
    }
}
