<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('emotions')) {
            return;
        }

        $existing = DB::table('emotions')->count();
        if ($existing > 0) {
            return;
        }

        $now = now();
        $defaults = [
            ['de' => 'Liebe', 'en' => 'Love', 'color' => '#f472b6'],
            ['de' => 'Freude', 'en' => 'Joy', 'color' => '#10b981'],
            ['de' => 'Rührung', 'en' => 'Touched', 'color' => '#60a5fa'],
            ['de' => 'Nostalgie', 'en' => 'Nostalgia', 'color' => '#a855f7'],
            ['de' => 'Überraschung', 'en' => 'Surprise', 'color' => '#f59e0b'],
        ];

        $rows = [];
        foreach ($defaults as $index => $emotion) {
            $rows[] = [
                'name' => json_encode($emotion),
                'description' => json_encode([]),
                'icon' => 'lucide-smile',
                'color' => $emotion['color'],
                'sort_order' => $index,
                'is_active' => true,
                'tenant_id' => null,
                'created_at' => $now,
                'updated_at' => $now,
            ];
        }

        DB::table('emotions')->insert($rows);
    }

    public function down(): void
    {
        if (! Schema::hasTable('emotions')) {
            return;
        }

        DB::table('emotions')->truncate();
    }
};
