<?php

namespace App\Providers;

use App\Listeners\SetAgentOfflineOnLogout;
use App\Services\NotificationService;
use App\Services\SendGridEmailService;
use App\Services\PdfService;
use App\Services\PrintQueueService;
use App\Services\WorkboardService;
use App\Support\Witness\WitnessQuestionnaire;
use Illuminate\Auth\Events\Logout;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {
    /**
     * Register any application services.
     */
    public function register(): void {
        $this->app->singleton(NotificationService::class);
        $this->app->singleton(PdfService::class);
        $this->app->alias(PdfService::class, 'pdf');
        $this->app->singleton('print-queue', PrintQueueService::class);
        $this->app->singleton(SendGridEmailService::class);
        $this->app->singleton(WorkboardService::class);
        $this->app->singleton(WitnessQuestionnaire::class, fn () => WitnessQuestionnaire::make());

        // These are dev-only dependencies excluded from package auto-discovery
        // (see composer.json "dont-discover") so their providers never land in
        // the cached manifest that ships to staging/production, where the
        // packages aren't installed. Register them by hand only in local
        // environments, where the classes actually exist. Each is guarded with
        // class_exists so a partial install never fatals.
        if ($this->app->environment('local')) {
            foreach ([
                \Laravel\Pail\PailServiceProvider::class,
                \NunoMaduro\Collision\Adapters\Laravel\CollisionServiceProvider::class,
                \Pest\Laravel\PestServiceProvider::class,
            ] as $devProvider) {
                if (class_exists($devProvider)) {
                    $this->app->register($devProvider);
                }
            }
        }
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void {
        Blade::anonymousComponentPath(resource_path('views/global'));

        Event::listen(Logout::class, SetAgentOfflineOnLogout::class);
    }
}
