<?php

namespace Database\Seeders;

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

class FertilizationSeeder 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' => 'Riesling',
                'treatment_note' => 'Spring nitrogen fertilization',
                'substance' => 'Nitrogen mix 12-6-6',
                'concentration' => 0.75,
                'note' => 'Applied pre-budbreak to boost growth',
                'planned_for' => now()->addDays(7)->format('Y-m-d'),
            ],
            [
                'variety' => 'Chardonnay',
                'treatment_note' => 'Potassium boost application',
                'substance' => 'Potassium sulfate',
                'concentration' => 0.60,
                'note' => 'Pre-flowering nutrient supplement',
                'planned_for' => now()->addWeeks(2)->format('Y-m-d'),
            ],
            [
                'variety' => 'Grüner Veltliner',
                'treatment_note' => 'Autumn potassium fertilization',
                'substance' => 'Potassium sulfate',
                'concentration' => 0.60,
                'note' => 'Supports winter hardiness',
                'completed_at' => now()->subWeeks(2)->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'],
                ],
                []
            );

            $fertilization = Fertilization::updateOrCreate(
                ['treatment_id' => $treatment->treatment_id],
                [
                    'substance' => $application['substance'],
                    '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' => 'Fertilization',
                        'taskable_id' => $fertilization->getKey(),
                        'taskable_type' => Fertilization::class,
                    ],
                    [
                        'planned_date' => $plannedDate ?? $defaultDate,
                        'execution_date' => $completedDate,
                        'note' => $application['treatment_note'],
                    ]
                );
            }
        }
    }
}
