<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Support\EmailNormalizer;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Illuminate\View\View;

class PasswordResetLinkController extends Controller
{
    public function create(): View
    {
        $page_title = 'Forgot Password';
        $page_description = 'Request a password reset link';

        return view('iqec.auth.forgot-password', compact('page_title', 'page_description'));
    }

    public function store(Request $request): RedirectResponse
    {
        $request->merge([
            'email' => EmailNormalizer::normalize($request->input('email')),
        ]);

        $request->validate([
            'email' => ['required', 'email', 'lowercase'],
        ]);

        $status = Password::sendResetLink(
            $request->only('email')
        );

        return $status == Password::RESET_LINK_SENT
            ? back()->with('status', __($status))
            : back()->withInput($request->only('email'))
                ->withErrors(['email' => __($status)]);
    }
}
