<?php

namespace App\Http\Requests;

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

class ExportSegmentContractsRequest 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(SegmentContractsExport::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'],
            'client' => ['nullable', 'string', 'max:64'],
            'page' => ['nullable', 'integer', 'min:1'],
            'per_page' => ['nullable', 'integer', 'min:1', 'max:200'],
        ];
    }

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