<?php

namespace App\Providers;

// use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Src\Modules\Auth\Interfaces\IModulesProvider;
use Illuminate\Auth\SessionGuard;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The model to policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [
        //
    ];

    /**
     * Register any authentication / authorization services.
     */
    public function boot(): void
    {
        Auth::extend('customSession', function ($app, $name, array $config) {
            $provider = Auth::createUserProvider($config['provider']);
            $session = $app['session.store'];
            $request = $app['request'];
            $cookie = $app['cookie'];

            $g = new SessionGuard('users', $provider, $session, $request);
            $g->setCookieJar($cookie);

            return $g;
        });
        Auth::provider('user-provider', function ($app) {
            /** @var IModulesProvider */
            $modulesProvider = $app->make(AppServiceProvider::ADMIN_MODULES);
            return $modulesProvider->getAuthFactory()->getLaravelFactory()->getUserProvider();
        });
    }
}
