<?php

declare(strict_types=1);

namespace App\Http\Requests\Invoice;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            'payment_method' => ['required', 'in:wallet,gateway'],
            'gateway' => ['nullable', 'string', 'max:50'],
            'wallet_amount' => ['nullable', 'integer', 'min:0'],
            'callback_url' => ['nullable', 'url'],
        ];
    }

    public function messages(): array
    {
        return [
            'payment_method.required' => 'روش پرداخت الزامی است.',
            'payment_method.in' => 'روش پرداخت باید wallet یا gateway باشد.',
        ];
    }

    protected function prepareForValidation(): void
    {
        if (! $this->filled('payment_method')) {
            $this->merge(['payment_method' => 'gateway']);
        }

        if (! $this->filled('gateway') && $this->input('payment_method') === 'gateway') {
            $this->merge(['gateway' => config('payment.default', 'zibal')]);
        }
    }
}
