<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class RegisterUserController extends Controller
{
    public function showRegistrationForm()
    {
        return view('auth.register');
    }

    public function register(Request $request)
    {
        // Validasi input
        $request->validate([
            'name' => 'required|string|max:191',
            'email' => 'required|email|unique:users',
            'password' => 'required|confirmed|min:8',
            'alamat' => 'required|string|max:255',
            'telepon' => 'required|string|max:20',
            'keterangan' => 'nullable|string|max:255',
            'g-recaptcha-response' => 'required|captcha',
        ]);
        User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password), // hash password wajib
            'alamat' => $request->alamat,
            'telepon' => $request->telepon,
            'keterangan' => $request->keterangan,
            'role' => 'user', // default role jika perlu
        ]);
        // Login pengguna secara otomatis (opsional)
        //auth()->login($user);

        // Arahkan ke halaman dengan pesan sukses
        return redirect()->route('login')->with('success', 'Akun berhasi didaftarkan, silahkan anda dapat login.');
    }
}