<?php

declare(strict_types=1);

namespace App\Http\Requests\Report;

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

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

    public function rules(): array
    {
        $isDraft = $this->boolean('is_draft') || $this->input('status') === 'draft';
        $isRemove = $this->input('report_type') === Report::HEALTH_TYPE_REMOVE_PRODUCT;

        return [
            'is_draft' => ['nullable', 'boolean'],
            'project_id' => [$isDraft ? 'nullable' : 'required', 'integer', 'exists:projects,id'],
            'producer_id' => ['nullable', 'integer', 'exists:producers,id'],
            'product_id' => ['nullable', 'integer', 'exists:products,id'],
            'user_id' => ['nullable', 'integer', 'exists:users,id'],
            'title' => ['nullable', 'string', 'max:255'],
            'date' => ['required', 'date'],
            'report_at' => ['nullable', 'date'],
            'report_type' => [$isDraft ? 'nullable' : 'required', 'string', Rule::in(array_keys(Report::healthReportTypes()))],
            'disease_name' => ['nullable', 'string', 'max:255'],
            'diagnosis' => ['nullable', 'string'],
            'expert_opinion' => ['nullable', 'string'],
            'next_visit_at' => ['nullable', 'date'],
            'desc' => ['nullable', 'string'],
            'written_report' => ['nullable', 'string'],
            'status' => ['nullable', 'in:draft,published,archived'],
            'attachments' => ['nullable', 'array'],
            'attachments.*' => ['string', 'max:500'],
            'documents' => ['nullable', 'array'],
            'documents.*' => ['string', 'max:500'],
            'total_cost' => ['nullable', 'numeric', 'min:0'],
            'invoice_image' => ['nullable', 'string', 'max:500'],
            'product_weight' => [$isRemove && ! $isDraft ? 'required' : 'nullable', 'numeric', 'min:0'],
            'items' => [$isDraft ? 'nullable' : 'required', 'array', 'min:1'],
            'items.*.product_id' => ['nullable', 'integer', 'exists:products,id'],
            'items.*.unit' => ['nullable', 'string', 'max:50'],
            'items.*.value' => ['nullable', 'numeric'],
            'consumables' => ['nullable', 'array'],
            'consumables.*.name' => ['required_with:consumables', 'string', 'max:255'],
            'consumables.*.unit' => ['nullable', 'string', Rule::in(array_keys(Report::consumableUnitOptions()))],
            'consumables.*.quantity' => ['nullable', 'numeric', 'min:0'],
            'consumables.*.unit_price' => ['nullable', 'numeric', 'min:0'],
            'invoice_items' => ['nullable', 'array'],
            'invoice_items.*.title' => ['required_with:invoice_items', 'string', 'max:255'],
            'invoice_items.*.amount' => ['nullable', 'numeric', 'min:0'],
        ];
    }

    public function messages(): array
    {
        return [
            'project_id.required' => 'انتخاب پروژه الزامی است.',
            'report_type.required' => 'نوع گزارش الزامی است.',
            'items.required' => 'انتخاب حداقل یک محصول الزامی است.',
            'date.required' => 'تاریخ گزارش الزامی است.',
            'product_weight.required' => 'وزن محصول برای گزارش حذف الزامی است.',
        ];
    }
}
