<?php

namespace App\Livewire\Forms\Studio\Setting;

use App\Models\User;
use App\Traits\WithMediaHandler;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use Livewire\Form;

class PasswordForm extends Form
{
    use WithMediaHandler;

    public ?User $user = null;

    public string $current_password = '';

    public string $new_password = '';

    public string $new_confirm_password = '';

    public function rules(): array
    {
        return [
            'current_password' => ['required', 'string'],
            'new_password' => ['required', 'string', Password::min(8)->letters()->mixedCase()->numbers()->symbols()->uncompromised()],
            'new_confirm_password' => ['required', 'string', 'same:new_password'],
        ];
    }

    public function validationAttributes(): array
    {
        return [
            'current_password' => 'kata sandi saat ini',
            'new_password' => 'kata sandi baru',
            'new_confirm_password' => 'konfirmasi kata sandi baru',
        ];
    }

    public function setUser(User $user)
    {
        $this->user = $user;
    }

    public function update()
    {
        $this->validate();

        $this->user->update([
            'password' => Hash::make($this->new_password),
        ]);
    }
}
