<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use App\Models\User;

class CustomPasswordResetController extends Controller
{
    public function showResetForm(Request $request, $token)
    {
        return view('emails.password_reset', [
            'token' => $token,
            'email' => $request->query('email'),
        ]);
    }

    // Send Reset Link
    public function sendResetLink(Request $request)
    {
        $request->validate(['email' => 'required|email']);

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

        if ($status === Password::RESET_LINK_SENT) {
            return back()->with('status', __($status));
        }

        return back()->withErrors(['email' => __($status)]);
    }

    // Reset Password
    public function resetPassword(Request $request)
    {
        $request->validate([
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed|min:8',
        ]);

        $status = Password::reset(
            $request->only('email', 'password', 'password_confirmation', 'token'),
            function ($user, $password) {
                $user->forceFill([
                    'password' => Hash::make($password),
                ])->save();
            }
        );

        return $status === Password::PASSWORD_RESET
            ? redirect('/')->with('status', 'Password reset successfully!')
            : back()->withErrors(['email' => [__($status)]]);
    }



    public function send()
    {
        Mail::raw('Hellow every one', function ($message) {
            $message->to('no-reply@gcc-demo.com')
                ->subject('Welcome')
                ->from('no-reply@gcc-demo.com');
        });
        return ' Email sent succesfully';
    }
}
