<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Models\User;


class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::where('role', '!=', 'A')->get();
        return view('admin.users.index', compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
          'name' => 'required|string|max:255',
          'email' => 'required|string|email|max:60|unique:users',
          'mobile' => 'nullable|string|max:15|regex:/^[\d\+]+$/|unique:users',
          'password' => 'required|string|min:6',
        ]);

        $input = $request->all();

        User::create([
          'name' => $input['name'],
          'email' => $input['email'],
          'password' => bcrypt($input['password']),
          'mobile' => $input['mobile'],
          'address' => $input['address'],
          'city' => $input['city'],
          'role' => $input['role'],
        ]);
        return redirect('admin/users')->with('Users has been added.');
        // return back()->with('added', 'User has been added');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
      
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $user = User::findOrFail($request->id);
        $request->validate([
          'name' => 'required|string|max:255',
          'email' => 'required|string|email|max:60',
          'mobile' => 'nullable|string|max:15|regex:/^[\d\+]+$/',
        ]);

        $input = $request->all();
        if($input['password']){
          $password = bcrypt($input['password']);
        } else {
          $password = $user->password;
        }
       
        if (Auth::user()->role == 'A') {
          $user->update([
            'name' => $input['name'],
            'email' => $input['email'],
            'password' => $password,
            'mobile' => $input['mobile'],
            'address' => $input['address'],
            'city' => $input['city'],
            'role' => $input['role'],
          ]);
        } else if (Auth::user()->role == 'S') {
          $user->update([
            'name' => $input['name'],
            'email' => $input['email'],
            'password' => $password,
            'mobile' => $input['mobile'],
            'address' => $input['address'],
            'city' => $input['city'],
          ]);
        }

        return back()->with('updated', 'Student has been updated');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $user = User::findOrFail($id);
        $user->delete();
        return back()->with('deleted', 'User has been deleted');
    }

    public function edit_profile()
    {
        return view('admin.users.edit_profile');
    }

    public function update_profile(Request $request)
    {
        $auth = Auth::user();

        $request->validate([
            'new_email' => 'required|email|max:60|unique:users,email,' . $auth->id,
            'current_password' => 'required',
        ]);

        if (!Hash::check($request->current_password, $auth->password)) {
            return back()->withErrors(['current_password' => 'Incorrect current password']);
        }

        $auth->email = $request->new_email;
        $auth->save();

        return back()->with('success', __('Email has been updated'));
    }
   

          
      public function updatePassword(Request $request)
{
    $request->validate([
        'new_password' => 'required|string|min:6',
        'confirm_password' => 'required|string|same:new_password',
    ]);

    $user = Auth::user();
    $user->password = Hash::make($request->new_password);
    $user->save();
    return redirect()->back()->with('success', 'Password updated successfully!');
}

}