<?php

namespace App\Http\Requests\Backoffice;

use App\Support\Backoffice\Sanitizer\Traits\SanitizesInput;
use Illuminate\Foundation\Http\FormRequest;

class AddressCreateUpdateRequest extends FormRequest
{
    use SanitizesInput;

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'type' => 'required|in:company,billing,shipping',
            'sameAsCompany' => 'required:boolean',
            'name' => 'required_if:sameAsCompany,0|max:100',
            'address' => 'required_if:sameAsCompany,0|max:255',
            'floor' => 'nullable|max:255',
            'postalCode' => 'required_if:sameAsCompany,0|max:15',
            'location' => 'required_if:sameAsCompany,0|max:100',
            'countryId' => 'nullable:required_if:sameAsCompany,0|integer|exists:countries,id',
            'phonePrefix' => 'required_if:sameAsCompany,0|max:10',
            'phone' => 'required_if:sameAsCompany,0|max:20',
        ];
    }

    /**
     * Filters to be applied to the input.
     *
     * @return array
     */
    public function filters(): array
    {
        return [
            'name' => "trim|strip_tags|xss_clean",
            'address' => "trim|strip_tags|xss_clean",
            'floor' => "trim|strip_tags|xss_clean",
            'postalCode' => "trim|strip_tags|xss_clean",
            'location' => "trim|strip_tags|xss_clean",
            'phonePrefix' => "trim|strip_tags|xss_clean",
            'phone' => "trim|strip_tags|xss_clean",
        ];
    }
}
