<?php

namespace App\Http\Requests\Admin;

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

class DatacenterUpdateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        $datacenter = $this->route('datacenter');

        return $this->user()->can('update', $datacenter);
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $datacenter = $this->route('datacenter');

        return [
            'name' => 'required|string|max:255',
            'code' => [
                'required',
                'string',
                'max:255',
                Rule::unique('datacenters', 'code')->ignore($datacenter),
            ],
            'address' => 'nullable|string',
            'city' => 'nullable|string|max:255',
            'country_code' => 'nullable|string|size:2',
            'notes' => 'nullable|string',
        ];
    }

    /**
     * Get custom error messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'name.required' => __('admin/datacenters.validation_name_required'),
            'code.required' => __('admin/datacenters.validation_code_required'),
            'code.unique' => __('admin/datacenters.validation_code_unique'),
            'country_code.size' => __('admin/datacenters.validation_country_code_size'),
        ];
    }
}
