<?php

declare(strict_types=1);

namespace App\Http\Requests\Report;

use App\Http\Requests\Concerns\NormalizesBooleanQueryParams;
use App\Models\Report;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class IndexHealthReportRequest extends FormRequest
{
    use NormalizesBooleanQueryParams;

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

    public function rules(): array
    {
        return [
            'project_id' => ['nullable', 'integer', 'exists:projects,id'],
            'project' => ['nullable', 'string', 'max:120'],
            'product_id' => ['nullable', 'integer', 'exists:products,id'],
            'product' => ['nullable', 'string', 'max:120'],
            'producer_id' => ['nullable', 'integer', 'exists:producers,id'],
            'producer' => ['nullable', 'string', 'max:120'],
            'disease_name' => ['nullable', 'string', 'max:255'],
            'report_type' => ['nullable', 'string', Rule::in(array_keys(Report::healthReportTypes()))],
            'workflow_status' => ['nullable', 'string', Rule::in(array_keys(Report::workflowOptions()))],
            'status' => ['nullable', 'in:draft,published,archived'],
            'from' => ['nullable', 'date'],
            'to' => ['nullable', 'date', 'after_or_equal:from'],
            'per_page' => ['nullable', 'integer', 'min:1', 'max:50'],
            'noPaginate' => ['nullable', 'boolean'],
        ];
    }

    protected function prepareForValidation(): void
    {
        $this->normalizeBooleanQueryParams();
    }
}
