<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

class TldCreateRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->can('create', \App\Models\Tld::class);
    }

    public function rules(): array
    {
        return [
            'tld' => ['required', 'string', 'unique:tlds,tld', 'starts_with:.'],
            'min_years' => ['required', 'integer', 'min:1', 'max:10'],
            'max_years' => ['required', 'integer', 'min:1', 'max:10', 'gte:min_years'],
            'min_chars' => ['required', 'integer', 'min:1', 'max:63'],
            'max_chars' => ['required', 'integer', 'min:1', 'max:63', 'gte:min_chars'],
            'grace_period' => ['required', 'integer', 'min:0'],
            'redemption_period' => ['required', 'integer', 'min:0'],
            'order' => ['required', 'integer', 'min:1'],
            'registrar' => ['required', 'string'],
            'whois_provider' => ['required', 'string', 'in:public,registrar'],
            'lookup_provider' => ['required', 'string', 'in:public,registrar'],
            'adaptive_pricing' => ['boolean'],
            'is_premium' => ['boolean'],
            'dns_management' => ['boolean'],
            'id_protection' => ['boolean'],
            'epp_code' => ['boolean'],
            'email_forwarding' => ['boolean'],
            'status' => ['required', 'boolean'],

            // Fiyatlandırma validasyonu
            'pricing' => ['required', 'array'],
            'pricing.*' => ['array'],
            'pricing.*.*' => ['array'],
            'pricing.*.*.*' => ['array'],
            'pricing.*.*.*.amount' => ['nullable', 'numeric', 'min:0'],
            'pricing.*.*.*.active' => ['boolean'],

            // Diğer ücretler validasyonu
            'pricing_other' => ['required', 'array'],
            'pricing_other.*' => ['array'],
            'pricing_other.*.*' => ['array'],
            'pricing_other.*.*.amount' => ['nullable', 'numeric', 'min:0'],
            'pricing_other.*.*.active' => ['boolean'],
        ];
    }

    public function messages(): array
    {
        return [
            'tld.required' => __('admin/tlds.validation.tld_required'),
            'tld.unique' => __('admin/tlds.validation.tld_unique'),
            'tld.starts_with' => __('admin/tlds.validation.tld_must_start_with_dot'),
            'min_years.required' => __('admin/tlds.validation.min_years_required'),
            'min_years.min' => __('admin/tlds.validation.min_years_min'),
            'max_years.required' => __('admin/tlds.validation.max_years_required'),
            'max_years.gte' => __('admin/tlds.validation.min_years_greater_than_max'),
            'min_chars.required' => __('admin/tlds.validation.min_chars_required'),
            'min_chars.min' => __('admin/tlds.validation.min_chars_min'),
            'max_chars.required' => __('admin/tlds.validation.max_chars_required'),
            'max_chars.gte' => __('admin/tlds.validation.min_chars_greater_than_max'),
            'grace_period.min' => __('admin/tlds.validation.periods_cannot_be_negative'),
            'redemption_period.min' => __('admin/tlds.validation.periods_cannot_be_negative'),
            'order.required' => __('admin/tlds.validation.order_required'),
            'order.min' => __('admin/tlds.validation.order_must_be_positive'),
            'registrar.required' => __('admin/tlds.validation_registrar_module_required'),
            'pricing.required' => __('admin/tlds.validation.pricing_required'),
            'pricing.*.*.*.amount.min' => __('admin/tlds.validation.price_cannot_be_negative'),
            'pricing_other.*.*.amount.min' => __('admin/tlds.validation.price_cannot_be_negative'),
        ];
    }

    protected function prepareForValidation(): void
    {
        // Boolean değerleri düzelt
        $this->merge([
            'adaptive_pricing' => $this->boolean('adaptive_pricing'),
            'is_premium' => $this->boolean('is_premium'),
            'dns_management' => $this->boolean('dns_management'),
            'id_protection' => $this->boolean('id_protection'),
            'epp_code' => $this->boolean('epp_code'),
            'email_forwarding' => $this->boolean('email_forwarding'),
            'status' => $this->boolean('status'),
        ]);

        // Fiyatlandırma verilerini düzenle
        if ($this->has('pricing')) {
            $pricing = $this->input('pricing');
            foreach ($pricing as &$currencyData) {
                foreach ($currencyData as &$yearData) {
                    foreach ($yearData as &$segmentData) {
                        $segmentData['active'] = isset($segmentData['active']) && $segmentData['active'] == '1';
                        if (isset($segmentData['amount'])) {
                            $segmentData['amount'] = filter_var($segmentData['amount'], FILTER_VALIDATE_FLOAT);
                        }
                    }
                }
            }
            $this->merge(['pricing' => $pricing]);
        }

        // Diğer ücretler verilerini düzenle
        if ($this->has('pricing_other')) {
            $pricingOther = $this->input('pricing_other');
            foreach ($pricingOther as &$currencyData) {
                foreach ($currencyData as &$segmentData) {
                    $segmentData['active'] = isset($segmentData['active']) && $segmentData['active'] == '1';
                    if (isset($segmentData['amount'])) {
                        $segmentData['amount'] = filter_var($segmentData['amount'], FILTER_VALIDATE_FLOAT);
                    }
                }
            }
            $this->merge(['pricing_other' => $pricingOther]);
        }
    }
}
