<?php

namespace App\Domains\Purchase\Requests;

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

class StartCheckoutRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'cart_id' => [
                'required_without:direct_items',
                Rule::prohibitedIf(fn (): bool => $this->has('direct_items')),
                'integer',
                'exists:carritos,id',
            ],
            'direct_items' => [
                'required_without:cart_id',
                Rule::prohibitedIf(fn (): bool => $this->has('cart_id')),
                'array',
                'min:1',
            ],
            'direct_items.*' => ['required', 'array'],
            'direct_items.*.catalog_item_id' => ['required', 'integer'],
            'direct_items.*.variant_id' => ['nullable', 'integer'],
            'direct_items.*.cantidad' => ['required', 'integer', 'min:1'],
            'dni' => ['prohibited'],
            'telefono' => ['prohibited'],
            'nombre_apellido' => ['prohibited'],
            'email' => ['prohibited'],
        ];
    }
}
