<?php

namespace App\Livewire\Pages\Profile;

use Illuminate\View\View;
use Livewire\Component;

class PersonalInfo extends Component
{
    public string $name = '';

    public string $email = '';

    public function mount(): void
    {
        $this->name = auth()->user()->name;
        $this->email = auth()->user()->email;
    }

    public function save(): void
    {
        $this->validate([
            'name' => 'required|string|max:100',
            'email' => 'required|email|max:200|unique:users,email,'.auth()->id(),
        ]);

        auth()->user()->update([
            'name' => $this->name,
            'email' => $this->email,
        ]);

        session()->flash('status', 'Profile updated.');
    }

    public function render(): View
    {
        return view('livewire.pages.profile.personal-info')
            ->layout('layouts.nimuli-app', ['title' => 'Profile']);
    }
}
