<?php

namespace App\Http\Requests;

use App\Exports\ClientContractsExport;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ExportClientContractsRequest extends FormRequest
{
    public const SCOPE_CURRENT = 'current';

    public const SCOPE_ALL = 'all';

    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    public function rules(): array
    {
        $columnRule = Rule::in(ClientContractsExport::allowedColumns());

        return [
            'scope' => ['required', Rule::in([self::SCOPE_CURRENT, self::SCOPE_ALL])],
            'columns' => ['required', 'array', 'min:1'],
            'columns.*' => ['string', $columnRule],
            'search' => ['nullable', 'string', 'max:255'],
            'from' => ['nullable', 'date'],
            'to' => ['nullable', 'date'],
            'segments' => ['nullable', 'string'],
            'page' => ['nullable', 'integer', 'min:1'],
            'per_page' => ['nullable', 'integer', 'min:1', 'max:200'],
        ];
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'per_page' => $this->input('per_page') ?? $this->input('perPage'),
        ]);
    }
}
