<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class Register extends Component
{
    public $name, $last_name, $email, $password, $confirm_password, $phne, $country;

    public function register()
    {
        $this->validate([
            'name' => 'required|string|max:255',
            'last_name' => 'nullable|string|max:255',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|string|min:6',
            'confirm_password' => 'required|same:password',
            'phne' => 'nullable|string|max:20',
            'country' => 'nullable|string|max:255',
        ]);

        User::create([
            'name' => $this->name,
            'last_name' => $this->last_name,
            'email' => $this->email,
            'password' => Hash::make($this->password),
            'phne' => $this->phne,
            'country' => $this->country,
            'email_verified_at' => now(), // Optional
        ]);

        session()->flash('success', 'Registration successful.');
        return redirect('/');
    }

    public function render()
    {
        return view('livewire.register');
    }
}
