<?php

namespace App\Http\Controllers\User;

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

class UserLoginController extends Controller
{
    function userLoginView()
    {
        return view('user.auth.login');
    }

    function userDashboard()
    {
        return view('user.dashboard');
    }

    function profile()
    {
        return view('user.profile');
    }


    function userChangePasswordView()
    {
        return view('user.auth.change-password');
    }


    //    function login(Request $request)
//    {
//        //Validate inputs
//        $input = $request->all();
//        $this->validate($request, [
//            'email' => 'required|email|exists:users,email',
//            'password' => 'required|min:5|max:30'
//        ], [
//            'email.exists' => 'This email is not exists on users table'
//        ]);
//
//
//        if (auth()->attempt(['email' => $input['email'], 'password' => $input['password']])) {
//            $user = auth()->user();
//
//            if ($user->hasRole('user')) {
//                return redirect()->route('user.dashboard')->with('success', 'Welcome to Dashboard');
//            } elseif ($user->hasRole('admin')) {
//                Auth::logout();
//                return redirect()->back()->with('error', 'Role not authorized');
//            } else {
//                return redirect()->back()->with('error', 'Role not authorized');
//            }
//
//        } else {
//            return redirect()->back()->with('error', 'Email and password are wrong');
//        }
//
//
//
//
//    }

    public function login(Request $request)
    {
        // Validate inputs
        $input = $request->all();
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:5|max:30'
        ]);

        // Check if the email exists in the database
        $user = \App\Models\User::where('email', $input['email'])->first();

        if ($user) {
            // Check if the password is correct
            if (auth()->attempt(['email' => $input['email'], 'password' => $input['password']])) {
                if ($user->hasRole('user')) {
                    return redirect()->route('user.dashboard')->with(
                        'success',
                        'Welcome to the dashboard!'
                    );
                } elseif ($user->hasRole('admin')) {
                    Auth::logout();
                    return redirect()->back()->with('error', 'Role not authorized.');

                } else {
                    return redirect()->back()->with('error', 'Role not authorized.');
                }
            } else {
                // Return specific error if password is incorrect
                return redirect()->back()->with('error', 'Password is incorrect.');
            }
        } else {
            // If email does not exist
            return redirect()->back()->with('error', 'This email does not exist in our records.');

        }
    }

    public function userUpdatePassword(Request $request)
    {

        $request->validate([
            'new_password' => ['required', 'min:8'],
            'confirm_password' => ['required', 'min:8', 'same:new_password'],
        ]);

        User::where(['email' => $request->email])
            ->update(['password' => Hash::make($request->new_password),]);

        return redirect()->back()->with('success', 'Your password has been changed!');

    }


    public function userProfileUpdate(Request $request)
    {
        $request->validate([
            'image' => ['mimes:png,jpg']
        ]);

        $user_id = $request->user_id;
        $user_profile_update = User::find($user_id);
        $user_profile_update->first_name = $request->first_name;

        if ($request->hasFile('user_profile')) {
            $user_profile_update->clearMediaCollection('user_profile');
            $user_profile_update->addMediaFromRequest('user_profile')->toMediaCollection('user_profile');
        }


        $user_profile_update->update();
        return redirect()->back()->with('success', 'User Profile is Updated');

    }


    function userLogout()
    {
        Auth::guard('web')->logout();
        return redirect()->route('front.index')->with('Logout successfully!');
    }

}
