<?php

namespace App\Http\Requests;

use App\Support\Tenancy;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CustomInvoiceRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->can('invoices.manage');
    }

    public function rules(): array
    {
        return [
            'patient_id' => ['required', Rule::exists('patients', 'id')->where('tenant_id', app(Tenancy::class)->id())],
            'discount_amount' => ['sometimes', 'integer', 'min:0'],
            'items' => ['required', 'array', 'min:1'],
            'items.*.description' => ['required', 'string', 'max:190'],
            'items.*.qty' => ['required', 'integer', 'min:1', 'max:100'],
            'items.*.unit_price' => ['required', 'integer', 'min:0', 'max:100000000'],
            'items.*.tooth' => ['nullable', 'integer', Rule::in(\App\Domain\Clinical\Fdi::all())],
        ];
    }
}
