<?php

namespace Database\Seeders;

use App\Models\Emotion;
use App\Models\EventType;
use App\Models\Task;
use App\Models\TaskCollection;
use App\Models\Tenant;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class TasksSeeder extends Seeder
{
    public function run(): void
    {
        $demoTenant = Tenant::where('slug', 'demo-tenant')->first();

        if (! $demoTenant) {
            $this->command?->warn('Demo tenant not found, skipping TasksSeeder');

            return;
        }

        $seed = [
            'Liebe' => [
                ['title' => ['de' => 'Kuss-Foto', 'en' => 'Kiss Photo'], 'description' => ['de' => 'Macht ein romantisches Kuss-Foto', 'en' => 'Take a romantic kiss photo'], 'difficulty' => 'easy'],
            ],
            'Freude' => [
                ['title' => ['de' => 'Sprung-Foto', 'en' => 'Jump Photo'], 'description' => ['de' => 'Alle springen gleichzeitig!', 'en' => 'Everyone jump together!'], 'difficulty' => 'medium'],
            ],
            'Teamgeist' => [
                ['title' => ['de' => 'High-Five-Runde', 'en' => 'High-Five Round'], 'description' => ['de' => 'Gebt euch High-Fives!', 'en' => 'Give each other high-fives!'], 'difficulty' => 'easy', 'event_type' => 'corporate'],
            ],
            'Besinnlichkeit' => [
                ['title' => ['de' => 'Lichterglanz', 'en' => 'Glow of Lights'], 'description' => ['de' => 'Foto mit Lichterkette', 'en' => 'Photo with string lights'], 'difficulty' => 'easy', 'event_type' => 'christmas'],
            ],
        ];

        $types = EventType::pluck('id', 'slug');
        $position = 10;

        foreach ($seed as $emotionNameDe => $tasks) {
            $emotion = Emotion::where('name->de', $emotionNameDe)->first();

            if (! $emotion) {
                continue;
            }

            $emotionTranslations = is_array($emotion->name) ? $emotion->name : [];
            $emotionNameEn = $emotionTranslations['en'] ?? $emotionNameDe;

            $collection = TaskCollection::updateOrCreate(
                [
                    'tenant_id' => $demoTenant->id,
                    'slug' => 'demo-'.Str::slug($emotionTranslations['en'] ?? $emotionNameDe),
                ],
                [
                    'name_translations' => [
                        'de' => $emotionNameDe,
                        'en' => $emotionNameEn,
                    ],
                    'description_translations' => [
                        'de' => 'Aufgaben rund um '.$emotionNameDe.'.',
                        'en' => 'Prompts inspired by '.$emotionNameEn.'.',
                    ],
                    'event_type_id' => null,
                    'is_default' => false,
                    'position' => $position,
                ]
            );

            $position += 10;
            $syncPayload = [];

            foreach ($tasks as $index => $t) {
                $slugBase = Str::slug($t['title']['en'] ?? $t['title']['de']);
                $slug = $slugBase ? $slugBase.'-'.$emotion->id : Str::uuid()->toString();

                $sortOrder = $t['sort_order'] ?? (($index + 1) * 10);

                $task = Task::updateOrCreate(
                    [
                        'slug' => $slug,
                    ],
                    [
                        'tenant_id' => $demoTenant->id,
                        'emotion_id' => $emotion->id,
                        'event_type_id' => isset($t['event_type'], $types[$t['event_type']]) ? $types[$t['event_type']] : null,
                        'title' => $t['title'],
                        'description' => $t['description'],
                        'difficulty' => $t['difficulty'],
                        'is_active' => true,
                        'sort_order' => $sortOrder,
                        'collection_id' => $collection->id,
                    ]
                );

                $syncPayload[$task->id] = ['sort_order' => $sortOrder];
            }

            if (! empty($syncPayload)) {
                $collection->tasks()->syncWithoutDetaching($syncPayload);
            }
        }
    }
}
