<?php
  
namespace App\Http\Controllers\Auth;
  
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Exception;
use Illuminate\Support\Facades\DB;
use Session;

use Carbon\Carbon; 
use App\Models\User;
use Illuminate\Support\Str;

use Mail;
use App\Mail\ForgetPassword;
  
class AuthController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        return view('Auth.login');
    }  

 

  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function registration()
    {
        return view('Auth.register');
    }
  
      
      
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function postLogin(Request $request)
    {
      
   
        $credentials = User::where('email', $request->email)->where('password', $request->password)
        ->first();

      $email=$request->email;
if($credentials) {
    Auth::login($credentials);
    
    if($request->user()->type==="admin"){
    return redirect('/admin')->withSuccess('Welcome Back');

    }
    return redirect('dashboard')->withSuccess('Welcome Back');
} else {
    return redirect("login")->withErrors('Oppes! You have entered invalid credentials');

}
    }

    /**
     * Write code on Method
     *
     * @return response()
     */




     public function showForgetPasswordForm()
     {
        return view('Auth.forgetPassword');
     }
 
     public function submitForgetPasswordForm(Request $request)
     {
         $request->validate([
             'email' => 'required|email|exists:users',
         ]);
         $email=$request->email;
 
         $token = Str::random(64);
 
         DB::table('forget_password')->insert([
             'email' => $email, 
             'token' => $token, 
             'created_at' => Carbon::now()
           ]);
          $name=DB::table('users')->where('email', $email)->value('name');
           $details = [
             'name' => $name,
             'email' =>$email,
             'token'=>$token
         ];
      
 
    
         Mail::to($email)->send(new ForgetPassword($details));
 
      
 
         return back()->with('success', 'We have e-mailed your password reset link!');
     }
 
 
 
 
     public function showResetPasswordForm($token) { 
         return view('Auth.forgetPasswordLink', ['token' => $token]);
      }
      public function submitResetPasswordForm(Request $request)
     {
         $request->validate([
             'password' => 'required|confirmed',
             'password_confirmation' => 'required'
         ]);
         $token=$request->token;
         $email = DB::table('forget_password')->where('token',$token)->value('email');
         if(!$email){
  
 
             return back()->with('warning', 'Invalid token!');
         }
 
         DB::table('users')->where('email',$email)->update([
             'password'=>$request->password
         ]);
         return redirect('/login')->with('success', 'Your password has been changed!');
         
     }

    public function logout() {
        Session::flush();
        Auth::logout();
  
        return Redirect('login');
    }



    public function change_pw(Request $request) {
        // Validate the request data
        $request->validate([
            'current_password' => 'required|string',
            'new_password' => 'required|string|min:6|confirmed', // Ensures new password confirmation
        ]);
    
        // Get the authenticated user
        $user = auth()->user();
        $id = auth()->user()->id;
    
        // Check if the current password matches the user's stored password
        if ($request->current_password===$request->user()->password) {
            DB::table('users')->where('id',$id)->update([
                'password'=>$request->new_password
            ]);
        }else{
            return back()->with('warning',"Old Password Not Found!");
        }
    
        // Update the user's password
        return back()->with('success',"Password Update Successfully!");
    
    }
    
    
}