<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Mails\EmailsController;
use App\Models\User;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;

class RecoverPasswordController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('auth.recover_password');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            $check = User::where('email', $request->email)->first();
            if ($check) {
                $root_url = route('index');
                $code = sha1(time());
                $check->update(['verification_code' => $code]);
                $url = $root_url . "/" . "reset_password/" . $code;
                $message = 'Dear ' . $check->name . ', Welcome to unga group stock take software. Use the link below to reset your password';
                $subject = 'Reset Password';
                $link_name = 'Reset Password Link';
                EmailsController::create_account($check->name, $check->email, $code, $url, $message, $subject, $link_name);
                $res = [
                    'status' => 200,
                    'message' => 'Password reset link has been sent to your email!'
                ];
                return response()->json($res, 200);
            } else {
                $res = [
                    'status' => 500,
                    'message' => 'Email does not exist in our database!'
                ];
                return response()->json($res, 500);
            }
        } catch (\Throwable $th) {
            $res = [
                'status' => 500,
                'message' => $th->getMessage()
            ];
            return response()->json($res, 500);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}