<?php

namespace App\Http\Requests;

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

class RegisterRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            // User fields
            'user_id' => [
                'required',
                'string',
                'max:255',
                'regex:/^[a-zA-Z0-9_-]+$/',
                'unique:users,user_id',
            ],
            'name' => 'required|string|max:255',
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                'unique:users,email',
            ],
            'phone' => [
                'nullable',
                'string',
                'max:20',
                'regex:/^[0-9-]+$/',
            ],
            'password' => 'required|string|min:8|confirmed',
            'position' => 'nullable|string|max:100',

            // Tenant fields
            'company_name' => 'required|string|max:255',
            'business_num' => [
                'required',
                'string',
                'regex:/^\d{3}-\d{2}-\d{5}$/',
                Rule::unique('tenants', 'business_num')->where(function ($query) {
                    return $query->where('tenant_st_code', 'active');
                }),
            ],
            'company_scale' => 'nullable|string|max:50',
            'industry' => 'nullable|string|max:100',
        ];
    }

    /**
     * Get custom attributes for validator errors.
     */
    public function attributes(): array
    {
        return [
            'user_id' => __('validation.attributes.user_id'),
            'name' => __('validation.attributes.name'),
            'email' => __('validation.attributes.email'),
            'phone' => __('validation.attributes.phone'),
            'password' => __('validation.attributes.password'),
            'position' => __('validation.attributes.position'),
            'company_name' => __('validation.attributes.company_name'),
            'business_num' => __('validation.attributes.business_num'),
            'company_scale' => __('validation.attributes.company_scale'),
            'industry' => __('validation.attributes.industry'),
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'business_num.regex' => __('error.business_num_format'),
            'business_num.unique' => __('error.business_num_duplicate_active'),
            'user_id.regex' => __('error.user_id_format'),
            'phone.regex' => __('error.phone_format'),
        ];
    }
}
