<?php

namespace Database\Seeders;

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

class WineSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Efficient bulk query for grape varieties
        $varieties = GrapeVariety::pluck('id', 'variety_name');

        $wines = [
            // ========== GRÜNER VELTLINER (4 wines) ==========
            [
                'variety' => 'Grüner Veltliner',
                'vintage' => 2023,
                'wine_name' => 'Grüner Veltliner Cabinet',
                'wine_type' => 'white',
                'sweetness' => 'dry',
                'description' => 'Fresh, light-bodied white wine with citrus and green apple notes. Perfect aperitif with excellent acidity.',
                'alcohol_percentage' => 11.5,
                'bottles_produced' => 1500,
                'bottles_in_stock' => 1320,
                'price_per_bottle' => 19.00,
                'bottle_volume' => 0.75,
                'production_date' => '2023-10-05',
                'bottling_date' => '2024-02-20',
                'status' => 'ready',
                'image_url' => 'wines/Gruner-Veltliner-Cabinet.png',
            ],
            [
                'variety' => 'Grüner Veltliner',
                'vintage' => 2023,
                'wine_name' => 'Grüner Veltliner Late Harvest',
                'wine_type' => 'white',
                'sweetness' => 'semi_dry',
                'description' => 'Late harvest wine with ripe stone fruit aromas, honey undertones, and balanced sweetness. Pairs beautifully with Asian cuisine.',
                'alcohol_percentage' => 12.5,
                'bottles_produced' => 1200,
                'bottles_in_stock' => 1150,
                'price_per_bottle' => 19.00,
                'bottle_volume' => 0.75,
                'production_date' => '2023-10-25',
                'bottling_date' => '2024-03-15',
                'status' => 'ready',
                'image_url' => 'wines/Gruner-Veltliner-Late-Harvest.png',
            ],
            [
                'variety' => 'Grüner Veltliner',
                'vintage' => 2022,
                'wine_name' => 'Grüner Veltliner Grape Selection',
                'wine_type' => 'white',
                'sweetness' => 'semi_sweet',
                'description' => 'Selection of finest grape clusters. Rich, complex wine with tropical fruit, hints of white pepper, and elegant finish.',
                'alcohol_percentage' => 13.0,
                'bottles_produced' => 800,
                'bottles_in_stock' => 650,
                'price_per_bottle' => 24.90,
                'bottle_volume' => 0.75,
                'production_date' => '2022-11-10',
                'bottling_date' => '2023-04-20',
                'status' => 'ready',
                'image_url' => 'wines/Gruner-Veltliner-Grape-Selection.png',
            ],
            [
                'variety' => 'Grüner Veltliner',
                'vintage' => 2024,
                'wine_name' => 'Grüner Veltliner Young Wine',
                'wine_type' => 'white',
                'sweetness' => 'dry',
                'description' => 'Young wine with vibrant freshness, grassy notes, and crisp minerality. Currently aging to develop full character.',
                'alcohol_percentage' => 11.0,
                'bottles_produced' => 1800,
                'bottles_in_stock' => 1800,
                'price_per_bottle' => 10.90,
                'bottle_volume' => 0.75,
                'production_date' => '2024-10-01',
                'bottling_date' => '2025-03-01',
                'status' => 'aging',
                'image_url' => 'wines/Grüner Veltliner Young Wine.png',
            ],

            // ========== RIESLING (3 wines) ==========
            [
                'variety' => 'Riesling',
                'vintage' => 2022,
                'wine_name' => 'Riesling Dry',
                'wine_type' => 'white',
                'sweetness' => 'dry',
                'description' => 'Dry Riesling with classic petrol notes, citrus aromas, and mineral finish. Crisp acidity makes it ideal for seafood.',
                'alcohol_percentage' => 12.0,
                'bottles_produced' => 900,
                'bottles_in_stock' => 825,
                'price_per_bottle' => 17.90,
                'bottle_volume' => 0.75,
                'production_date' => '2022-10-15',
                'bottling_date' => '2023-03-10',
                'status' => 'ready',
                'image_url' => 'wines/Riesling-Dry.png',
            ],
            [
                'variety' => 'Riesling',
                'vintage' => 2021,
                'wine_name' => 'Riesling Berry Selection',
                'wine_type' => 'white',
                'sweetness' => 'sweet',
                'description' => 'Noble sweet wine from individually selected berries. Luscious apricot, honey, and orange marmalade flavors. Perfect dessert wine.',
                'alcohol_percentage' => 11.5,
                'bottles_produced' => 500,
                'bottles_in_stock' => 380,
                'price_per_bottle' => 39.90,
                'bottle_volume' => 0.75,
                'production_date' => '2021-11-20',
                'bottling_date' => '2022-05-15',
                'status' => 'ready',
                'image_url' => 'wines/Riesling-Berry-Selection.png',
            ],
            [
                'variety' => 'Riesling',
                'vintage' => 2024,
                'wine_name' => 'Riesling Classic',
                'wine_type' => 'white',
                'sweetness' => 'dry',
                'description' => 'Classic Riesling style with green apple, peach, and petrol notes. Still developing in the cellar.',
                'alcohol_percentage' => 12.0,
                'bottles_produced' => 1100,
                'bottles_in_stock' => 1100,
                'price_per_bottle' => 19.90,
                'bottle_volume' => 0.75,
                'production_date' => '2024-10-10',
                'bottling_date' => '2025-04-01',
                'status' => 'aging',
                'image_url' => 'wines/Riesling Classic.png',
            ],

            // ========== MÜLLER THURGAU (2 wines) ==========
            [
                'variety' => 'Müller Thurgau',
                'vintage' => 2023,
                'wine_name' => 'Müller Thurgau Cabinet',
                'wine_type' => 'white',
                'sweetness' => 'semi_dry',
                'description' => 'Easy-drinking wine with delicate muscat aroma, elderflower notes, and refreshing character. Perfect for warm summer evenings.',
                'alcohol_percentage' => 11.0,
                'bottles_produced' => 1600,
                'bottles_in_stock' => 1450,
                'price_per_bottle' => 9.00,
                'bottle_volume' => 0.75,
                'production_date' => '2023-09-20',
                'bottling_date' => '2024-01-15',
                'status' => 'ready',
                'image_url' => 'wines/Muller-Thurgau-Cabinet.png',
            ],
            [
                'variety' => 'Müller Thurgau',
                'vintage' => 2024,
                'wine_name' => 'Müller Thurgau Young',
                'wine_type' => 'white',
                'sweetness' => 'semi_dry',
                'description' => 'Young, fresh wine with aromatic profile. Floral and fruity character will develop beautifully. Currently in production.',
                'alcohol_percentage' => 10.5,
                'bottles_produced' => 2000,
                'bottles_in_stock' => 2000,
                'price_per_bottle' => 8.90,
                'bottle_volume' => 0.75,
                'production_date' => '2024-09-25',
                'bottling_date' => '2025-01-20',
                'status' => 'in_production',
                'image_url' => 'wines/Müller Thurgau Young.png',
            ],

            // ========== CHARDONNAY (3 wines) ==========
            [
                'variety' => 'Chardonnay',
                'vintage' => 2022,
                'wine_name' => 'Chardonnay Barrique',
                'wine_type' => 'white',
                'sweetness' => 'dry',
                'description' => 'Premium Chardonnay aged in French oak barrels. Rich, buttery texture with vanilla, toasted nuts, and ripe apple. Complex and elegant.',
                'alcohol_percentage' => 13.5,
                'bottles_produced' => 700,
                'bottles_in_stock' => 580,
                'price_per_bottle' => 27.90,
                'bottle_volume' => 0.75,
                'production_date' => '2022-10-01',
                'bottling_date' => '2023-09-15',
                'status' => 'ready',
                'image_url' => 'wines/Chardonnay-Barrique.png',
            ],
            [
                'variety' => 'Chardonnay',
                'vintage' => 2023,
                'wine_name' => 'Chardonnay Unfiltered',
                'wine_type' => 'white',
                'sweetness' => 'dry',
                'description' => 'Unfiltered Chardonnay with natural cloudiness. Full-bodied with citrus, pear, and mineral notes. Authentic, terroir-driven style.',
                'alcohol_percentage' => 12.5,
                'bottles_produced' => 900,
                'bottles_in_stock' => 820,
                'price_per_bottle' => 18.90,
                'bottle_volume' => 0.75,
                'production_date' => '2023-09-28',
                'bottling_date' => '2024-03-10',
                'status' => 'ready',
                'image_url' => 'wines/Chardonnay-Unfiltered.png',
            ],
            [
                'variety' => 'Chardonnay',
                'vintage' => 2024,
                'wine_name' => 'Chardonnay Classic',
                'wine_type' => 'white',
                'sweetness' => 'dry',
                'description' => 'Classic style Chardonnay without oak aging. Clean, fresh, with green apple and white flowers. Currently maturing in tank.',
                'alcohol_percentage' => 12.0,
                'bottles_produced' => 1200,
                'bottles_in_stock' => 1200,
                'price_per_bottle' => 13.90,
                'bottle_volume' => 0.75,
                'production_date' => '2024-09-30',
                'bottling_date' => '2025-02-15',
                'status' => 'aging',
                'image_url' => 'wines/Chardonnay Classic.png',
            ],

            // ========== BLAUFRÄNKISCH (4 wines) ==========
            [
                'variety' => 'Blaufränkisch',
                'vintage' => 2022,
                'wine_name' => 'Blaufränkisch Reserve',
                'wine_type' => 'red',
                'sweetness' => 'dry',
                'description' => 'Reserve quality red wine with deep ruby color. Intense blackberry, cherry, and spice. Firm tannins with excellent aging potential.',
                'alcohol_percentage' => 13.5,
                'bottles_produced' => 600,
                'bottles_in_stock' => 450,
                'price_per_bottle' => 25.90,
                'bottle_volume' => 0.75,
                'production_date' => '2022-10-20',
                'bottling_date' => '2024-03-15',
                'status' => 'ready',
                'image_url' => 'wines/Blaufrankisch-Reserve.png',
            ],
            [
                'variety' => 'Blaufränkisch',
                'vintage' => 2023,
                'wine_name' => 'Blaufränkisch Classic',
                'wine_type' => 'red',
                'sweetness' => 'dry',
                'description' => 'Classic style with pronounced spicy character, dark fruit, and medium body. Excellent with grilled meats and hearty stews.',
                'alcohol_percentage' => 12.5,
                'bottles_produced' => 1400,
                'bottles_in_stock' => 1150,
                'price_per_bottle' => 14.90,
                'bottle_volume' => 0.75,
                'production_date' => '2023-10-18',
                'bottling_date' => '2024-09-01',
                'status' => 'ready',
                'image_url' => 'wines/Blaufrankisch-Classic.png',
            ],
            [
                'variety' => 'Blaufränkisch',
                'vintage' => 2020,
                'wine_name' => 'Blaufränkisch Archive',
                'wine_type' => 'red',
                'sweetness' => 'dry',
                'description' => 'Mature archive wine from excellent vintage. Complex layers of dried fruit, leather, tobacco, and forest floor. Limited quantity.',
                'alcohol_percentage' => 13.0,
                'bottles_produced' => 400,
                'bottles_in_stock' => 0,
                'price_per_bottle' => 32.90,
                'bottle_volume' => 0.75,
                'production_date' => '2020-10-25',
                'bottling_date' => '2022-09-10',
                'status' => 'sold_out',
                'image_url' => 'wines/Blaufränkisch Archive.png',
            ],
            [
                'variety' => 'Blaufränkisch',
                'vintage' => 2024,
                'wine_name' => 'Blaufränkisch Young',
                'wine_type' => 'red',
                'sweetness' => 'dry',
                'description' => 'Young red wine with bright fruit and fresh acidity. Currently developing tannin structure and complexity during barrel aging.',
                'alcohol_percentage' => 12.0,
                'bottles_produced' => 1500,
                'bottles_in_stock' => 1500,
                'price_per_bottle' => 12.90,
                'bottle_volume' => 0.75,
                'production_date' => '2024-10-15',
                'bottling_date' => '2025-09-01',
                'status' => 'aging',
                'image_url' => 'wines/Blaufränkisch Young.png',
            ],

            // ========== ST. LAURENT (4 wines) ==========
            [
                'variety' => 'St. Laurent',
                'vintage' => 2022,
                'wine_name' => 'St. Laurent Barrique',
                'wine_type' => 'red',
                'sweetness' => 'dry',
                'description' => 'Oak-aged St. Laurent with velvety texture. Red cherry, vanilla, and subtle smoky notes. Elegant and sophisticated.',
                'alcohol_percentage' => 13.0,
                'bottles_produced' => 750,
                'bottles_in_stock' => 620,
                'price_per_bottle' => 22.90,
                'bottle_volume' => 0.75,
                'production_date' => '2022-10-12',
                'bottling_date' => '2024-02-28',
                'status' => 'ready',
                'image_url' => 'wines/St-Laurent-Barrique.png',
            ],
            [
                'variety' => 'St. Laurent',
                'vintage' => 2023,
                'wine_name' => 'St. Laurent Moravian',
                'wine_type' => 'red',
                'sweetness' => 'dry',
                'description' => 'Traditional Moravian style with soft tannins, red fruit, and silky mouthfeel. Approachable and food-friendly.',
                'alcohol_percentage' => 12.0,
                'bottles_produced' => 1200,
                'bottles_in_stock' => 950,
                'price_per_bottle' => 13.90,
                'bottle_volume' => 0.75,
                'production_date' => '2023-10-10',
                'bottling_date' => '2024-08-15',
                'status' => 'ready',
                'image_url' => 'wines/St-Laurent-Moravian.png',
            ],
            [
                'variety' => 'St. Laurent',
                'vintage' => 2023,
                'wine_name' => 'St. Laurent Rosé',
                'wine_type' => 'red',
                'sweetness' => 'semi_dry',
                'description' => 'Lighter style with emphasis on fresh fruit. Strawberry, raspberry, and floral notes. Versatile food pairing.',
                'alcohol_percentage' => 11.5,
                'bottles_produced' => 1000,
                'bottles_in_stock' => 870,
                'price_per_bottle' => 12.90,
                'bottle_volume' => 0.75,
                'production_date' => '2023-10-08',
                'bottling_date' => '2024-02-10',
                'status' => 'ready',
                'image_url' => 'wines/St-Laurent-Rose.png',
            ],
            [
                'variety' => 'St. Laurent',
                'vintage' => 2024,
                'wine_name' => 'St. Laurent New Wine',
                'wine_type' => 'red',
                'sweetness' => 'dry',
                'description' => 'New wine with vibrant color and youthful energy. Fresh red fruit and gentle tannins. Currently being produced.',
                'alcohol_percentage' => 11.0,
                'bottles_produced' => 1600,
                'bottles_in_stock' => 1600,
                'price_per_bottle' => 10.90,
                'bottle_volume' => 0.75,
                'production_date' => '2024-10-12',
                'bottling_date' => '2025-08-01',
                'status' => 'in_production',
                'image_url' => 'wines/St. Laurent New Wine.png',
            ],
        ];

        foreach ($wines as $wine) {
            $varietyName = $wine['variety'];

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

            $payload = $wine;
            unset($payload['variety']);
            $payload['grape_variety_id'] = $varieties[$varietyName];

            Wine::updateOrCreate(
                ['wine_name' => $payload['wine_name']],
                $payload
            );
        }

        $this->command->info('✓ Created/updated ' . count($wines) . ' wines');
    }
}
