<?php

namespace App\Http\Requests\AdminAccount;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Hash;

class UpdateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'name' => 'required',
            'email' => 'required|email',
            'confirm' => 'same:password'
        ];
    }

    public function handleRequest(){
        $allRequest = $this->all();
        if($this->password != null){
            $allRequest['password'] = Hash::make($this->password);
        }else{
            $allRequest['password'] = $this->old_password;
        }
        return $allRequest;
    }
}
