<?php

namespace App\Http\Requests\V1\Bill;

use Illuminate\Foundation\Http\FormRequest;

class UpdateBillRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'bill_number' => ['nullable', 'string', 'max:50'],
            'bill_type' => ['nullable', 'string', 'in:received,issued'],
            'client_id' => ['nullable', 'integer', 'exists:clients,id'],
            'client_name' => ['nullable', 'string', 'max:100'],
            'amount' => ['nullable', 'numeric', 'min:0'],
            'issue_date' => ['nullable', 'date'],
            'maturity_date' => ['nullable', 'date', 'after_or_equal:issue_date'],
            'status' => ['nullable', 'string', 'in:stored,maturityAlert,maturityResult,paymentComplete,dishonored,collectionRequest,collectionComplete,suing'],
            'reason' => ['nullable', 'string', 'max:255'],
            'installment_count' => ['nullable', 'integer', 'min:0'],
            'note' => ['nullable', 'string', 'max:1000'],
            'is_electronic' => ['nullable', 'boolean'],
            'bank_account_id' => ['nullable', 'integer', 'exists:bank_accounts,id'],
            'installments' => ['nullable', 'array'],
            'installments.*.date' => ['required_with:installments', 'date'],
            'installments.*.amount' => ['required_with:installments', 'numeric', 'min:0'],
            'installments.*.note' => ['nullable', 'string', 'max:255'],
        ];
    }

    public function messages(): array
    {
        return [
            'bill_type.in' => __('validation.in', ['attribute' => __('validation.attributes.bill_type')]),
            'amount.min' => __('validation.min.numeric', ['attribute' => __('validation.attributes.amount'), 'min' => 0]),
            'maturity_date.after_or_equal' => __('validation.after_or_equal', ['attribute' => __('validation.attributes.maturity_date'), 'date' => __('validation.attributes.issue_date')]),
        ];
    }

    public function attributes(): array
    {
        return [
            'bill_number' => __('validation.attributes.bill_number'),
            'bill_type' => __('validation.attributes.bill_type'),
            'client_id' => __('validation.attributes.client_id'),
            'client_name' => __('validation.attributes.client_name'),
            'amount' => __('validation.attributes.amount'),
            'issue_date' => __('validation.attributes.issue_date'),
            'maturity_date' => __('validation.attributes.maturity_date'),
            'status' => __('validation.attributes.status'),
            'reason' => __('validation.attributes.reason'),
            'note' => __('validation.attributes.note'),
            'is_electronic' => __('validation.attributes.is_electronic'),
            'bank_account_id' => __('validation.attributes.bank_account_id'),
        ];
    }
}
