<?php

declare(strict_types=1);

namespace App\Http\Requests\Report;

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

class ReportRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $type = (int) $this->input('type', Report::TYPE_HEALTH);

        $rules = [
            'type' => ['required', 'integer', 'in:1,2,3'],
            'project_id' => ['nullable', 'integer', 'exists:projects,id'],
            'producer_id' => ['nullable', 'integer', 'exists:producers,id'],
            'product_id' => ['nullable', 'integer', 'exists:products,id'],
            'title' => ['required', 'string', 'max:255'],
            'period' => ['nullable', 'string', 'max:120'],
            'date' => ['required', 'date'],
            'report_at' => ['nullable', 'date'],
            'desc' => ['nullable', 'string'],
            'written_report' => ['nullable', 'string'],
            'status' => ['nullable', 'in:draft,published,archived'],
            'workflow_status' => ['nullable', 'in:pending,approved,rejected,needs_edit'],
            'attachments' => ['nullable', 'array'],
            'documents' => ['nullable', 'array'],
            'items' => ['nullable', 'array'],
            'items.*.product_id' => ['nullable', 'integer', 'exists:products,id'],
            'items.*.group_name' => ['nullable', 'string', 'max:120'],
            'items.*.value' => ['nullable', 'numeric'],
            'items.*.min_value' => ['nullable', 'numeric'],
            'items.*.max_value' => ['nullable', 'numeric'],
            'items.*.avg_value' => ['nullable', 'numeric'],
            'items.*.unit' => ['nullable', 'string', 'max:50'],
            'items.*.action_note' => ['nullable', 'string', 'max:2000'],
        ];

        if ($type === Report::TYPE_GROWTH) {
            $rules = array_merge($rules, [
                'unit' => ['nullable', 'string', Rule::in(array_keys(Report::unitOptions()))],
                'report_type' => ['nullable', 'string', Rule::in(array_keys(Report::growthReportTypes()))],
                'create_invoice' => ['nullable', 'boolean'],
                'total_cost' => ['nullable', 'numeric', 'min:0'],
                'invoice_image' => ['nullable', 'string'],
                'invoice_items' => ['nullable', 'array'],
                'invoice_items.*.title' => ['required_with:invoice_items', 'string', 'max:255'],
                'invoice_items.*.amount' => ['nullable', 'numeric', 'min:0'],
                'metrics' => ['nullable', 'array'],
                'admin_reason' => ['nullable', 'string'],
            ]);
        }

        if ($type === Report::TYPE_HEALTH) {
            $rules = array_merge($rules, [
                'report_type' => ['required', Rule::in(array_keys(Report::healthReportTypes()))],
                'disease_name' => ['nullable', 'string', 'max:255'],
                'diagnosis' => ['nullable', 'string'],
                'expert_opinion' => ['nullable', 'string'],
                'next_visit_at' => ['nullable', 'date'],
                'total_cost' => ['nullable', 'integer', 'min:0'],
                'invoice_image' => ['nullable', 'string'],
                'product_weight' => ['nullable', 'numeric', 'min:0'],
                'consumables' => ['nullable', 'array'],
                'consumables.*.name' => ['required_with:consumables', 'string', 'max:255'],
                'consumables.*.unit' => ['nullable', 'string', 'max:50'],
                'consumables.*.quantity' => ['nullable', 'numeric', 'min:0'],
                'invoice_items' => ['nullable', 'array'],
                'invoice_items.*.title' => ['required_with:invoice_items', 'string', 'max:255'],
                'invoice_items.*.amount' => ['nullable', 'integer', 'min:0'],
            ]);
        }

        return $rules;
    }
}
