<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

class ProductConfigOptionUpdateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->user()->can('update', $this->route('productConfigOption'));
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'group_id' => 'required|exists:product_config_option_groups,id',
            'name' => 'required|string|max:255',
            'description' => 'nullable|string|max:1000',
            'type' => 'required|in:number,select,checkboxgroup,text,checkbox,ip_address',
            'order' => 'nullable|integer|min:0',
            'hidden' => 'nullable|boolean',
            'is_required' => 'nullable|boolean',
            'adaptive_term_pricing' => 'nullable|boolean',
            'ip_pool_override_id' => 'nullable|integer|exists:ip_pools,id',
            // Option value (number/checkbox için)
            'option_value' => 'nullable|string|max:255',
            // Grid tabanlı pricing doğrulamaları
            // Option-bazlı grid (number/checkbox)
            'pricing_grid_option' => 'sometimes|array',
            'pricing_grid_option.*' => 'sometimes|array',
            'pricing_grid_option.*.*' => 'nullable|numeric|min:0',
            // Değer-bazlı grid (select/checkboxgroup)
            'values_list' => 'sometimes|array',
            'values_list.*.id' => 'nullable|integer|exists:product_config_option_values,id',
            'values_list.*.value' => 'nullable|string|max:255',
            'pricing_grid' => 'sometimes|array',
            'pricing_grid.*' => 'sometimes|array', // valueIndex
            'pricing_grid.*.*' => 'sometimes|array', // currencyId
            'pricing_grid.*.*.*' => 'nullable|numeric|min:0', // period => amount
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'group_id.required' => __('admin/validation.product_config_option_group_id_required'),
            'group_id.exists' => __('admin/validation.product_config_option_group_id_exists'),
            'name.required' => __('admin/validation.product_config_option_name_required'),
            'name.max' => __('admin/validation.product_config_option_name_max'),
            'description.max' => __('admin/validation.product_config_option_description_max'),
            'type.required' => __('admin/validation.product_config_option_type_required'),
            'type.in' => __('admin/validation.product_config_option_type_in'),
            'minimum.integer' => __('admin/validation.product_config_option_minimum_integer'),
            'minimum.min' => __('admin/validation.product_config_option_minimum_min'),
            'maximum.integer' => __('admin/validation.product_config_option_maximum_integer'),
            'maximum.min' => __('admin/validation.product_config_option_maximum_min'),
            // values alanına dair mesajlar kaldırıldı
            'order.integer' => __('admin/validation.product_config_option_order_integer'),
            'order.min' => __('admin/validation.product_config_option_order_min'),
            'hidden.boolean' => __('admin/validation.product_config_option_hidden_boolean'),
            'is_required.boolean' => __('admin/validation.product_config_option_is_required_boolean'),
            'pricing.array' => __('admin/validation.product_config_option_pricing_array'),
            'pricing.*.period.required' => __('admin/validation.product_config_option_pricing_period_required'),
            'pricing.*.period.integer' => __('admin/validation.product_config_option_pricing_period_integer'),
            'pricing.*.period.min' => __('admin/validation.product_config_option_pricing_period_min'),
            'pricing.*.time.required' => __('admin/validation.product_config_option_pricing_time_required'),
            'pricing.*.time.in' => __('admin/validation.product_config_option_pricing_time_in'),
            'pricing.*.amount.required' => __('admin/validation.product_config_option_pricing_amount_required'),
            'pricing.*.amount.numeric' => __('admin/validation.product_config_option_pricing_amount_numeric'),
            'pricing.*.amount.min' => __('admin/validation.product_config_option_pricing_amount_min'),
            'pricing.*.setup_amount.numeric' => __('admin/validation.product_config_option_pricing_setup_amount_numeric'),
            'pricing.*.setup_amount.min' => __('admin/validation.product_config_option_pricing_setup_amount_min'),
            'pricing.*.currency_id.exists' => __('admin/validation.product_config_option_pricing_currency_id_exists'),
            'pricing.*.order.integer' => __('admin/validation.product_config_option_pricing_order_integer'),
            'pricing.*.order.min' => __('admin/validation.product_config_option_pricing_order_min'),
        ];
    }

    /**
     * Get custom attributes for validator errors.
     */
    public function attributes(): array
    {
        return [
            'group_id' => 'grup',
            'name' => 'opsiyon adı',
            'description' => 'açıklama',
            'type' => 'tip',
            'minimum' => 'minimum değer',
            'maximum' => 'maksimum değer',
            // values kaldırıldı
            'order' => 'sıralama',
            'hidden' => 'gizli',
            'is_required' => 'zorunlu',
            'pricing' => 'fiyatlandırma',
        ];
    }
}
