<?php

namespace Database\Seeders;

use App\Models\Event;
use App\Models\EventOccurrence;
use App\Models\Location;
use App\Models\Source;
use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class EventSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Locations erstellen
        $locations = [
            [
                'name' => 'Tiergarten Berlin',
                'street' => 'Straße des 17. Juni',
                'house_number' => '100',
                'postal_code' => '10785',
                'city' => 'Berlin',
                'state' => 'Berlin',
                'country' => 'Germany',
                'phone' => '+49 30 39403501',
                'email' => 'info@tiergarten.de',
                'website' => 'https://www.tiergarten.de',
            ],
            [
                'name' => 'Kunsthalle München',
                'street' => 'Theresienstraße',
                'house_number' => '15',
                'postal_code' => '80333',
                'city' => 'München',
                'state' => 'Bayern',
                'country' => 'Germany',
                'phone' => '+49 89 598183360',
                'email' => 'info@kunsthalle-muenchen.de',
                'website' => 'https://www.kunsthalle-muenchen.de',
            ],
            [
                'name' => 'Rathaus Hamburg',
                'street' => 'Rathausplatz',
                'house_number' => '1',
                'postal_code' => '20095',
                'city' => 'Hamburg',
                'state' => 'Hamburg',
                'country' => 'Germany',
                'phone' => '+49 40 4288332211',
                'email' => 'events@hamburg.de',
                'website' => 'https://www.hamburg.de',
            ],
            [
                'name' => 'Cinemaxx Köln',
                'street' => 'Hansaring',
                'house_number' => '72-76',
                'postal_code' => '50670',
                'city' => 'Köln',
                'state' => 'Nordrhein-Westfalen',
                'country' => 'Germany',
                'phone' => '+49 221 5677580',
                'email' => 'info@cinemaxx-koeln.de',
                'website' => 'https://www.cinemaxx.de',
            ],
            [
                'name' => 'Convention Center Frankfurt',
                'street' => 'Ludwigstraße',
                'house_number' => '285',
                'postal_code' => '60327',
                'city' => 'Frankfurt',
                'state' => 'Hessen',
                'country' => 'Germany',
                'phone' => '+49 69 7566960',
                'email' => 'info@convention-frankfurt.de',
                'website' => 'https://www.convention-frankfurt.de',
            ],
            [
                'name' => 'Staatstheater Stuttgart',
                'street' => 'Königstraße',
                'house_number' => '6',
                'postal_code' => '70173',
                'city' => 'Stuttgart',
                'state' => 'Baden-Württemberg',
                'country' => 'Germany',
                'phone' => '+49 711 2090500',
                'email' => 'info@staatstheater-stuttgart.de',
                'website' => 'https://www.staatstheater-stuttgart.de',
            ],
            [
                'name' => 'Altstadt Nürnberg',
                'street' => 'Marktplatz',
                'house_number' => '25',
                'postal_code' => '90403',
                'city' => 'Nürnberg',
                'state' => 'Bayern',
                'country' => 'Germany',
                'phone' => '+49 911 2336123',
                'email' => 'info@nuernberg-events.de',
                'website' => 'https://www.nuernberg.de',
            ],
            [
                'name' => 'Stadtpark Düsseldorf',
                'street' => 'Königsallee',
                'house_number' => '60',
                'postal_code' => '40212',
                'city' => 'Düsseldorf',
                'state' => 'Nordrhein-Westfalen',
                'country' => 'Germany',
                'phone' => '+49 211 8991000',
                'email' => 'info@duesseldorf-events.de',
                'website' => 'https://www.duesseldorf.de',
            ],
            [
                'name' => 'Stadtbibliothek Leipzig',
                'street' => 'Wilhelmstraße',
                'house_number' => '32',
                'postal_code' => '04109',
                'city' => 'Leipzig',
                'state' => 'Sachsen',
                'country' => 'Germany',
                'phone' => '+49 341 1233503',
                'email' => 'info@leipzig-bibliothek.de',
                'website' => 'https://www.leipzig-bibliothek.de',
            ],
            [
                'name' => 'Kochschule Meyer Dresden',
                'street' => 'Königstraße',
                'house_number' => '15',
                'postal_code' => '01097',
                'city' => 'Dresden',
                'state' => 'Sachsen',
                'country' => 'Germany',
                'phone' => '+49 351 4956789',
                'email' => 'info@kochschule-meyer.de',
                'website' => 'https://www.kochschule-meyer.de',
            ],
        ];

        $locationMap = [];
        foreach ($locations as $locationData) {
            // Automatisch Slug generieren wenn nicht vorhanden
            if (!isset($locationData['slug'])) {
                $locationData['slug'] = Str::slug($locationData['name']);
            }
            
            $location = Location::firstOrCreate(
                ['name' => $locationData['name']],
                $locationData
            );
            $locationMap[] = $location->id;
        }

        // Source erstellen
        $source = Source::firstOrCreate(['name' => 'Demo Source']);

        $events = [
            [
                'title' => 'Jazz-Konzert im Park',
                'description' => 'Ein wunderschönes Jazz-Konzert mit bekannten Künstlern unter freiem Himmel. Genießen Sie klassischen Jazz bei schönem Wetter.',
                'location_idx' => 0,
                'category' => 'Musik',
                'image_url' => null,
            ],
            [
                'title' => 'Kunstausstellung "Modern Wonders"',
                'description' => 'Zeitgenössische Kunstwerke von internationalen Künstlern. Eine Reise durch moderne Kunstrichtungen.',
                'location_idx' => 1,
                'category' => 'Kunst',
                'image_url' => null,
            ],
            [
                'title' => 'Marathon 2026',
                'description' => 'Halbmarathon durch die Stadt. Meldung erforderlich. Für verschiedene Leistungsstufen geeignet.',
                'location_idx' => 2,
                'category' => 'Sport',
                'image_url' => null,
            ],
            [
                'title' => 'Film-Festival "Indie Nights"',
                'description' => 'Die besten Independentfilme des Jahres. Entdecken Sie neue Talente im internationalen Kino.',
                'location_idx' => 3,
                'category' => 'Film',
                'image_url' => null,
            ],
            [
                'title' => 'Technologie-Konferenz 2026',
                'description' => 'Die neuesten Trends in AI und Cloud Computing. Vorträge von Experten und Networking-Sessions.',
                'location_idx' => 4,
                'category' => 'Technologie',
                'image_url' => null,
            ],
            [
                'title' => 'Theaterstück: "Der Traum"',
                'description' => 'Eine klassische Theaterproduktion in moderner Inszenierung. Ein Muss für Theaterliebhaber.',
                'location_idx' => 5,
                'category' => 'Theater',
                'image_url' => null,
            ],
            [
                'title' => 'Sommerfest Altstadt',
                'description' => 'Traditionelles Volksfest mit Musik, Essen und Unterhaltung für die ganze Familie.',
                'location_idx' => 6,
                'category' => 'Festival',
                'image_url' => null,
            ],
            [
                'title' => 'Yoga und Meditation Workshop',
                'description' => 'Kostenloser Workshop für Anfänger und Fortgeschrittene. Bitte Matte mitbringen.',
                'location_idx' => 7,
                'category' => 'Wellness',
                'image_url' => null,
            ],
            [
                'title' => 'Buchlesung: "Das Erbe"',
                'description' => 'Die Autorin liest aus ihrem neuen Roman. Anschließend Signieren und Austausch mit Lesern.',
                'location_idx' => 8,
                'category' => 'Literatur',
                'image_url' => null,
            ],
            [
                'title' => 'Kochkurs "Italienische Küche"',
                'description' => 'Lernen Sie authentische italienische Gerichte von einem Proffikoch. Inkl. Verkostung.',
                'location_idx' => 9,
                'category' => 'Kulinarik',
                'image_url' => null,
            ],
        ];

        foreach ($events as $eventData) {
            $event = Event::create([
                'source_id' => $source->id,
                'location_id' => $locationMap[$eventData['location_idx']],
                'title' => $eventData['title'],
                'description' => $eventData['description'],
                'category' => $eventData['category'],
                'slug' => \Illuminate\Support\Str::slug($eventData['title']),
                'image_url' => $eventData['image_url'],
                'status' => 'published',
            ]);

            // Erste Termin
            EventOccurrence::create([
                'event_id' => $event->id,
                'start_datetime' => Carbon::now()->addDays(rand(5, 15))->setHour(rand(14, 19))->setMinute(0),
                'end_datetime' => Carbon::now()->addDays(rand(5, 15))->setHour(rand(20, 23))->setMinute(0),
                'status' => 'scheduled',
            ]);

            // Zweiter Termin
            EventOccurrence::create([
                'event_id' => $event->id,
                'start_datetime' => Carbon::now()->addDays(rand(20, 40))->setHour(rand(14, 19))->setMinute(0),
                'end_datetime' => Carbon::now()->addDays(rand(20, 40))->setHour(rand(20, 23))->setMinute(0),
                'status' => 'scheduled',
            ]);

            // Dritter Termin
            EventOccurrence::create([
                'event_id' => $event->id,
                'start_datetime' => Carbon::now()->addDays(rand(45, 90))->setHour(rand(14, 19))->setMinute(0),
                'end_datetime' => Carbon::now()->addDays(rand(45, 90))->setHour(rand(20, 23))->setMinute(0),
                'status' => 'scheduled',
            ]);
        }
    }
}
