<?php

declare(strict_types=1);

namespace App\Http\Requests\Install;

use Illuminate\Foundation\Http\FormRequest;

final class Stage2DbRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true; // 미들웨어 'installer.only' 에서 검증됨
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'host'         => ['required', 'string', 'max:253'],
            'port'         => ['required', 'integer', 'between:1,65535'],
            'database'     => ['required', 'string', 'max:64', 'regex:/^[a-zA-Z0-9_]+$/'],
            'username'     => ['required', 'string', 'max:128'],
            'password'     => ['nullable', 'string', 'max:512'],
            // 그누보드 패리티: 한 DB 에 여러 인스턴스 (선택, 빈 값 기본)
            'table_prefix' => ['nullable', 'string', 'max:16', 'regex:/^[a-zA-Z0-9_]*$/'],
        ];
    }

    /**
     * @return array{host:string, port:int, database:string, username:string, password:string, table_prefix:string}
     */
    public function creds(): array
    {
        $v = $this->validated();

        return [
            'host'         => (string) $v['host'],
            'port'         => (int) $v['port'],
            'database'     => (string) $v['database'],
            'username'     => (string) $v['username'],
            'password'     => (string) ($v['password'] ?? ''),
            'table_prefix' => (string) ($v['table_prefix'] ?? ''),
        ];
    }
}
