<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use App\Models\{Event, Emotion, Task, Photo};

class DemoAchievementsSeeder extends Seeder
{
    public function run(): void
    {
        $event = Event::where('slug', 'demo-wedding-2025')->first();
        if (!$event) {
            $event = Event::create([
                'slug' => 'demo-wedding-2025',
                'name' => ['de' => 'Demo Hochzeit 2025', 'en' => 'Demo Wedding 2025'],
                'description' => ['de' => 'Demo-Event', 'en' => 'Demo event'],
                'date' => now()->toDateString(),
                'event_type_id' => null,
                'is_active' => true,
                'settings' => [],
                'default_locale' => 'de',
            ]);
        }

        $emotions = Emotion::query()->take(6)->get();

        if (Task::count() === 0 && $emotions->isNotEmpty()) {
            foreach (range(1, 10) as $i) {
                $emo = $emotions->random();
                Task::create([
                    'title' => ['de' => "Aufgabe #$i", 'en' => "Task #$i"],
                    'description' => ['de' => 'Kurzbeschreibung', 'en' => 'Short description'],
                    'emotion_id' => $emo->id,
                    'is_active' => true,
                ]);
            }
        }

        $tasks = Task::inRandomOrder()->take(10)->get();
        if ($tasks->isEmpty()) {
            return; // nothing to seed
        }

        // Simple placeholder PNG (100x100)
        $png = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAGXRFWHRTb2Z0d2FyZQBwYWludC5uZXQgNC4xLjM2qefiAAABVklEQVR4Xu3UQQrCMBRF0cK1J3YQyF5z6jYv3Q0W3gQ6b8IYc3ov2Jf6n8A0Yq1nG2mZfE8y2GQkAAAAAAAAAANhK0ZL8b3xP2m5b4+0O8S9I3o9b3r8CwV8u0aH3bX8wE4WqgX3m4v3zO2KJ6l4yT4xvCw0b1q2c2w8bqQO3vFf0u8wUo5L3a8b0n2l5yq9Kf4zvCw0f1q2s2w0bpQO7PFv0s8wco4b3a8b0n2k5yq9Kf4zvCw0f1q2s2w0bpQO7PFv0s8wYgAAAAAAAAAAAACw9wG0qN2b2l3cMQAAAABJRU5ErkJggg==');
        $guests = ['Alex', 'Marie', 'Lukas', 'Lena', 'Tom', 'Sophie', 'Jonas', 'Mia'];

        foreach (range(1, 24) as $i) {
            $task = $tasks->random();
            $fileName = 'photo_demo_'.Str::random(6).'.png';
            $thumbName = 'thumb_demo_'.Str::random(6).'.png';
            Storage::disk('public')->put('photos/'.$fileName, $png);
            Storage::disk('public')->put('thumbnails/'.$thumbName, $png);

            Photo::create([
                'event_id' => $event->id,
                'emotion_id' => $task->emotion_id,
                'task_id' => $task->id,
                'guest_name' => $guests[array_rand($guests)],
                'file_path' => 'photos/'.$fileName,
                'thumbnail_path' => 'thumbnails/'.$thumbName,
                'likes_count' => rand(0, 7),
                'metadata' => ['seeded' => true],
                'created_at' => now()->subMinutes(rand(1, 180)),
                'updated_at' => now(),
            ]);
        }
    }
}

