<?php

namespace App\Http\Controllers;

use App\Mail\ForgotPassword;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;

class AuthController extends Controller
{
    public function login()
    {
        return view('login');
    }

    public function loginproses(Request $request)
    {
        $credentials = $request->validate([
            'email' => 'required',
            'password' => 'required'
        ]);

        if (Auth::attempt($credentials)) {
            $request->session()->regenerate();
            return redirect()->intended('admin')->with('success', 'Login success');
        } else {
            return back()->with('error', 'Credential not match');
        } 
    }

    public function logout()
    {
        Auth::logout();
        return redirect('login')->with('success', 'Logout success');
    }

    public function register()
    {
        return view('register');
    }

    public function registersimpan(Request $request)
    {
        $request->validate([
            'username' => 'required|unique:users',
            'email' => 'required|unique:users',
            'password' => 'required|confirmed',
            'level' => 'required|in:Ketua,Bendahara', 
        ]);

        User::create([
            'username' => $request->username,
            'email' => $request->email,
            'password' => bcrypt($request->password),
            'level' => $request->level,
            'foto' => 'default.png',

        ]);

        return redirect('login')->with('success', 'User created successfully');
    }


    public function forgotpassword()
    {
        return view('forgotpassword');
    }

    public function forgotpasswordsend(Request $request)
    {
        $request->validate([
            'email' => 'required|email|exists:users,email',
        ]);

        // Generate token
        $token = Str::random(60);

        // Simpan token ke database
        $user = User::where('email', $request->email)->first();
        $user->password_token = $token;
        $user->save();

        // Kirim email dengan token
        Mail::to($user->email)->send(new ForgotPassword($token));

        return back()->with('success', 'Password reset link has been sent to your email.');
    }

    public function resetPasswordForm($token)
    {
        return view('resetpassword', ['token' => $token]);
    }

    public function resetPassword(Request $request)
    {
        $request->validate([
            'token' => 'required',
            'password' => 'required|confirmed',
        ]);

        $user = User::where('password_token', $request->token)->first();

        if (!$user) {
            return back()->with('error', 'Invalid token.');
        }

        // Reset password
        $user->password = bcrypt($request->password);
        $user->password_token = null; // Hapus token setelah digunakan
        $user->save();

        return redirect('login')->with('success', 'Password successfully reset. You can now login.');
    }
}
