<?php

namespace App\Http\Requests;

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

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

    public function rules(): array
    {
        $clientCase = $this->route('client_case');

        return [
            'reference' => [
                'nullable',
                'string',
                'max:125',
                Rule::unique('contracts', 'reference')
                    ->where(fn ($q) => $q
                        ->where('client_case_id', optional($clientCase)->id)
                        ->whereNull('deleted_at')
                    ),
            ],
            'start_date' => ['required', 'date'],
            'type_id' => ['required', 'integer', 'exists:contract_types,id'],
            'description' => ['nullable', 'string', 'max:255'],
            'initial_amount' => ['nullable', 'numeric'],
            'balance_amount' => ['nullable', 'numeric'],
            'account_type_id' => ['nullable', 'integer', 'exists:account_types,id'],
        ];
    }
}
