<?php

namespace App\Http\Controllers\UserAuth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\ResetPasswordRequest;
use App\Models\ResentActivity;
use App\Models\User;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Str;
use Carbon\Carbon;
use Illuminate\Support\Facades\Http;

class NewPasswordController extends Controller
{

    public function create(Request $request, $token)
    {
        $validation = $this->validateResetPasswordToken($request);
        if(!$validation){
            return redirect()->route('login.py')->with(['info_message' => 'Token is expired']);
        }
        return view('auth.reset-password', ['request' => $request]);
    }

        /**
     * Handle an incoming new password request.
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(ResetPasswordRequest $request)
    {

        // Here we will attempt to reset the user's password. If it is successful we
        // will update the password on an actual user model and persist it to the
        // database. Otherwise we will parse the error and return the response.
        $password = Hash::make($request->password);
        $status = Password::reset(
            $request->only('email', 'password', 'password_confirmation', 'token'),
            function (User $user) use ($request, $password) {
                $user->forceFill([
                    'password' => $password,
                    'remember_token' => Str::random(60),
                ])->save();

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

        $user = User::where('email' , $request->email)->first();
        $this->sendSMS($user, $password);
        $this->insertResentActivity($user);

        // If the password was successfully reset, we will redirect the user back to
        // the application's home authenticated view. If there is an error we can
        // redirect them back to where they came from with their error message.
        return $status == Password::PASSWORD_RESET
                    ? redirect()->route('login.py')->with('success_message', 'Password successfully changed')
                    : back()->withInput($request->only('email'))
                        ->withErrors(['email' => __($status)]);

    }










    protected function insertResentActivity($user) {
        ResentActivity::updateOrCreate(['user_id'=>$user->id],[
            'forgot_password_date' => Carbon::now()->format('Y-m-d')
        ]);
    }


    protected function sendSMS($user, $password) {
            
        if($user->sms_status == 1){
            Http::post('https://sms.af/api/UserLeapChangePassword/', [
            'email' => $user->email,
            'api_token' => $user->api_token,
            'password' => $password
        ]);
        
        }
    }


    protected function validateResetPasswordToken($request){
        $reset = DB::table('password_resets')->where('email', $request->email)->first();
        $success = true;
        if($reset and Carbon::parse($reset->created_at)->diffInHours(now()) > 2){
            DB::table('password_resets')->where('email', $request->email)->delete();
            $success = false;
        }
        if (!$reset || !Hash::check($request->token, $reset->token)) {
            $success = false;
        }
        return $success;
    }

}
