<?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 ExpenseRequest 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
    {
        $isEdit = $this->isMethod('put');

        return [
            'name' => ['required', 'string', 'max:100'],
            'amount' => ['required', 'numeric', 'min:0', 'max:99999999.99'],
            'proof' => $isEdit
                ? ['nullable', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048']
                : ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'],
            'use_cash' => ['required', Rule::in(['1', '2'])],
        ];
    }

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