<?php

namespace App\Http\Requests;

use App\Models\FactoryProduct;
use App\Models\FactoryUnit;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    /**
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        return [
            'name' => [
                'required',
                'string',
                'max:255',
                Rule::unique(FactoryProduct::class, 'name')
                    ->where('factory_unit_id', $this->integer('factory_unit_id')),
            ],
            'factory_unit_id' => [
                'required',
                'integer',
                Rule::exists(FactoryUnit::class, 'id')->where('is_active', true),
            ],
        ];
    }
}
