<?php

namespace SpondonIt\SchoolService\Requests;

use Illuminate\Foundation\Http\FormRequest;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {

        $rules = array();

        $rules = [
            'email'                 => 'required|email',
            'staff_no'              => ['required', 'string', 'max:50', staffIdentifierValidationRule()],
            'password'              => 'required|min:6',
            'password_confirmation' => 'required|same:password',
        ];

        return $rules;
    }

    protected function prepareForValidation()
    {
        if ($this->has('staff_no')) {
            $this->merge([
                'staff_no' => normalizeAdmissionNumber($this->staff_no),
            ]);
        }
    }

    public function messages()
    {
        return [
            'staff_no.regex' => 'Enter a valid Staff ID, e.g. BPA-STA-001-2026.',
        ];
    }

    /**
     * Translate fields with user friendly name.
     *
     * @return array
     */
    public function attributes()
    {
        return [

            'email'                 => trans('advocate::install.email'),
            'staff_no'              => 'Staff ID',
            'password'              => trans('advocate::install.password'),
            'password_confirmation' => trans('advocate::install.password_confirmation'),
        ];
    }
}
