<?php

namespace Webkul\Support\Http\Requests;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Webkul\Support\Enums\UOMType;

class UOMRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $isUpdate = $this->isMethod('PUT') || $this->isMethod('PATCH');
        $requiredRule = $isUpdate ? ['sometimes', 'required'] : ['required'];

        return [
            'type'        => [...$requiredRule, 'string', Rule::enum(UOMType::class)],
            'name'        => [...$requiredRule, 'string', 'max:255'],
            'factor'      => [...$requiredRule, 'numeric', 'min:0'],
            'rounding'    => [...$requiredRule, 'numeric', 'min:0'],
            'category_id' => [...$requiredRule, 'integer', 'exists:unit_of_measure_categories,id'],
        ];
    }

    /**
     * Get body parameters for Scribe documentation.
     */
    public function bodyParameters(): array
    {
        return [
            'type'        => [
                'description' => 'UOM type (reference, bigger, smaller)',
                'example'     => 'reference',
            ],
            'name'        => [
                'description' => 'UOM name',
                'example'     => 'Kilogram',
            ],
            'factor'      => [
                'description' => 'Conversion factor',
                'example'     => 1.0,
            ],
            'rounding'    => [
                'description' => 'Rounding precision',
                'example'     => 0.01,
            ],
            'category_id' => [
                'description' => 'UOM category ID',
                'example'     => 1,
            ],
        ];
    }
}
