<?php

namespace App\Http\Controllers\Backend\User;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Traits\UploadAble;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Illuminate\View\View;

class ProfileController extends Controller
{
    use UploadAble;
    /**
     * Show the form for editing the specified resource.
     *
     * @return Application|Factory|\Illuminate\Contracts\View\View|View
     */
    public function edit()
    {
        return view('backend.user.profile.edit');
    }
    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @return RedirectResponse
     * @throws ValidationException
     */
    public function update(Request $request): RedirectResponse
    {
        $this->validate($request, [
            'name' => 'required|string|max:255',
            'profile_photo' => 'nullable|image|mimes:jpg,png,jpeg,gif,svg|max:2048'
        ]);
        $user = User::findOrFail(auth()->user()->id);

        if ($request->has('profile_photo') && ($request->profile_photo instanceof UploadedFile)) {
            if ($user->profile_photo !== null) {
                $this->deleteOne($user->profile_photo);
            }
            $image = $this->uploadOne($request->profile_photo, 'profile_photo');
        } else {
            $image = $user->profile_photo;
        }
        $user->update([
            'name' => $request->name,
            'address' => $request->address,
            'phone' => $request->phone,
            'profile_photo' => $image,
            'gender' => $request->gender
        ]);
        return redirect()->back()->with(['alert-type' => 'success', 'message' => 'Your Profile has been updated successfully']);
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @return Application|Factory|\Illuminate\Contracts\View\View
     */
    public function editPassword()
    {
        return view('backend.user.profile.change_password');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @return RedirectResponse
     * @throws ValidationException
     */
    public function updatePassword(Request $request): ?RedirectResponse
    {
        $this->validate($request, [
            'old_password' => 'required',
            'password' => 'required|confirmed',
        ]);
        $hashedPassword = auth()->user()->password;
        if (Hash::check($request->old_password, $hashedPassword)) {
            if (!Hash::check($request->password, $hashedPassword)) {
                $admin = User::find(auth()->user()->id);
                $admin->password = Hash::make($request->password);
                $admin->save();
                auth()->logout();
                $request->session()->invalidate();
                return redirect()->route('login')->with(['alert-type' => 'success', 'message' => 'Password Successfully Changed']);
            }
            return back()->with(['alert-type' => 'error', 'message' => 'New password cannot be the same as old password.']);
        }
        return back()->with(['alert-type' => 'error', 'message' => 'Current password not match.']);
    }
}
