<?php

namespace App\Providers;

use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;

use Laravel\Fortify\Contracts\ResetPasswordViewResponse;

use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\Auth\PasswordResetLinkController;


use Laravel\Fortify\Fortify;

class FortifyServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->app->singleton(ResetPasswordController::class, function () {
            return new ResetPasswordController();
        });
    }

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

        Fortify::loginView(function () {
            return view('auth.login');
        });

        Fortify::registerView(function () {
            return view('auth.register');
        });

        Fortify::verifyEmailView(function () {
            return view('auth.verify-email');
        });


        Fortify::createUsersUsing(CreateNewUser::class);

        Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
        Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);



        RateLimiter::for('login', function (Request $request) {
            $email = (string) $request->email;

            return Limit::perMinute(5)->by($email . $request->ip());
        });

        RateLimiter::for('two-factor', function (Request $request) {
            return Limit::perMinute(5)->by($request->session()->get('login.id'));
        });


        Fortify::resetUserPasswordsUsing(ResetUserPassword::class);


        Fortify::requestPasswordResetLinkView(function () {
            return view('auth.forgot-password');
        });
        
        $this->registerPasswordResetResponseBindings();
        Fortify::resetPasswordView(function ($request) {
            return view('auth.reset-password', ['token' => $request->route('token')]);
        });

    }

    protected function registerPasswordResetResponseBindings()
    {
        $this->app->singleton(SendPasswordResetLinkViewResponse::class, function () {
            return view('auth.forgot-password');
        });

        $this->app->singleton(ResetPasswordViewResponse::class, function () {
            return view('auth.reset-password');
        });
    }
}
