<?php

declare(strict_types=1);

namespace App\Http\Requests\Report;

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

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

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

        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'],
            'title' => ['nullable', 'string', 'max:255'],
            'period' => ['nullable', 'string', 'max:120'],
            'date' => ['required', 'date'],
            'report_at' => ['nullable', 'date'],
            'desc' => ['nullable', 'string'],
            'written_report' => ['nullable', 'string'],
            'unit' => ['nullable', 'string', Rule::in(array_keys(Report::unitOptions()))],
            'report_type' => ['nullable', 'string', Rule::in(array_keys(Report::growthReportTypes()))],
            'status' => ['nullable', 'in:draft,published,archived'],
            'attachments' => ['nullable', 'array'],
            'attachments.*' => ['string', 'max:500'],
            'documents' => ['nullable', 'array'],
            'documents.*' => ['string', 'max:500'],
            'create_invoice' => ['nullable', 'boolean'],
            'total_cost' => ['nullable', 'numeric', 'min:0'],
            'invoice_image' => ['nullable', 'string', 'max:500'],
            'invoice_items' => ['nullable', 'array'],
            'invoice_items.*.title' => ['required_with:invoice_items', 'string', 'max:255'],
            'invoice_items.*.amount' => ['nullable', 'numeric', 'min:0'],
            'metrics' => ['nullable', 'array'],
            'metrics.current' => ['nullable', 'numeric'],
            'metrics.target' => ['nullable', 'numeric'],
            'metrics.daily_growth' => ['nullable', 'numeric'],
            'items' => [$isDraft ? 'nullable' : 'required', 'array', 'min:1'],
            'items.*.product_id' => ['nullable', 'integer', 'exists:products,id'],
            'items.*.group_name' => ['nullable', 'string', 'max:120'],
            'items.*.unit' => ['nullable', 'string', 'max:50'],
            'items.*.min_value' => ['nullable', 'numeric'],
            'items.*.max_value' => ['nullable', 'numeric', 'gte:items.*.min_value'],
            'items.*.value' => ['nullable', 'numeric'],
            'items.*.avg_value' => ['nullable', 'numeric'],
            'items.*.action_note' => ['nullable', 'string', 'max:2000'],
        ];
    }

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