<?php

namespace App\Domains\Catalog\Requests;

use App\Domains\Shared\Rules\ImageOrBase64Rule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'categoria_id' => ['required', 'integer'],
            'brand_id' => [
                'required',
                'integer',
                Rule::exists('brands', 'id')->where(
                    fn ($query) => $query->where('tenant_codigo', $this->route('tenant')?->codigo)
                ),
            ],
            'slug' => ['required', 'string', 'max:255', Rule::unique('productos', 'slug')],
            'nombre' => ['required', 'string', 'max:255'],
            'descripcion' => ['nullable', 'string'],
            'precio' => ['required', 'numeric', 'min:0'],
            'attribute_ids' => ['sometimes', 'array'],
            'attribute_ids.*' => [
                'required',
                'integer',
                Rule::exists('attribute', 'id')->where(
                    fn ($query) => $query->where('tenant_codigo', $this->route('tenant')?->codigo)
                ),
            ],
            'images' => ['sometimes', 'nullable', 'array'],
            'images.*' => ['required', new ImageOrBase64Rule()],
        ];
    }
}
