<?php

namespace App\Http\Requests\V1\Bill;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    public function rules(): array
    {
        $tenantId = app('tenant_id') ?? 0;

        return [
            'bill_number' => [
                'nullable',
                'string',
                'max:50',
                Rule::unique('bills', 'bill_number')->where(function ($query) use ($tenantId) {
                    return $query->where('tenant_id', $tenantId);
                }),
            ],
            'bill_type' => ['required', 'string', 'in:received,issued'],
            'client_id' => ['nullable', 'integer', 'exists:clients,id'],
            'client_name' => ['nullable', 'string', 'max:100'],
            'amount' => ['required', 'numeric', 'min:0'],
            'issue_date' => ['required', 'date'],
            'maturity_date' => ['required', '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_number.unique' => __('error.bill.duplicate_number'),
            'bill_type.required' => __('validation.required', ['attribute' => __('validation.attributes.bill_type')]),
            'bill_type.in' => __('validation.in', ['attribute' => __('validation.attributes.bill_type')]),
            'amount.required' => __('validation.required', ['attribute' => __('validation.attributes.amount')]),
            'amount.min' => __('validation.min.numeric', ['attribute' => __('validation.attributes.amount'), 'min' => 0]),
            'issue_date.required' => __('validation.required', ['attribute' => __('validation.attributes.issue_date')]),
            'maturity_date.required' => __('validation.required', ['attribute' => __('validation.attributes.maturity_date')]),
            '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'),
        ];
    }
}
