<?php

namespace App\Domains\Cart\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $tenantCode = $this->route('tenant')?->codigo;

        return [
            'catalog_item_id' => [
                'required',
                'integer',
                Rule::exists('catalog_items', 'id')->where(
                    fn ($query) => $query->where('tenant_code', $tenantCode)
                ),
            ],
            'variant_id' => [
                'sometimes',
                'nullable',
                'integer',
                Rule::exists('variantes', 'id')->where(
                    fn ($query) => $query->where('catalog_item_id', $this->input('catalog_item_id'))
                ),
            ],
            'cantidad' => ['required', 'integer', 'min:1'],
        ];
    }
}
