<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\PlannedTask;
use App\Models\Spraying;
use App\Models\Treatment;
use App\Models\VineyardRow;

class SprayingSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $rows = VineyardRow::with('varietyVariation.grapeVariety')
            ->get()
            ->keyBy(fn (VineyardRow $row) => $row->varietyVariation->grapeVariety->variety_name ?? $row->id);

        $applications = [
            [
                'variety' => 'Blaufränkisch',
                'treatment_note' => 'Fungicide application for mildew prevention',
                'pesticide' => 'Copper sulfate',
                'concentration' => 0.20,
                'note' => 'Preventive treatment before rainy season',
                'planned_for' => now()->addDays(3)->format('Y-m-d'),
            ],
            [
                'variety' => 'St. Laurent',
                'treatment_note' => 'Botrytis control spray',
                'pesticide' => 'Sulfur-lime mix',
                'concentration' => 0.15,
                'note' => 'Evening spray to avoid leaf burn',
                'planned_for' => now()->addWeeks(1)->format('Y-m-d'),
            ],
            [
                'variety' => 'Riesling',
                'treatment_note' => 'Organic pest control application',
                'pesticide' => 'Dithane M-45',
                'concentration' => 0.18,
                'note' => 'Preventive spray for pest management',
                'planned_for' => now()->addWeeks(5)->format('Y-m-d'),
            ],
            [
                'variety' => 'Chardonnay',
                'treatment_note' => 'Pre-flowering mildew protection',
                'pesticide' => 'Copper sulfate',
                'concentration' => 0.20,
                'note' => 'Applied to prevent downy mildew before flowering',
                'completed_at' => now()->subWeeks(3)->format('Y-m-d'),
            ],
            [
                'variety' => 'Grüner Veltliner',
                'treatment_note' => 'Urgent aphid control treatment',
                'pesticide' => 'Neem oil solution',
                'concentration' => 0.12,
                'note' => 'Apply in early morning to avoid leaf damage',
                'planned_for' => now()->format('Y-m-d'),
            ],
        ];

        foreach ($applications as $application) {
            $row = $rows[$application['variety']] ?? null;

            if (! $row) {
                continue;
            }

            $treatment = Treatment::updateOrCreate(
                [
                    'row_id' => $row->id,
                    'note' => $application['treatment_note'],
                ],
                []
            );

            $spraying = Spraying::updateOrCreate(
                ['treatment_id' => $treatment->treatment_id],
                [
                    'pesticide' => $application['pesticide'],
                    'concentration' => $application['concentration'],
                    'note' => $application['note'],
                ]
            );

            $plannedDate = $application['planned_for'] ?? null;
            $completedDate = $application['completed_at'] ?? null;
            $defaultDate = $plannedDate ?? $completedDate;

            if ($plannedDate || $completedDate || $defaultDate) {
                PlannedTask::updateOrCreate(
                    [
                        'type' => 'Spraying',
                        'taskable_id' => $spraying->getKey(),
                        'taskable_type' => Spraying::class,
                    ],
                    [
                        'planned_date' => $plannedDate ?? $defaultDate,
                        'execution_date' => $completedDate,
                        'note' => $application['treatment_note'],
                    ]
                );
            }
        }
    }
}
