<?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\Support\Facades\Hash;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Carbon;
use Laravel\Fortify\Fortify;

//Get Location By IP
use Adrianorosa\GeoLocation\GeoLocation;

use App\Models\User;
use App\Models\Login;

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

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Fortify::loginView(function () {
            return view('auth.login');
        });

        // Fortify::authenticateUsing(function (Request $request) {
        //     $user = User::where('username', $request->username)->first();
        //     if ($user && Hash::check($request->password, $user->password)) {
        //         $ipaddress = $request->getClientIp();
        //         $details = GeoLocation::lookup($ipaddress);
        //         $fullbrowser = $_SERVER['HTTP_USER_AGENT'];
        //         $pchbrowser = explode('(', $fullbrowser);
        //         $pchbrowser2 = explode(')', $pchbrowser[1]);
        //         $pchsystem = explode(')', $pchbrowser[1]);

        //         $browser1 = $pchbrowser[0];
        //         $browser2 = $pchbrowser2[1];
        //         $osystem = $pchsystem[0];

        //         User::where('username', $request->username)
        //             ->update([
        //                 'last_login_at' => Carbon::now()->toDateTimeString(),
        //                 'last_login_ip' => $ipaddress
        //             ]);
        //         Login::create([
        //             'iduser' => $user->iduser,
        //             'hostname' => $_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI'],
        //             'ipaddress' => $ipaddress,
        //             'country' => $details->getCountry(),
        //             'regional' => $details->getRegion(),
        //             'city' => $details->getCity(),
        //             'longitude' => $details->getLongitude(),
        //             'latitude' => $details->getLatitude(),
        //             'browser1' => $browser1,
        //             'browser2' => $browser2,
        //             'osystem' => $osystem,
        //         ]);
        //         return $user;
        //     }
        // });

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

        Fortify::requestPasswordResetLinkView(function () {
            return view('auth.forgot-password');
        });

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

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

        Fortify::createUsersUsing(CreateNewUser::class);
        Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
        Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
        Fortify::resetUserPasswordsUsing(ResetUserPassword::class);

        RateLimiter::for('login', function (Request $request) {
            return Limit::perMinute(5)->by($request->email . $request->ip());
        });

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