<?php

namespace App\Http\Requests\Api\V1\Product;

use Illuminate\Foundation\Http\FormRequest;

class StoreInnerOptionItemRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'name' => 'required|string|max:255',
            'price' => 'nullable|numeric|min:0',
            'price_modifier' => 'nullable|numeric|min:0', // Accept both for compatibility
            'sort_order' => 'integer|min:0',
            'is_active' => 'boolean',
        ];
    }

    /**
     * Handle both price and price_modifier for compatibility
     */
    public function validated($key = null, $default = null): array
    {
        $data = parent::validated($key, $default);

        // Map price_modifier to price if provided
        if (isset($data['price_modifier']) && !isset($data['price'])) {
            $data['price'] = $data['price_modifier'];
        }
        unset($data['price_modifier']);

        return $data;
    }
}
