<?php

namespace App\Http\Requests\Admin\Finance;

use App\Traits\NotificationTrait;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class DebtRequest extends FormRequest
{
    use NotificationTrait;

    public function authorize(): bool
    {
        return Auth::check();
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'amount' => removeRupiahFormatting($this->amount),
        ]);
    }

    public function rules(): array
    {
        return [
            'employee' => ['required', Rule::exists('users', 'id')],
            'amount' => ['required', 'numeric', 'min:0', 'max:99999999.99'],
        ];
    }

    protected function failedValidation(Validator $validator): void
    {
        $this->handleValidationFailure($validator);
    }
}
