<?php

namespace App\Http\Requests;

use App\Models\ExpenseCategory;
use App\Support\ExpensePaymentMethod;
use App\Support\ExpenseStatus;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreExpenseRequest extends FormRequest
{
    public function authorize(): bool
    {
        return (bool) $this->user()?->can('expenses.create');
    }

    /**
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        return [
            'expense_category_id' => [
                'required',
                'integer',
                Rule::exists(ExpenseCategory::class, 'id')->where('is_active', true),
            ],
            'amount_original' => ['required', 'numeric', 'min:0.01'],
            'payment_method' => ['required', Rule::in(ExpensePaymentMethod::values())],
            'vendor_paid_to' => ['nullable', 'string', 'max:255'],
            'reference_number' => ['nullable', 'string', 'max:100'],
            'description' => ['nullable', 'string', 'max:2000'],
            'status' => ['required', Rule::in(ExpenseStatus::values())],
            'attachments' => ['nullable', 'array'],
            'attachments.*' => ['file', 'mimes:jpeg,jpg,png,webp,pdf', 'max:5120'],
        ];
    }
}
