<?php

namespace Database\Seeders;

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

class PruningSeeder extends Seeder
{
    use WithoutModelEvents;

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

        $sessions = [
            [
                'variety' => 'Grüner Veltliner',
                'treatment_note' => 'Summer canopy thinning',
                'method' => 'Canopy thinning',
                'percentage_removed' => 20,
                'note' => 'Light thinning for air circulation',
                'planned_for' => now()->addDays(10)->format('Y-m-d'),
            ],
            [
                'variety' => 'Riesling',
                'treatment_note' => 'Leaf removal around clusters',
                'method' => 'Leaf thinning',
                'percentage_removed' => 15,
                'note' => 'Improve sunlight exposure and air circulation',
                'planned_for' => now()->addWeeks(4)->format('Y-m-d'),
            ],
            [
                'variety' => 'Blaufränkisch',
                'treatment_note' => 'Winter spur pruning',
                'method' => 'Spur pruning',
                'percentage_removed' => 35,
                'note' => 'Balanced spur pruning to manage yield',
                'completed_at' => now()->subMonths(1)->format('Y-m-d'),
            ],
        ];

        foreach ($sessions as $session) {
            $row = $vineyardRows[$session['variety']] ?? null;

            if (! $row) {
                continue;
            }

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

            $pruning = Pruning::updateOrCreate(
                ['treatment_id' => $treatment->treatment_id],
                [
                    'method' => $session['method'],
                    'percentage_removed' => $session['percentage_removed'],
                    'note' => $session['note'],
                ]
            );

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

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