<?php

namespace App\Http\Requests\Loan;

use App\Models\Tenants\Loan;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class LoanUpdateRequest 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(): array
    {
        return [
            'user_id' => ['sometimes', 'integer', 'exists:users,id'],
            'loan_date' => ['sometimes', 'date', 'date_format:Y-m-d'],
            'amount' => ['sometimes', 'numeric', 'min:0', 'max:999999999999.99'],
            'purpose' => ['nullable', 'string', 'max:1000'],
            'withdrawal_id' => ['nullable', 'integer', 'exists:withdrawals,id'],
            'category' => ['sometimes', 'string', Rule::in(Loan::CATEGORIES)],
            'status' => ['sometimes', 'string', Rule::in(Loan::STATUSES)],
            'settlement_date' => ['nullable', 'date', 'date_format:Y-m-d'],
            'metadata' => ['nullable', 'array'],
            'metadata.serial_number' => ['nullable', 'string', 'max:100'],
            'metadata.cert_name' => ['nullable', 'string', 'max:200'],
            'metadata.vendor_id' => ['nullable', 'string', 'max:50'],
            'metadata.vendor_name' => ['nullable', 'string', 'max:200'],
            'metadata.purchase_purpose' => ['nullable', 'string', 'max:50'],
            'metadata.entertainment_expense' => ['nullable', 'string', 'max:50'],
            'metadata.recipient_name' => ['nullable', 'string', 'max:100'],
            'metadata.recipient_organization' => ['nullable', 'string', 'max:200'],
            'metadata.usage_description' => ['nullable', 'string', 'max:1000'],
            'metadata.memo' => ['nullable', 'string', 'max:2000'],
        ];
    }

    /**
     * Get the validation attribute names.
     *
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'user_id' => __('validation.attributes.user_id'),
            'loan_date' => __('validation.attributes.loan_date'),
            'amount' => __('validation.attributes.amount'),
            'purpose' => __('validation.attributes.purpose'),
            'withdrawal_id' => __('validation.attributes.withdrawal_id'),
        ];
    }
}
