<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

class ServerStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->user()->can('create', \App\Models\Server::class);
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'module' => 'nullable|string|max:255',
            // PRC-817 — bağlantı alanları YALNIZ provisioning|both için zorunlu; physical
            // asset'te boş olabilir (server_type gönderilmezse default provisioning → zorunlu,
            // mevcut form davranışı korunur).
            'hostname' => 'required_unless:server_type,physical|nullable|string|max:255',
            'ip_address' => 'required_unless:server_type,physical|nullable|ip',
            'username' => 'required_unless:server_type,physical|nullable|string|max:255',
            'password' => 'required_unless:server_type,physical|nullable|string|max:255',
            'port' => 'required|integer|min:1|max:65535',
            'status' => 'required|in:active,inactive',
            'maximum_hosts' => 'nullable|integer|min:0',
            'stock' => 'nullable|integer|min:0',
            'module_config' => 'nullable|array',
            // PRC-817 — fiziksel asset + lokasyon
            'server_type' => 'nullable|in:provisioning,physical,both',
            'serial_number' => 'nullable|string|max:255',
            'asset_tag' => 'nullable|string|max:255',
            'manufacturer' => 'nullable|string|max:255',
            'model_name' => 'nullable|string|max:255',
            'specs' => 'nullable|array',
            'purchase_date' => 'nullable|date',
            'warranty_expiry' => 'nullable|date',
            'asset_status' => 'nullable|in:active,stock,decommissioned,terminated',
            'datacenter_id' => 'nullable|exists:datacenters,id',
            'rack_id' => 'nullable|exists:racks,id',
            'u_position' => 'nullable|integer|min:1|max:100',
            'u_size' => 'nullable|integer|min:1|max:20',
            'client_product_id' => 'nullable|exists:client_products,id',
        ];
    }

    /**
     * Get custom error messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'name.required' => __('admin/servers.validation.name.required'),
            'hostname.required' => __('admin/servers.validation.hostname.required'),
            'ip_address.required' => __('admin/servers.validation.ip_address.required'),
            'ip_address.ip' => __('admin/servers.validation.ip_address.ip'),
            'username.required' => __('admin/servers.validation.username.required'),
            'password.required' => __('admin/servers.validation.password.required'),
            'port.required' => __('admin/servers.validation.port.required'),
            'port.integer' => __('admin/servers.validation.port.integer'),
            'port.min' => __('admin/servers.validation.port.min'),
            'port.max' => __('admin/servers.validation.port.max'),
            'status.required' => __('admin/servers.validation.status.required'),
            'status.in' => __('admin/servers.validation.status.in'),
        ];
    }
}
