<?php

namespace App\Http\Requests;

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

class BankRequest extends FormRequest
{
    public function rules(): array
    {
        $uniqueCode = Rule::unique('banks', 'code')
            ->ignore($this->route('id'))
            ->whereNull('deleted_at');

        $rules = [
            'name'   => ['sometimes', 'string', 'max:255'],
            'code'   => ['sometimes', 'string', 'max:10', $uniqueCode],
            'active' => ['sometimes', 'boolean'],
        ];

        if ($this->isMethod('POST')) {
            $rules['name'] = ['required', 'string', 'max:255'];
            $rules['code'] = ['required', 'string', 'max:10', $uniqueCode];
        }

        return $rules;
    }

    /**
    * Add custom messages when needed
    * public function messages(): array
    * {
    *   return [
    *        'field.required' => __('message.algo'),
    *    ];
    * }
    */
}
