<?php

namespace App\Http\Controllers\User;

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

class ProfileControllers extends Controller
{
    public function index()
    {
        return view('user.profile.index');
    }

    public function update(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'nama_pangkalan' => 'required',
            'alamat' => 'required',
            'email' => 'required|email|unique:users,email,' . Auth::id(),
        ]);

        $user = User::findOrFail(Auth::id());

        $user->update([
            'name' => $request->name,
            'nama_pangkalan' => $request->nama_pangkalan,
            'alamat' => $request->alamat,
            'email' => $request->email,
        ]);

        return back()->with('success', 'Profil berhasil diperbarui');
    }

    public function updatePassword(Request $request)
    {
        $request->validate([
            'password' => 'required|min:8|confirmed'
        ]);

        $user = User::findOrFail(Auth::id());

        $user->update([
            'password' => Hash::make($request->password)
        ]);

        return back()->with('success', 'Password berhasil diubah');
    }
}