<?php

declare(strict_types=1);

namespace App\Http\Requests\PropertyRequest;

use App\Enums\RequestPropertyType;
use App\Enums\UrgencyLevel;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Enum;

/**
 * Filtres § 5.3 — consultation des demandes disponibles.
 */
class IndexPropertyRequestRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->hasApprovedAccount() ?? false;
    }

    /**
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        return [
            'property_type' => ['nullable', new Enum(RequestPropertyType::class)],
            'city_id' => ['nullable', 'integer', 'exists:cities,id'],
            'zone_id' => [
                'nullable',
                'integer',
                Rule::exists('zones', 'id')->where(function ($query): void {
                    if ($this->filled('city_id')) {
                        $query->where('city_id', (int) $this->input('city_id'));
                    }
                }),
            ],
            'budget_max' => ['nullable', 'numeric', 'min:0'],
            'urgency_level' => ['nullable', new Enum(UrgencyLevel::class)],
            'page' => ['nullable', 'integer', 'min:1'],
            'per_page' => ['nullable', 'integer', 'min:1', 'max:50'],
        ];
    }
}
