<?php

namespace Modules\Auth\app\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Str;
use Hash;
use Modules\Auth\app\Models\User;

class ForgotController extends Controller
{
    protected function guard()
    {
        return Auth::guard('api');
    }

    protected function broker()
    {
        return CustomResetLink::broker('users');
    }
    public function sendResetLinkEmail(Request $request)
    {

        // dd($request->email);
        $request->validate(['email' => 'required|email']);
        $user = User::where('email', $request->email)->first();
        if ($user !== null) {
            $response = $this->broker()->sendResetLink($request->only('email'));

            return $response == CustomResetLink::RESET_LINK_SENT
                ? response()->json(['status' => true, 'mess' => 'Send email success'], Response::HTTP_OK)
                : response()->json(['status' => false, 'mess' => 'Send email failed'], Response::HTTP_OK);
        } else {
            return response()->json(['status' => false, 'mess' => 'E-mail not exists'], Response::HTTP_OK);
        }
    }

    public function showResetForm(Request $request, $token)
    {
        return view('admin.resetPassword')->with([
            'token' => $token,
            'email' => $request->email
        ]);
    }

    public function updatePassword(Request $request)
    {

        $messages = array(
            'email.required' => 'Email là bắt buộc.',
            'email.email' => 'Email không đúng định dạng.',
            'password.required' => 'Mật khẩu là bắt buộc.',
            'password.min' => 'Mật khẩu phải có ít nhất 8 ký tự.',
            'password.confirmed' => 'Mật khẩu không trùng khớp.',
            'password_confirmation.required' => 'Mật khẩu xác thực là bắt buộc.',
        );

        $validateData = [
            'email' => 'required|email',
            'password' => 'required|min:6|confirmed',
            'password_confirmation' => 'required'

        ];

        $request->validate($validateData, $messages);

        $status = Password::broker("users")->reset($request->only('email', 'password', 'password_confirmation', 'token'), function ($user, $password) {

            $user->forceFill([
                'password' => Hash::make($password)
            ])->setRememberToken(Str::random(60));

            $user->save();

            event(new PasswordReset($user));
        });

        return $status === Password::PASSWORD_RESET
            ? response()->json(['status' => true, 'mess' => 'Reset password success'], Response::HTTP_OK)
            : response()->json(['status' => false, 'mess' => 'Token has expired'], Response::HTTP_OK);
    }

}

class CustomResetLink extends Password
{
    const PASSWORD_RESET = "api.v1.password.reset";
}