<?php

namespace App\Providers;

use App\Auth\XenForoGuard;
use App\Auth\XenForoUser;
use App\Policies\TempFilePolicy;
use App\Proxy\VisitorProxy;
use App\Services\TemporaryFileService;
use App\Support\XenForoCauserResolver;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Spatie\Activitylog\Support\CauserResolver;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->app->bind(CauserResolver::class, XenForoCauserResolver::class );
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {

        \Auth::extend('xenforo', function ($app, $name, array $config) {
            return new XenForoGuard($app['request']);
        });

        \Gate::policy(TemporaryFileService::class, TempFilePolicy::class );

        Gate::define('is-admin', function (XenForoUser $user) {
            return $user->is_admin === 1;
        });
        Gate::define('is-mod', function (XenForoUser $user) {
            return $user->is_moderator === 1;
        });

        \View::composer('*', function ($view) {
            $view->with('VISITOR', new VisitorProxy( \Auth::user() ) );
        });

    }
}
