<?php

namespace App\Http\Requests;

use App\Enums\ChargeBasis;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreProductRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->hasPermission('products.create') ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'category_id' => ['required', 'integer', 'exists:product_categories,id'],
            'name' => ['required', 'string', 'max:255'],
            'code' => ['required', 'string', 'max:50', 'unique:products,code'],
            'description' => ['nullable', 'string'],
            'unit' => ['nullable', 'string', 'max:50'],
            'default_price' => ['required', 'numeric', 'min:0'],
            'default_tax_rate' => ['nullable', 'numeric', 'min:0'],
            'default_charge_basis' => ['required', Rule::enum(ChargeBasis::class)],
            'min_qty' => ['nullable', 'numeric', 'min:0'],
            'max_qty' => ['nullable', 'numeric', 'min:0', 'gte:min_qty'],
            'is_active' => ['sometimes', 'boolean'],
            'internal_notes' => ['nullable', 'string'],
        ];
    }
}
