<?php

namespace App\Http\Controllers\Auth;

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

class RegisterController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest')->except(['showAdminRegisterForm', 'registerAdmin']);
        $this->middleware('auth')->only(['showAdminRegisterForm', 'registerAdmin']);
    }

    /**
     * Menampilkan form registrasi untuk pengguna
     */
    public function showRegistrationForm()
    {
        return view('auth.register');
    }

    /**
     * Menampilkan form registrasi untuk admin
     * Hanya admin yang bisa mengakses halaman ini
     */
    public function showAdminRegisterForm()
    {
        // Cek apakah user adalah admin
        if (!Auth::user()->is_admin) {
            abort(403, 'Unauthorized action.');
        }

        return view('auth.register_admin');
    }

    /**
     * Proses registrasi pengguna
     */
    public function register(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'phone' => 'required|string|max:20',
            'business_name' => 'required|string|max:255',
            'business_address' => 'required|string',
            'password' => 'required|string|min:8|confirmed',
        ]);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'phone' => $request->phone,
            'business_name' => $request->business_name,
            'business_address' => $request->business_address,
            'password' => Hash::make($request->password),
            'role' => 'pelanggan', // Default role untuk customer
        ]);

        return redirect()->route('login')->with('success', 'Pendaftaran berhasil, silakan login.');
    }

    /**
     * Proses registrasi admin
     * Hanya admin yang sudah ada yang bisa mendaftarkan admin baru
     */
    public function registerAdmin(Request $request)
    {
        // Cek apakah user adalah admin
        if (!Auth::user()->is_admin) {
            abort(403, 'Unauthorized action.');
        }

        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'phone' => 'required|string|max:20',
            'gender' => 'required|in:Laki-laki,Perempuan',
            'password' => 'required|string|min:8|confirmed',
        ]);

        User::create([
            'name' => $request->name,
            'email' => $request->email,
            'phone' => $request->phone,
            'gender' => $request->gender,
            'password' => Hash::make($request->password),
            'role' => 'admin',
            'is_admin' => true,
        ]);

        return redirect()->route('admin.users')->with('success', 'Admin baru berhasil ditambahkan!');
    }
}
