<?php

namespace App\Livewire\Settings\Modals;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use LivewireUI\Modal\ModalComponent;

class DeleteAccount extends ModalComponent
{
    public $password;

    public function delete()
    {
        $this->validate([
            'password' => ['required'],
        ], [
            'password.required' => t('settings.validation.password_required'),
        ]);

        $user = auth()->user();

        if (!Hash::check($this->password, $user->password)) {
            $this->addError('password', t('settings.validation.current_password_wrong'));
            return;
        }

        Auth::logout();

        request()->session()->invalidate();
        request()->session()->regenerateToken();

        $user->requestDeletion();

        return redirect()->route('homepage.index');
    }

    public function render()
    {
        return view('livewire.settings.modals.delete-account');
    }
}
