<?php

namespace Database\Seeders;

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

class WateringSeeder 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);

        $schedules = [
            [
                'variety' => 'Grüner Veltliner',
                'treatment_note' => 'Irrigation cycle before flowering',
                'watering_note' => 'Applied via drip system to maintain soil moisture',
                'time_interval' => 72,
                'amount' => 120.50,
                'planned_for' => now()->addDays(5)->format('Y-m-d'),
            ],
            [
                'variety' => 'Müller Thurgau',
                'treatment_note' => 'Post-harvest irrigation',
                'watering_note' => 'Deep soak to support root recovery',
                'time_interval' => 96,
                'amount' => 150.00,
                'planned_for' => now()->addWeeks(3)->format('Y-m-d'),
            ],
            [
                'variety' => 'Riesling',
                'treatment_note' => 'Summer watering cycle',
                'watering_note' => 'Regular irrigation during dry period',
                'time_interval' => 48,
                'amount' => 100.00,
                'completed_at' => now()->subDays(10)->format('Y-m-d'),
            ],
            [
                'variety' => 'Blaufränkisch',
                'treatment_note' => 'Emergency drought response irrigation',
                'watering_note' => 'Deep root watering to prevent vine stress',
                'time_interval' => 24,
                'amount' => 200.00,
                'planned_for' => now()->subDays(3)->format('Y-m-d'),
            ],
        ];

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

            if (! $row) {
                continue;
            }

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

            $watering = Watering::updateOrCreate(
                ['treatment_id' => $treatment->treatment_id],
                [
                    'time_interval' => $schedule['time_interval'],
                    'amount' => $schedule['amount'],
                    'note' => $schedule['watering_note'],
                ]
            );

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

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