<?php

namespace App\Http\Requests\Admin;

use App\Http\Requests\Admin\Concerns\AdminAccountFieldRules;
use App\Http\Requests\Admin\Concerns\AgentProfileFieldRules;
use App\Http\Requests\ApiFormRequest;
use Illuminate\Validation\Rule;

final class AgentNodeStoreRequest extends ApiFormRequest
{
    use AdminAccountFieldRules;
    use AgentProfileFieldRules;
    public function authorize(): bool
    {
        return true;
    }

    protected function prepareForValidation(): void
    {
        $this->normalizeAdminAccountFields();
        $this->prepareAgentProfileFieldsForValidation();
    }

    /** @return array<string, mixed> */
    public function rules(): array
    {
        return [
            'parent_id' => ['required', 'integer', 'exists:agent_nodes,id'],
            'code' => ['sometimes', 'nullable', 'string', 'max:64', 'regex:/^[a-zA-Z0-9_-]+$/'],
            'name' => ['required', 'string', 'max:128'],
            'username' => ['sometimes', 'nullable', ...$this->adminOperatorUsernameRules(required: false), Rule::unique('admin_users', 'username')],
            'email' => ['nullable', 'email', 'max:255', Rule::unique('admin_users', 'email')],
            'password' => ['required', 'string', 'min:6', 'max:128'],
            'status' => ['sometimes', 'integer', 'in:0,1'],
            ...$this->agentProfileFieldRules(),
        ];
    }
}
