<?php

namespace App\Http\Requests\Common;

use App\Http\Requests\Traits\HasPagination;
use Illuminate\Foundation\Http\FormRequest;

class PaginateRequest extends FormRequest
{
    use HasPagination;

    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'page' => 'nullable|integer|min:1',
            'size' => 'nullable|integer|min:1',
            'q' => 'nullable|string|max:100',
            'sort' => 'nullable|string|in:id,code,name,created_at',
            'order' => 'nullable|string|in:asc,desc',
        ];
    }

    public function validatedOrDefaults(): array
    {
        $v = $this->validated();
        $v['page'] = $v['page'] ?? 1;
        $v['size'] = $v['size'] ?? config('pagination.default_size', 20);
        $v['order'] = $v['order'] ?? 'desc';

        return $v;
    }
}
