<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Invoices\Http\Requests;

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

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

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'type' => ['required', Rule::in(['invoice', 'credit_note'])],
            'customer_id' => ['required', 'exists:customers,id'],
            'credited_invoice_id' => [
                'nullable',
                'integer',
                'exists:invoices,id',
                Rule::requiredIf(fn (): bool => $this->input('type') === 'credit_note'),
            ],
            'invoice_date' => ['required', 'date'],
            'expiration_date' => ['required', 'date'],
            'description' => ['nullable', 'string'],
            'email_body' => ['nullable', 'string'],
            'lines' => ['array'],
            'lines.*.description' => ['required', 'string', 'max:20000'],
            'lines.*.quantity' => ['required', 'numeric', 'min:0.0001'],
            'lines.*.unit_price_ex_vat' => ['required', 'numeric'],
            'lines.*.vat_rate_id' => ['required', 'exists:maestro_vat_rates,id'],
            'lines.*.item_id' => ['nullable', 'integer'],
        ];
    }
}
