<?php

namespace App\Http\Requests;

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

class RegisterClientRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'code' => 'required|string|max:6',

            'email' => [
                Rule::requiredIf(fn () => ! config('services.sms.enabled') || ! $this->filled('phone')),
                'string', 'email', 'max:255',
            ],

            'name'           => 'required|string|max:255',
            'phone'          => 'required|string|max:20',
            'document'       => 'required|string|max:20',
            'zip_code'       => 'required|string|max:20',
            'number'         => 'required|string|max:20',
            'address'        => 'required|string|max:255',
            'district'       => 'required|string|max:255',
            'city'           => 'required|string|max:255',
            'state'          => 'required|string|max:10',
            'has_complement' => 'required|boolean',
            'complement'     => 'required|string|max:255',
            'nickname'       => 'sometimes|nullable|string|max:255',
            'instructions'   => 'sometimes|nullable|string|max:500',
            'address_type'   => 'sometimes|nullable|string|in:home,commercial,other',
            'latitude'       => 'sometimes|nullable|numeric',
            'longitude'      => 'sometimes|nullable|numeric',
            'avatar'         => 'sometimes|nullable|file|image|mimes:jpg,jpeg,png,webp|max:5120',
        ];
    }

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