<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class RegisterUserController extends Controller
{
    public function showRegistrationForm()
    {
        return view('auth.register-filament'); // Buat view ini nanti
    }

    public function register(Request $request)
    {
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:6', 'confirmed'],
        ]);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
            'role' => 'admin_dusun',
            'is_approved' => false,     // Default status false (belum disetujui)
        ]);

        // Opsional: Redirect ke halaman tunggu persetujuan atau login
        // Auth::login($user); // Jangan langsung login jika perlu persetujuan

        return redirect()->route('register.success')->with('status', 'Akun Anda berhasil didaftarkan. Mohon tunggu persetujuan dari Admin.');
    }

    public function showRegistrationSuccess()
    {
        return view('auth.register-success');
    }
}