<?php

namespace App\Http\Requests\UserInvitation;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            'email' => ['required', 'email', 'max:255'],
            'role' => ['nullable', 'string', 'in:admin,manager,user'],
            'role_id' => ['nullable', 'integer', 'exists:roles,id'],
            'message' => ['nullable', 'string', 'max:1000'],
            'expires_days' => ['nullable', 'integer', 'min:1', 'max:30'],
        ];
    }

    /**
     * 추가 유효성 검사: role과 role_id 중 하나만 사용
     */
    public function withValidator($validator): void
    {
        $validator->after(function ($validator) {
            if ($this->filled('role') && $this->filled('role_id')) {
                $validator->errors()->add('role', __('validation.custom.role_conflict'));
            }
        });
    }

    public function messages(): array
    {
        return [
            'email.required' => __('validation.required', ['attribute' => '이메일']),
            'email.email' => __('validation.email', ['attribute' => '이메일']),
            'role_id.exists' => __('validation.exists', ['attribute' => '역할']),
        ];
    }
}
