<?php

namespace Database\Seeders;

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

class VineyardRowSeeder extends Seeder
{
    use WithoutModelEvents;

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

        $rows = [
            // Grüner Veltliner - 3 rows
            [
                'variety' => 'Grüner Veltliner',
                'vine_count' => 120,
                'planting_year' => 2015,
                'area' => 450.50,
                'location' => [2, 1],
                'status' => 'active',
                'notes' => 'Oldest vineyard row, excellent quality',
            ],
            [
                'variety' => 'Grüner Veltliner',
                'vine_count' => 115,
                'planting_year' => 2016,
                'area' => 430.00,
                'location' => [2, 2],
                'status' => 'active',
                'notes' => 'North-facing slope, good drainage',
            ],
            [
                'variety' => 'Grüner Veltliner',
                'vine_count' => 125,
                'planting_year' => 2015,
                'area' => 465.75,
                'location' => [2, 3],
                'status' => 'active',
                'notes' => 'Premium section with old vines',
            ],

            // Riesling - 3 rows
            [
                'variety' => 'Riesling',
                'vine_count' => 100,
                'planting_year' => 2016,
                'area' => 380.00,
                'location' => [3, 1],
                'status' => 'active',
                'notes' => 'Optimal sun orientation',
            ],
            [
                'variety' => 'Riesling',
                'vine_count' => 95,
                'planting_year' => 2017,
                'area' => 365.00,
                'location' => [3, 2],
                'status' => 'active',
                'notes' => 'Steep slope, excellent ripening',
            ],
            [
                'variety' => 'Riesling',
                'vine_count' => 105,
                'planting_year' => 2016,
                'area' => 395.50,
                'location' => [3, 3],
                'status' => 'active',
                'notes' => 'Protected from wind',
            ],

            // Blaufränkisch - 4 rows
            [
                'variety' => 'Blaufränkisch',
                'vine_count' => 150,
                'planting_year' => 2014,
                'area' => 520.75,
                'location' => [4, 2],
                'status' => 'active',
                'notes' => 'Oldest red variety',
            ],
            [
                'variety' => 'Blaufränkisch',
                'vine_count' => 140,
                'planting_year' => 2015,
                'area' => 495.00,
                'location' => [4, 3],
                'status' => 'active',
                'notes' => 'South-facing, ideal for reds',
            ],
            [
                'variety' => 'Blaufränkisch',
                'vine_count' => 145,
                'planting_year' => 2014,
                'area' => 510.25,
                'location' => [4, 4],
                'status' => 'active',
                'notes' => 'Clay-rich soil, intense flavor',
            ],
            [
                'variety' => 'Blaufränkisch',
                'vine_count' => 135,
                'planting_year' => 2016,
                'area' => 480.00,
                'location' => [4, 5],
                'status' => 'active',
                'notes' => 'Younger vines, developing well',
            ],

            // St. Laurent - 2 rows
            [
                'variety' => 'St. Laurent',
                'vine_count' => 80,
                'planting_year' => 2017,
                'area' => 290.30,
                'location' => [5, 2],
                'status' => 'active',
                'notes' => 'Younger planting, doing well',
            ],
            [
                'variety' => 'St. Laurent',
                'vine_count' => 85,
                'planting_year' => 2018,
                'area' => 305.50,
                'location' => [5, 3],
                'status' => 'active',
                'notes' => 'Experimental clone, promising results',
            ],

            // Müller Thurgau - 3 rows
            [
                'variety' => 'Müller Thurgau',
                'vine_count' => 110,
                'planting_year' => 2018,
                'area' => 410.20,
                'location' => [1, 3],
                'status' => 'active',
                'notes' => 'Test planting of new clone',
            ],
            [
                'variety' => 'Müller Thurgau',
                'vine_count' => 105,
                'planting_year' => 2019,
                'area' => 395.00,
                'location' => [1, 4],
                'status' => 'active',
                'notes' => 'Cool microclimate, early harvest',
            ],
            [
                'variety' => 'Müller Thurgau',
                'vine_count' => 115,
                'planting_year' => 2018,
                'area' => 425.75,
                'location' => [1, 5],
                'status' => 'active',
                'notes' => 'Higher elevation, good acidity',
            ],

            // Chardonnay - 3 rows
            [
                'variety' => 'Chardonnay',
                'vine_count' => 95,
                'planting_year' => 2019,
                'area' => 350.00,
                'location' => [2, 4],
                'status' => 'active',
                'notes' => 'Premium variety, requires care',
            ],
            [
                'variety' => 'Chardonnay',
                'vine_count' => 90,
                'planting_year' => 2020,
                'area' => 335.50,
                'location' => [2, 5],
                'status' => 'active',
                'notes' => 'Newest planting, organic certified',
            ],
            [
                'variety' => 'Chardonnay',
                'vine_count' => 100,
                'planting_year' => 2019,
                'area' => 365.00,
                'location' => [2, 6],
                'status' => 'active',
                'notes' => 'Reserve quality potential',
            ],
        ];

        foreach ($rows as $row) {
            $variation = $variationByVariety->get($row['variety']);

            if (! $variation) {
                continue;
            }

            $location = $row['location'];
            if (is_array($location) && count($location) === 2) {
                $location = sprintf('%d,%d', $location[0], $location[1]);
            }

            VineyardRow::updateOrCreate(
                ['variety_variation_id' => $variation->id],
                [
                    'vine_count' => $row['vine_count'],
                    'planting_year' => $row['planting_year'],
                    'area' => $row['area'],
                    'location' => $location,
                    'status' => $row['status'],
                    'notes' => $row['notes'],
                ]
            );
        }

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