<?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\Str;
// use Illuminate\Auth\Events\PasswordReset;
// use App\Models\User;
// use Illuminate\Support\Facades\DB;
// use Carbon\Carbon;

// class PasswordResetController extends Controller
// {
//     // Tampilkan form untuk meminta reset password
//     public function showLinkRequestForm()
//     {
//         return view('auth.passwords.email'); // Sesuaikan jika menggunakan API
//     }

//     // Kirim email reset password
//     public function sendResetLinkEmail(Request $request)
//     {
//         $request->validate(['email' => 'required|email']);

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

//         return $status === Password::RESET_LINK_SENT
//             ? response()->json(['message' => __($status)], 200)
//             : response()->json(['error' => __($status)], 400);
//     }


//     // Tampilkan form reset password
//     public function showResetForm(Request $request, $token)
//     {
//         return view('auth.passwords.reset', [
//             'token' => $token,
//             'email' => $request->email
//         ]); // Sesuaikan jika menggunakan API
//     }

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

//             $status = Password::reset(
//                 $request->only('email', 'password', 'token'), // Hapus 'password_confirmation'
//                 function (User $user, string $password) {
//                     try {
//                         $user->forceFill([
//                             'password' => Hash::make($password)
//                         ])->setRememberToken(Str::random(60));

//                         $user->save();

//                         event(new PasswordReset($user));
//                     } catch (\Exception $e) {
//                         Log::error('Password reset callback error: ' . $e->getMessage());
//                         return response()->json([
//                             'error' => 'Error updating password. Please try again later.'
//                         ], 501);
//                     }
//                 }
//             );

//             return $status === Password::PASSWORD_RESET
//                 ? response()->json(['message' => __($status)], 200)
//                 : response()->json(['error' => __($status)], 400);

//         } catch (\Exception $e) {
//             Log::error('Password reset error: ' . $e->getMessage());
//             return response()->json([
//                 'error' => 'Error processing password reset. Please try again later.'
//             ], 502);
//         }
//     }

// }

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\Str;
use Illuminate\Auth\Events\PasswordReset;
use App\Models\User;

class PasswordResetController extends Controller
{
    // Menampilkan form permintaan reset password
    public function showLinkRequestForm()
    {
        return view('auth.passwords.email'); // View untuk mengirim link reset password
    }

    // Mengirim link reset password ke email
    public function sendResetLinkEmail(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
        ]);

        $user = User::where('email', $request->input('email'))->first();

        if (!$user) {
            return response()->json(['error' => 'Email not found.'], 404);
        }

        if (is_null($user->email_verified_at)) {
            return response()->json(['error' => 'Email not verified. Please verify your email first.'], 403);
        }
        

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

            $response = $status === Password::RESET_LINK_SENT
                ? ['message' => __('Password reset link sent successfully.')]
                : ['error' => __('Error sending password reset link.')];

            return response()->json($response, $status === Password::RESET_LINK_SENT ? 200 : 400);
        } catch (\Exception $e) {
            Log::error('Error sending reset link: ' . $e->getMessage());
            return response()->json(['error' => 'Error sending reset link. Please try again later.'], 500);
        }
    }


    // Menampilkan form reset password
    public function showResetForm(Request $request, $token)
    {
        return view('auth.passwords.reset', [
            'token' => $token,
            'email' => $request->email
        ]);
    }

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

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

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

            return $status === Password::PASSWORD_RESET
                ? response()->json(['status' => __($status)], 200) // Mengembalikan status sebagai JSON untuk frontend ReactJS
                : response()->json(['error' => __($status)], 400);
        } catch (\Exception $e) {
            Log::error('Error resetting password: ' . $e->getMessage());
            return response()->json(['error' => 'Error resetting password. Please try again later.'], 500);
        }
    }
}
