<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Collection;

use App\Notifications\Channels\SlackWebhookChannel;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Route;

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

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
   
        // Standard migrations.
        $this->loadMigrationsFrom(database_path('migrations'));

        // Slack notifications
        Notification::resolved(function (ChannelManager $service) {
            $service->extend('slack', fn ($app) => new SlackWebhookChannel());
        });

        // Custom methods, we're specifically adding the query method
        Route::macro('query', function ($uri, $action) {
            return Route::addRoute('QUERY', $uri, $action);
        });

        // Custom Blade Directives
        Blade::if('see', function ($_permissions) {
            return Auth::user()->canDo($_permissions);
        });

        // Custom collection handling
        Collection::macro('select', function ($keys) {
            return $this->map(function ($item) use ($keys) {
                return collect($item)->only($keys)->all();
            });
        });
    }
}
