<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CommuneRequest 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 = [
            'name' => [
                'required',
                'string',
                Rule::unique('communes')
            ],
            'department_id' => [
                'required',
                'string',
            ],
            'pf_name' => [
                'string',
                'nullable',
            ],
            'pf_poste' => [
                'string',
                'nullable',
            ],
            'pf_phone' => [
                'string',
                'nullable',
            ],
            'pf_email' => [
                'email',
                'nullable',
            ]
        ];

        if ($this->getMethod() == 'NULL') {
            $rules['name'] = [
                'required',
                'string',
                Rule::unique('communes')
                    ->ignore($this->route('commune')->id, 'id')
            ];
        }

        return $rules;
    }

    public function getCommunePayload()
    {
        return collect($this->validated())
            ->toArray();
    }
}
