<?php

namespace App\Http\Livewire\Pages\Auth;

use App\Models\User;
use Livewire\Component;
use Illuminate\Support\Facades\Password;

class ForgotPassword extends Component
{
    public $email;

    public $responseText;
    public $responseCode;

    public $rules = [
        'email' => ['required', 'email']
    ];

    public function updated($property)
    {
        $this->validateOnly($property);
    }

    public function forgot_password()
    {
        $user = User::firstWhere('email', $this->email);

        if (!$user) {
            return $this->addError('email', 'Email address not found'); // this works
        }

        $status = Password::sendResetLink(['email' => $this->email]);

        if ($status === Password::RESET_LINK_SENT) {
            $this->responseCode = 1;
            $this->responseText = 'Password reset link sent. check your email inbox or spam';
        } else {
            $this->responseCode = 0;
            $this->responseText = 'Could not sent password reset link now. try again later';
        }
    }

    public function render()
    {
        return view('livewire.pages.auth.forgot-password');
    }
}
