<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Password;

use Illuminate\Support\Str;

class ResetPasswordController extends Controller
{
    // public function create(Request $request, $token)
    // {
    //     return view('auth.reset-password', ['token' => $token, 'email' => $request->email]);
    // }
        
    public function reset(Request $request)
    {
        $request->validate([
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|min:8|confirmed',
        ]);

        $response = Password::reset(
            $request->only('email', 'password', 'password_confirmation', 'token'),
            function ($user, $password) {
                $user->forceFill([
                    'password' => bcrypt($password)
                ])->save();

                $user->setRememberToken(Str::random(60));
            }
        );

        if ($response == Password::PASSWORD_RESET) {
            return response()->json(['message' => 'Password reset successfully.']);
        } else {
            return response()->json(['message' => 'Failed to reset the password.'], 400);
        }
    }

}

// use App\Http\Controllers\ResetPasswordController;

// Route::post('/reset-password', [ResetPasswordController::class, 'reset'])->name('password.update');










