<?php

namespace Database\Seeders;

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

class WineProductionSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $wineIds = Wine::pluck('id', 'wine_name');
        $harvests = Harvest::with('vineyardRow.varietyVariation.grapeVariety')
            ->get()
            ->keyBy(fn (Harvest $harvest) => ($harvest->vineyardRow?->varietyVariation->grapeVariety->variety_name ?? $harvest->vineyard_row_id) . ':' . $harvest->date->format('Y-m-d'));

        $productions = [
            // ========== 2024 VINTAGE WINES ==========
            [
                'wine_name' => 'Grüner Veltliner Young Wine',
                'variety' => 'Grüner Veltliner',
                'date' => '2024-09-15',
                'consumed_weight' => 850.50,
                'blend_percentage' => 100.00,
            ],
            // Riesling Classic 2024 - blended from two harvests
            [
                'wine_name' => 'Riesling Classic',
                'variety' => 'Riesling',
                'date' => '2024-09-20',
                'consumed_weight' => 500.00,
                'blend_percentage' => 64.00,
            ],
            [
                'wine_name' => 'Riesling Classic',
                'variety' => 'Riesling',
                'date' => now()->subDays(18)->format('Y-m-d'),
                'consumed_weight' => 280.40,
                'blend_percentage' => 36.00,
            ],
            [
                'wine_name' => 'Müller Thurgau Young',
                'variety' => 'Müller Thurgau',
                'date' => '2024-09-10',
                'consumed_weight' => 780.20,
                'blend_percentage' => 100.00,
            ],
            [
                'wine_name' => 'Chardonnay Classic',
                'variety' => 'Chardonnay',
                'date' => now()->subDays(5)->format('Y-m-d'),
                'consumed_weight' => 420.50,
                'blend_percentage' => 100.00,
            ],
            // Blaufränkisch Young 2024 - blended from two harvests
            [
                'wine_name' => 'Blaufränkisch Young',
                'variety' => 'Blaufränkisch',
                'date' => '2024-10-05',
                'consumed_weight' => 650.00,
                'blend_percentage' => 59.00,
            ],
            [
                'wine_name' => 'Blaufränkisch Young',
                'variety' => 'Blaufränkisch',
                'date' => now()->subDays(8)->format('Y-m-d'),
                'consumed_weight' => 450.00,
                'blend_percentage' => 41.00,
            ],
            [
                'wine_name' => 'St. Laurent New Wine',
                'variety' => 'St. Laurent',
                'date' => '2024-10-01',
                'consumed_weight' => 600.75,
                'blend_percentage' => 100.00,
            ],

            // ========== 2023 VINTAGE WINES ==========
            [
                'wine_name' => 'Grüner Veltliner Cabinet',
                'variety' => 'Grüner Veltliner',
                'date' => '2023-09-18',
                'consumed_weight' => 750.00,
                'blend_percentage' => 100.00,
            ],
            [
                'wine_name' => 'Grüner Veltliner Late Harvest',
                'variety' => 'Grüner Veltliner',
                'date' => '2023-10-12',
                'consumed_weight' => 650.50,
                'blend_percentage' => 100.00,
            ],
            [
                'wine_name' => 'Müller Thurgau Cabinet',
                'variety' => 'Müller Thurgau',
                'date' => '2023-09-08',
                'consumed_weight' => 890.75,
                'blend_percentage' => 100.00,
            ],
            [
                'wine_name' => 'Chardonnay Unfiltered',
                'variety' => 'Chardonnay',
                'date' => '2023-09-22',
                'consumed_weight' => 580.30,
                'blend_percentage' => 100.00,
            ],
            [
                'wine_name' => 'Blaufränkisch Classic',
                'variety' => 'Blaufränkisch',
                'date' => '2023-10-08',
                'consumed_weight' => 900.00,
                'blend_percentage' => 100.00,
            ],
            [
                'wine_name' => 'St. Laurent Moravian',
                'variety' => 'St. Laurent',
                'date' => '2023-10-02',
                'consumed_weight' => 720.50,
                'blend_percentage' => 100.00,
            ],
            [
                'wine_name' => 'St. Laurent Rosé',
                'variety' => 'St. Laurent',
                'date' => '2023-09-28',
                'consumed_weight' => 480.25,
                'blend_percentage' => 100.00,
            ],

            // ========== 2022 VINTAGE WINES ==========
            [
                'wine_name' => 'Grüner Veltliner Grape Selection',
                'variety' => 'Grüner Veltliner',
                'date' => '2022-10-20',
                'consumed_weight' => 580.80,
                'blend_percentage' => 100.00,
            ],
            [
                'wine_name' => 'Riesling Dry',
                'variety' => 'Riesling',
                'date' => '2022-10-05',
                'consumed_weight' => 640.60,
                'blend_percentage' => 100.00,
            ],
            [
                'wine_name' => 'Chardonnay Barrique',
                'variety' => 'Chardonnay',
                'date' => '2022-09-25',
                'consumed_weight' => 510.40,
                'blend_percentage' => 100.00,
            ],
            // Blaufränkisch Reserve 2022 - premium blend from single harvest
            [
                'wine_name' => 'Blaufränkisch Reserve',
                'variety' => 'Blaufränkisch',
                'date' => '2022-10-12',
                'consumed_weight' => 680.75,
                'blend_percentage' => 100.00,
            ],
            [
                'wine_name' => 'St. Laurent Barrique',
                'variety' => 'St. Laurent',
                'date' => '2022-10-05',
                'consumed_weight' => 620.90,
                'blend_percentage' => 100.00,
            ],

            // ========== 2021 VINTAGE WINES ==========
            [
                'wine_name' => 'Riesling Berry Selection',
                'variety' => 'Riesling',
                'date' => '2021-11-08',
                'consumed_weight' => 420.50,
                'blend_percentage' => 100.00,
            ],

            // ========== 2020 VINTAGE WINES ==========
            [
                'wine_name' => 'Blaufränkisch Archive',
                'variety' => 'Blaufränkisch',
                'date' => '2020-10-18',
                'consumed_weight' => 550.00,
                'blend_percentage' => 100.00,
            ],
        ];

        foreach ($productions as $production) {
            $wineId = $wineIds[$production['wine_name']] ?? null;
            $harvestKey = $production['variety'] . ':' . $production['date'];
            $harvest = $harvests[$harvestKey] ?? null;

            if (! $wineId || ! $harvest) {
                continue;
            }

            WineProduction::updateOrCreate(
                [
                    'wine_id' => $wineId,
                    'harvest_id' => $harvest->id,
                ],
                [
                    'consumed_weight' => $production['consumed_weight'],
                    'blend_percentage' => $production['blend_percentage'],
                ]
            );
        }

        if ($this->command) {
            $this->command->info('✓ Created ' . count($productions) . ' wine production records');
        }
    }
}
