<?php

namespace App\Http\Requests\Api;

use App\Enums\PermissionSlug;
use App\Models\Floor;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreFloorRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->hasPermission(PermissionSlug::UnitsCreate) === true;
    }

    /**
     * @return array<string, ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:50', Rule::unique(Floor::class, 'name')],
            'sort_order' => ['sometimes', 'required', 'integer', 'min:0', 'max:1000'],
        ];
    }

    /**
     * @return array{name: string, sort_order: int}
     */
    public function payload(): array
    {
        /** @var array{name: string, sort_order?: mixed} $data */
        $data = $this->safe()->only(['name', 'sort_order']);

        return [
            'name' => $data['name'],
            'sort_order' => isset($data['sort_order']) ? (int) $data['sort_order'] : 0,
        ];
    }
}
