<?php

declare(strict_types=1);

namespace App\Http\Requests\Install;

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

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

    protected function prepareForValidation(): void
    {
        if ($this->has('admin_email') && trim((string) $this->input('admin_email')) === '') {
            $this->merge(['admin_email' => null]);
        }
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'system_url' => ['required', 'string', 'url', 'max:512'],
            'timezone' => ['required', 'string', Rule::in(timezone_identifiers_list())],
            'admin_name' => ['required', 'string', 'max:80'],
            'login_account' => ['required', 'string', 'max:64', 'regex:/^[a-zA-Z0-9_.-]+$/'],
            'admin_email' => ['nullable', 'string', 'email', 'max:255'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'reinstall_reset' => ['sometimes', 'boolean'],
        ];
    }

    /**
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'system_url' => '系统访问地址',
            'timezone' => '时区',
            'admin_name' => '管理员姓名',
            'login_account' => '登录账号',
            'admin_email' => '邮箱',
            'password' => '密码',
        ];
    }
}
