<?php

namespace App\Http\Requests;

use App\Enums\LanguageEnum;
use App\Enums\UserTypeEnum;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UserRequest extends FormRequest
{
    public function rules(): array
    {
        $rules = [
            'avatar' => 'sometimes|string|nullable',
            'name'   => 'sometimes|string|nullable',

            'email' => [
                'sometimes',
                'email',
                Rule::unique('users', 'email')->ignore($this->route('id')),
            ],

            'password' => 'sometimes|string|nullable',
            'type'     => ['sometimes', Rule::enum(UserTypeEnum::class)],
            'language' => ['sometimes', Rule::enum(LanguageEnum::class)],
            'phone'    => 'sometimes|string|nullable',
        ];

        if ($this->isMethod('post')) {
            $rules['name']     = 'required|string|max:255';
            $rules['email']    = 'required|email|unique:users,email';
            $rules['password'] = 'required|string|min:6';

            if (! $this->has('language')) {
                $this->merge(['language' => LanguageEnum::PORTUGUESE->value]);
            }
        }

        return $rules;
    }
}
