<?php

namespace App\Http\Requests\V1\BadDebt;

use App\Models\BadDebts\BadDebt;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreBadDebtRequest 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 [
            'client_id' => ['required', 'integer', 'exists:clients,id'],
            'debt_amount' => ['required', 'numeric', 'min:0'],
            'status' => ['nullable', 'string', Rule::in(array_keys(BadDebt::STATUSES))],
            'overdue_days' => ['nullable', 'integer', 'min:0'],
            'assigned_user_id' => ['nullable', 'integer', 'exists:users,id'],
            'occurred_at' => ['nullable', 'date'],
            'closed_at' => ['nullable', 'date', 'after_or_equal:occurred_at'],
            'is_active' => ['nullable', 'boolean'],
            'options' => ['nullable', 'array'],
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'client_id.required' => __('validation.required', ['attribute' => '거래처']),
            'client_id.exists' => __('validation.exists', ['attribute' => '거래처']),
            'debt_amount.required' => __('validation.required', ['attribute' => '채권금액']),
            'debt_amount.numeric' => __('validation.numeric', ['attribute' => '채권금액']),
            'status.in' => __('validation.in', ['attribute' => '상태']),
            'closed_at.after_or_equal' => __('validation.after_or_equal', ['attribute' => '종료일', 'date' => '발생일']),
        ];
    }
}
