<?php

namespace Database\Seeders;

use App\Models\GrapeVariety;
use App\Models\GrapeVarietyVariant;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class GrapeVarietyVariantSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $varietyIds = GrapeVariety::pluck('id', 'variety_name');

        $variants = [
            ['variety' => 'Grüner Veltliner', 'color' => 'green'],
            ['variety' => 'Grüner Veltliner', 'color' => 'yellow'],
            ['variety' => 'Riesling', 'color' => 'green'],
            ['variety' => 'Riesling', 'color' => 'yellow'],
            ['variety' => 'Blaufränkisch', 'color' => 'blue'],
            ['variety' => 'St. Laurent', 'color' => 'red'],
            ['variety' => 'Müller Thurgau', 'color' => 'green'],
            ['variety' => 'Chardonnay', 'color' => 'yellow'],
        ];

        foreach ($variants as $variant) {
            $varietyName = $variant['variety'];

            if (! isset($varietyIds[$varietyName])) {
                continue;
            }

            GrapeVarietyVariant::updateOrCreate(
                [
                    'grape_variety_id' => $varietyIds[$varietyName],
                    'color' => $variant['color'],
                ],
                []
            );
        }
    }
}
