<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreBulkPlannedTasksRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        $rows = $this->input('rows');
        if (is_string($rows)) {
            $rows = array_filter(array_map('trim', explode(',', $rows)));
        }

        $details = $this->input('details');
        if (is_string($details)) {
            $decoded = json_decode($details, true);
            $details = is_array($decoded) ? $decoded : [];
        }

        $plannedDate = $this->input('planned_date');
        if (! $plannedDate && $this->query('date')) {
            $plannedDate = $this->query('date');
        }

        $this->merge([
            'rows' => $rows,
            'details' => $details ?? [],
            'planned_date' => $plannedDate,
        ]);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $action = $this->input('action');

        $rules = [
            'action' => ['required', 'string', Rule::in(['watering', 'fertilization', 'spraying', 'pruning', 'harvest'])],
            'planned_date' => ['required', 'date'],
            'execution_date' => ['nullable', 'date', 'after_or_equal:planned_date'],
            'rows' => ['required', 'array', 'min:1'],
            'rows.*' => ['integer', 'exists:vineyard_rows,id'],
            'note' => ['nullable', 'string', 'max:1000'],
            'details' => ['nullable', 'array'],
            'details.time_interval' => ['nullable', 'integer', 'min:1'],
            'details.amount' => ['required_if:action,watering', 'numeric', 'min:0.01'],
            'details.substance' => ['required_if:action,fertilization', 'string', 'max:255'],
            'details.concentration' => ['nullable', 'numeric', 'min:0'],
            'details.pesticide' => ['required_if:action,spraying', 'string', 'max:255'],
            'details.method' => ['required_if:action,pruning', 'string', 'max:255'],
            'details.percentage_removed' => ['nullable', 'integer', 'min:0', 'max:100'],
            'details.weight' => ['nullable', 'numeric', 'min:0'],
            'details.sugar_content' => ['nullable', 'numeric', 'min:0'],
            'details.date' => ['nullable', 'date'],
            'details.quality' => ['nullable', 'string', 'max:255'],
            'details.condition' => ['nullable', 'string', 'max:255'],
            'details.weather' => ['nullable', 'string', 'max:255'],
            'details.time' => ['nullable', 'date_format:H:i'],
            'details.user_id' => ['nullable', 'integer', 'exists:users,id'],
            'details.variety_variation_id' => ['nullable', 'integer', 'exists:variety_variations,id'],
        ];

        if (in_array($action, ['watering', 'fertilization', 'spraying', 'pruning'], true)) {
            $rules['details'] = ['required', 'array'];
        }

        return $rules;
    }
}
