<?php

namespace App\Http\Requests;

use App\Models\Impresora;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateImpresoraRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('administracion.areas') ?? false;
    }

    public function rules(): array
    {
        /** @var Impresora $impresora */
        $impresora = $this->route('impresora');

        return [
            'area_id' => ['sometimes', 'integer', 'exists:areas,id'],
            'codigo' => ['sometimes', 'string', 'max:64', 'alpha_dash', Rule::unique('impresoras', 'codigo')->ignore($impresora->id)],
            'nombre' => ['sometimes', 'string', 'max:255'],
            'nombre_sistema' => ['sometimes', 'string', 'max:255'],
            'ip' => ['nullable', 'string', 'max:45'],
            'puerto' => ['nullable', 'integer', 'min:1', 'max:65535'],
            'tipo_ticket' => ['sometimes', 'string', Rule::in([Impresora::TIPO_VENTA, Impresora::TIPO_COCINA])],
            'activa' => ['sometimes', 'boolean'],
            'es_por_defecto' => ['sometimes', 'boolean'],
            'ancho_papel' => ['nullable', 'integer', 'min:32', 'max:120'],
        ];
    }
}
