<?php

namespace App\Http\Requests\Admin\Concerns;

trait AdminAccountFieldRules
{
    /** @return array<int, mixed> */
    protected function adminLoginAccountRules(bool $required = true): array
    {
        $rules = ['string', 'min:2', 'max:64', 'regex:/^[a-zA-Z0-9._-]+$/u'];

        return $required ? array_merge(['required'], $rules) : array_merge(['sometimes'], $rules);
    }

    /** @return array<int, mixed> */
    protected function adminLoginPasswordRules(bool $required = true, int $min = 6, int $max = 256): array
    {
        $rules = ['string', 'min:'.$min, 'max:'.$max];

        if ($required) {
            return array_merge(['required'], $rules);
        }

        return array_merge(['sometimes', 'nullable'], $rules);
    }

    /** @return array<int, mixed> */
    protected function adminOperatorUsernameRules(bool $required = true): array
    {
        $rules = ['string', 'min:2', 'max:64', 'regex:/^[a-zA-Z0-9._-]+$/u'];

        return $required ? array_merge(['required'], $rules) : array_merge(['sometimes'], $rules);
    }

    /** @return array<int, mixed> */
    protected function nativePlayerUsernameRules(bool $required = false): array
    {
        $rules = ['string', 'min:2', 'max:64', 'regex:/^[a-zA-Z0-9._-]+$/u'];

        return $required ? array_merge(['required'], $rules) : array_merge(['nullable'], $rules);
    }

    /** @return array<int, mixed> */
    protected function nativePlayerPasswordRules(bool $required = false): array
    {
        $rules = ['string', 'min:6', 'max:128'];

        return $required ? array_merge(['required'], $rules) : array_merge(['nullable'], $rules);
    }

    protected function normalizeAdminAccountFields(): void
    {
        if ($this->has('account')) {
            $this->merge(['account' => strtolower(trim((string) $this->input('account')))]);
        }

        if ($this->has('username')) {
            $this->merge(['username' => strtolower(trim((string) $this->input('username')))]);
        }
    }
}
