<?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:6',
            'alamat' => 'required|string|max:255',
            'telepon' => 'required|string|max:20',
            'keterangan' => 'nullable|string|max:255',
            'g-recaptcha-response' => 'required|captcha',
        ]);

        // Buat pengguna
        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
            'alamat' => $request->alamat,
            'telepon' => $request->telepon,
            'keterangan' => $request->keterangan,
            'role' => 'user', 
        ]);

    

        // Login pengguna secara otomatis (opsional)
        //auth()->login($user);

        // Arahkan ke halaman verifikasi
        return redirect()->route('home')->with('success', 'Akun berhasil didaftarkan, silakan verifikasi email Anda!');
    }
}