<?php

namespace App\Http\Requests\Admin;

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

class CiudadRequest extends FormRequest
{
    /**
     * La autorización ya está protegida por middlewares administrativos.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Valida el catálogo oficial con reglas estrictas para evitar duplicados y
     * coordenadas inválidas que luego rompan filtros o mapas.
     */
    public function rules(): array
    {
        $cityId = $this->route('ciudade') ?? $this->route('ciudad');

        return [
            'nombre' => [
                'required',
                'string',
                'min:2',
                'max:150',
                Rule::unique('ciudades', 'nombre')
                    ->where(fn ($query) => $query->where('departamento_id', $this->input('departamento_id')))
                    ->ignore($cityId),
            ],
            'departamento_id' => ['required', 'integer', 'exists:departamentos,id'],
            'latitud' => ['required', 'numeric', 'between:-90,90'],
            'longitud' => ['required', 'numeric', 'between:-180,180'],
        ];
    }
}
