<?php

namespace App\Http\Requests\Nas;

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

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

    public function rules(): array
    {
        return array_merge($this->commonRules(), match ($this->route()->defaults['nas_resource'] ?? null) {
            'mikrotik' => [
                'connection_type' => ['required', Rule::in(['api', 'radius'])],
                'host' => ['required', 'string', 'max:255'],
                'api_port' => ['nullable', 'integer', 'between:1,65535'],
                'api_username' => ['nullable', 'string', 'max:255'],
                'api_password' => ['nullable', 'string', 'max:1000'],
                'radius_auth_port' => ['nullable', 'integer', 'between:1,65535'],
                'radius_accounting_port' => ['nullable', 'integer', 'between:1,65535'],
                'radius_secret' => ['nullable', 'string', 'max:1000'],
                'timeout' => ['nullable', 'integer', 'between:1,300'],
            ],
            'package-profile' => [
                'service_type' => ['required', Rule::in(['ppp', 'hotspot', 'radius'])],
                'external_profile_name' => ['nullable', 'string', 'max:255'],
                'download_kbps' => ['nullable', 'integer', 'min:0'],
                'upload_kbps' => ['nullable', 'integer', 'min:0'],
                'local_address' => ['nullable', 'string', 'max:255'],
                'remote_address_pool' => ['nullable', 'string', 'max:255'],
                'session_timeout' => ['nullable', 'integer', 'min:0'],
                'idle_timeout' => ['nullable', 'integer', 'min:0'],
                'shared_users' => ['nullable', 'integer', 'min:1'],
                'price' => ['nullable', 'numeric', 'min:0'],
            ],
            'olt' => [
                'vendor' => ['nullable', 'string', 'max:255'],
                'model' => ['nullable', 'string', 'max:255'],
                'host' => ['required', 'string', 'max:255'],
                'snmp_port' => ['nullable', 'integer', 'between:1,65535'],
                'snmp_version' => ['required', Rule::in(['v1', 'v2c', 'v3'])],
                'community' => ['nullable', 'string', 'max:1000'],
                'security_level' => ['nullable', Rule::in(['noAuthNoPriv', 'authNoPriv', 'authPriv'])],
                'security_name' => ['nullable', 'string', 'max:255'],
                'auth_protocol' => ['nullable', Rule::in(['MD5', 'SHA', 'SHA224', 'SHA256', 'SHA384', 'SHA512'])],
                'auth_password' => ['nullable', 'string', 'max:1000'],
                'privacy_protocol' => ['nullable', Rule::in(['DES', 'AES', 'AES192', 'AES256'])],
                'privacy_password' => ['nullable', 'string', 'max:1000'],
                'timeout' => ['nullable', 'integer', 'between:1,300'],
            ],
            'webfig' => [
                'device_type' => ['nullable', 'string', 'max:100'],
                'url' => ['required', 'url:http,https', 'max:2000'],
                'username' => ['nullable', 'string', 'max:255'],
                'password' => ['nullable', 'string', 'max:1000'],
            ],
            default => [],
        });
    }

    protected function commonRules(): array
    {
        return [
            'tenant_id' => ['nullable', 'integer', 'exists:tenants,id'],
            'name' => ['required', 'string', 'max:255'],
            'status' => ['nullable', Rule::in(['active', 'inactive'])],
            'notes' => ['nullable', 'string'],
        ];
    }
}
