<?php

namespace App\Http\Requests\Invoicing;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use App\Rules\SAT\ValidSatCode;

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

    public function rules(): array
    {
        // Inputs Condicionales
        $cancelMotive   = $this->input('cancel_motive');

        return [
            'cancel_motive' => [
                'required',
                new ValidSatCode( table: 'catalog_items', column: 'item_key', wheres: ['catalog_code' => 'c_MotivoCancelacion'] )
            ],

            'substitution_invoice_id' => [
                Rule::requiredIf(fn () => $cancelMotive === '01'),
                'nullable',
                'integer',
                'exists:tax_invoices,id'
            ],
        ];
    }
}
