<?php

namespace App\Http\Requests\Admin\Settings;

use App\Models\ClientCustomField;
use Illuminate\Foundation\Http\FormRequest;

/**
 * @property string $name Özel alanın adı
 * @property string $type Özel alan tipi (input/select)
 * @property string|null $description Özel alan açıklaması
 * @property bool $is_required Zorunlu alan mı
 * @property bool $show_in_order Siparişlerde gösterilsin mi
 * @property bool $show_in_clientarea Müşteri panelinde gösterilsin mi
 * @property bool $show_in_invoice Faturalarda gösterilsin mi
 * @property array|null $values Select tipi için değerler
 */
class StoreClientCustomFieldRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->user()->can('create', ClientCustomField::class);
    }

    /**
     * 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', 'string', 'max:255'],
            'type' => ['required', 'string', 'in:input,select'],
            'description' => ['nullable', 'string'],
            'is_required' => ['sometimes', 'boolean'],
            'show_in_order' => ['sometimes', 'boolean'],
            'show_in_clientarea' => ['sometimes', 'boolean'],
            'show_in_invoice' => ['sometimes', 'boolean'],
            'client_locked' => ['sometimes', 'boolean'],
        ];
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'name' => __('admin/client-custom-fields.fields_name'),
            'type' => __('admin/client-custom-fields.fields_type'),
            'description' => __('admin/client-custom-fields.fields_description'),
            'is_required' => __('admin/client-custom-fields.fields_is_required'),
            'show_in_order' => __('admin/client-custom-fields.fields_show_in_order'),
            'show_in_clientarea' => __('admin/client-custom-fields.fields_show_in_clientarea'),
            'show_in_invoice' => __('admin/client-custom-fields.fields_show_in_invoice'),
        ];
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        // Boolean değerlerin doğru şekilde işlenmesi
        $this->merge([
            'is_required' => $this->boolean('is_required'),
            'show_in_order' => $this->boolean('show_in_order'),
            'show_in_clientarea' => $this->boolean('show_in_clientarea'),
            'show_in_invoice' => $this->boolean('show_in_invoice'),
            'client_locked' => $this->boolean('client_locked'),
        ]);
    }
}
