<?php

namespace App\Http\Requests;

use App\Enums\AccountTypeEnum;
use App\Enums\BankAccountTypeEnum;
use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        $pixAccountType   = AccountTypeEnum::PIX->value;
        $bankAccountType  = AccountTypeEnum::BANK_ACCOUNT->value;
        $accountTypes     = implode(',', [$pixAccountType, $bankAccountType]);
        $bankAccountTypes = implode(',', [BankAccountTypeEnum::CHECKING->value, BankAccountTypeEnum::SAVINGS->value]);

        if ($this->account_type == AccountTypeEnum::PIX->value) {

            $rules =  [
                'account_type'      => "sometimes|in:{$accountTypes}",
                'pix_key'           => "required|string|max:255|required_if:account_type,{$pixAccountType}",
                'bank_account_type' => "nullable|required_if:account_type,{$bankAccountType}|in:{$bankAccountTypes}",
                'agency'            => "nullable|string|max:255|required_if:account_type,{$bankAccountType}",
                'account'           => "nullable|string|max:255|required_if:account_type,{$bankAccountType}",
                'digit'             => "nullable|string|max:255|required_if:account_type,{$bankAccountType}",
            ];
        } else {
            $rules =  [
                'account_type'      => "sometimes|in:{$accountTypes}",
                'pix_key'           => "nullable|string|max:255|required_if:account_type,{$pixAccountType}",
                'bank_account_type' => "sometimes|required_if:account_type,{$bankAccountType}|in:{$bankAccountTypes}",
                'agency'            => "sometimes|string|max:255|required_if:account_type,{$bankAccountType}",
                'account'           => "sometimes|string|max:255|required_if:account_type,{$bankAccountType}",
                'digit'             => "sometimes|string|max:255|required_if:account_type,{$bankAccountType}",
            ];
        }

        if ($this->isMethod('post')) {
            $rules['provider_id'] = 'required|exists:providers,id';
        };


        return $rules;
    }
}
