<?php

namespace App\Http\Controllers;

use App\Mail\Sendmail;
use App\Models\UserLogin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;

class UserRegistraionloginController extends Controller
{
    //
    function user_register_page(){
        return view("user.user_register");
    }
    function user_register(Request $request){
        $request->validate([
            'username'=>'required|string',
            
            'email'=>'required|unique:user_logins,email',
            'password'=>'required|min:8',
            'confirm_password'=>'required|min:8',
            'referred_by'=>'required',
         ]);
        $username = $request->username;
      $phonenumber = $request->phone_number;
      $randomNumber = str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT);

  $randomId = '#' . $request->username . $randomNumber;
 if($request->password == $request->confirm_password){
    if($request->referred_by){
        $mailData = [
            'title'=>'Welcome to oandafx Ai Trading!',
            'body'=>'We are thrilled to have you as part of our Ai Trading community, and we want to express our gratitude for choosing us. Your trust means the world to us, and we are committed to providing you with an exceptional experience.',

        ];
        Mail::to($request->email)->send(new Sendmail($mailData));
       
        UserLogin::create([
            'name'=>$request->username,
            'phone_number'=>$request->phone_number,
            'email'=>$request->email,
            'password'=>bcrypt($request->password),
            'Refer_id'=>$randomId,
            'Refer_by'=>$request->referred_by,

        ]);
        return back()->with('success',"Successfully Registered");
    }else{
        $mailData = [
            'title'=>'This is testing Purpose',
            'body'=>'Thanks For the registration',

        ];
        Mail::to($request->email)->send(new Sendmail($mailData));
        UserLogin::create([
            'name'=>$request->username,
            'phone_number'=>$request->phone_number,
            'email'=>$request->email,
            'password'=>bcrypt($request->password),
            'Refer_id'=>$randomId,
            

        ]);
        return back()->with('success',"Successfully Registered");
    }
 }else{
    return back()->with('error',"Sorry Password Not Matched");
 }
       
    }



    function user_login_page(){
        return view('user.user_login');
    }
    function user_login(Request $request){
        
        $request->validate([
            'email' => 'required',
            'password' => 'required',
        ]);
       $credentials = $request->only('email', 'password');

        
    
        // Attempt to log in the user
        if (Auth::guard('user_login')->attempt($credentials)) {
            return redirect()->route('user.dashboard')->with('success', 'Successfully Logged In');
            // return back()->with('success',"Successfully Logged In");
            
        } else {
            // return redirect()->route('home_page')->with('error', 'Invalid Mobile Number or Password');
            return back()->with('error',"Sorry Email Or Password Is Incorrect!!");
        }
    }
    function forget_pass_user(){
        return view('user.forget_pass.enter_mobile');
    }
    function user_pass_reset_email(Request $request){
        $randomNumber = rand(1000, 9999);
        $randomNumber = str_pad($randomNumber, 4, '0', STR_PAD_LEFT);
         $randomNumber;
      
      $user = UserLogin::where('email',$request->email)->first();
      if($user){
        $user->update([
            'code'=>$randomNumber,
        ]);
        $mailData = [
            'title'=>'This is testing Purpose',
            'body'=>'Your Pass Reset Code is .'.$randomNumber,

        ];
        Mail::to($request->email)->send(new Sendmail($mailData));
        return redirect()->route('user.give_code_reset',$user->id)->with('success',"Successfully Send The Code");
      }else{
        return redirect()->route('user.register.page')->with('erro',"Sorry Some information of yours were wrong!!");
      }
    }
    function user_give_code_reset($id){
        $user = UserLogin::find($id);
        return view('user.forget_pass.enter_code',[
            'user'=>$user,
        ]);
    }
    function user_code_confirm(Request $request,$id){
        $user = UserLogin::find($id);
        if($user->code == $request->code){
            return redirect()->route('pass.up_page',$id);
        }else{
            return redirect()->route('user.register.page');
        }
    }
    function pass_up_page($id){
        $user = UserLogin::find($id);
        return redirect()->route('user.pass_res',$id);
    }
    function user_pass_res($id){
        $user = UserLogin::find($id);
        return view('user.forget_pass.pass_update_page',[
            'user'=>$user,
        ]);
    }
    function user_pass_reset_confirmation(Request $request,$id){

        $request->validate([
           
            'password'=>'required|min:8',
            'confirm_password'=>'required|min:8',
         ]);
        if($request->password == $request->confirm_password){
            UserLogin::find($id)->update([
                'password'=>bcrypt($request->password)
            ]);
            return redirect()->route('user.login.page')->with('success',"Successfully change The Password");
        }else{
            return back()->with('error',"Please Check some thing is wrong");
        }
        
    }
}
