<?php

namespace App\Http\Controllers\Backend;

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

class AuthController extends Controller
{
    public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => ['required', 'email'],
            'password' => ['required'],
        ]);

        try {
            if (Auth::attempt($validator->validated(), $request->boolean('remember'))) {
                $request->session()->regenerate();
                return redirect()->route('admin.dashboard');
            }

            return back()->withErrors([
                'email' => 'The provided credentials do not match our records.',
            ])->onlyInput('email');
        } catch (\Exception $e) {
            return back()->withErrors([
                'error' => 'An error occurred while trying to log in. Please try again.',
            ]);
        }
    }

    public function logout(Request $request)
    {
        Auth::logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        return redirect()->route('admin.login.form');
    }

    public function profile()
    {
        return view('Backend.Auth.profile');
    }

    public function updateProfile(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'name' => ['required', 'string', 'max:255'],
                'email' => ['required', 'email', 'unique:users,email,' . auth()->user()->id],
            ]);
            $user = User::find(auth()->user()->id);
            $user->update($validator->validated());
            toastr()->success('Profile updated successfully');
            return redirect()->route('admin.profile');
        } catch (\Exception $e) {
            toastr()->error('An error occurred while updating the profile. Please try again.');
            return redirect()->back()->withInput();
        }
    }

    public function updatePassword(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'current_password' => ['required'],
                'password' => ['required', 'string', 'min:6', 'confirmed'],
            ]);
            if (!Hash::check($validator->validated()['current_password'], auth()->user()->password)) {
                return back()->withErrors([
                    'current_password' => 'The provided current password is incorrect.',
                ])->onlyInput('current_password');
            }
            $user = User::find(auth()->user()->id);
            $user->update([
                'password' => Hash::make($validator->validated()['password']),
            ]);
            toastr()->success('Password updated successfully');
            return redirect()->route('admin.profile');
        } catch (\Exception $e) {
            toastr()->error('An error occurred while updating the password. Please try again.');
            return redirect()->back()->withInput();
        }
    }
}
