<?php

namespace Database\Seeders;

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

class VarietyVariationSeeder extends Seeder
{
    use WithoutModelEvents;

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

        $variations = [
            [
                'variety' => 'Grüner Veltliner',
                'color' => 'green',
                'description' => 'Classic green variation',
                'typical_sugar_content' => 19.5,
                'typical_alcohol' => 12.5,
                'ripening_period' => 'September - early October',
                'status' => 'active'
            ],
            [
                'variety' => 'Riesling',
                'color' => 'green',
                'description' => 'Green variation with high acidity',
                'typical_sugar_content' => 21.0,
                'typical_alcohol' => 13.0,
                'ripening_period' => 'Late September - mid-October',
                'status' => 'active'
            ],
            [
                'variety' => 'Blaufränkisch',
                'color' => 'blue',
                'description' => 'Blue variation with pronounced tannins',
                'typical_sugar_content' => 22.5,
                'typical_alcohol' => 12.0,
                'ripening_period' => 'Mid-October',
                'status' => 'active'
            ],
            [
                'variety' => 'St. Laurent',
                'color' => 'blue',
                'description' => 'Blue variation with delicate tannins',
                'typical_sugar_content' => 20.0,
                'typical_alcohol' => 11.5,
                'ripening_period' => 'Early October',
                'status' => 'active'
            ],
            [
                'variety' => 'Müller Thurgau',
                'color' => 'green',
                'description' => 'Early green variation',
                'typical_sugar_content' => 18.0,
                'typical_alcohol' => 11.0,
                'ripening_period' => 'Early September',
                'status' => 'active'
            ],
            [
                'variety' => 'Chardonnay',
                'color' => 'green',
                'description' => 'Premium green variation',
                'typical_sugar_content' => 21.5,
                'typical_alcohol' => 13.5,
                'ripening_period' => 'Mid-October',
                'status' => 'active'
            ],
        ];

        foreach ($variations as $variation) {
            $varietyName = $variation['variety'];

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

            VarietyVariation::updateOrCreate(
                [
                    'grape_variety_id' => $varietyIds[$varietyName],
                    'color' => $variation['color'],
                ],
                [
                    'description' => $variation['description'],
                    'typical_sugar_content' => $variation['typical_sugar_content'],
                    'typical_alcohol' => $variation['typical_alcohol'],
                    'ripening_period' => $variation['ripening_period'],
                    'status' => $variation['status'],
                ]
            );
        }

        if ($this->command) {
            $this->command->info('✓ Created ' . count($variations) . ' variety variations');
        }
    }
}
