<?php

namespace App\Http\Requests;

use App\Models\TreasuryCategory;
use Illuminate\Foundation\Http\FormRequest;

class TreasuryExitUpdateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules()
    {
        return [
            'category_id' => ['required', 'exists:treasury_categories,id', function ($attribute, $value, $fail) {
                $category = TreasuryCategory::find($value);
                if ($category && $category->type !== 'exit') {
                    $fail('La catégorie doit être de type "sortie".');
                }
            }],
            'date' => 'required|date',
            'amount' => 'required|numeric',
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'attachment' => 'nullable|file|mimes:pdf,jpg,png|max:2048',
        ];
    }
}
