<?php

namespace Database\Seeders;

use App\Models\InviteLayout;
use App\Support\JoinTokenLayoutRegistry;
use Illuminate\Database\Seeder;
use ReflectionClass;

class InviteLayoutSeeder extends Seeder
{
    public function run(): void
    {
        $reflection = new ReflectionClass(JoinTokenLayoutRegistry::class);
        $layoutsConst = $reflection->getReflectionConstant('LAYOUTS');
        $fallbackLayouts = $layoutsConst ? $layoutsConst->getValue() : [];
        $qrSizeOverrides = [
            'evergreen-vows' => 640,
            'midnight-gala' => 640,
            'garden-brunch' => 660,
            'sparkler-soiree' => 680,
            'confetti-bash' => 680,
        ];
        $defaultQrSize = 640;
        $targetSvgWidth = 1240;
        $targetSvgHeight = 1754;

        foreach ($fallbackLayouts as $layout) {
            $layoutId = $layout['id'] ?? null;
            $forcedQrSize = $qrSizeOverrides[$layoutId] ?? $defaultQrSize;
            $existingQrSize = (int) ($layout['qr']['size_px'] ?? $layout['qr_size_px'] ?? 0);
            $qrSize = max($existingQrSize, $forcedQrSize);

            $existingSvgWidth = (int) ($layout['svg']['width'] ?? $layout['svg_width'] ?? 0);
            $existingSvgHeight = (int) ($layout['svg']['height'] ?? $layout['svg_height'] ?? 0);
            $svgWidth = max($existingSvgWidth, $targetSvgWidth);
            $svgHeight = max($existingSvgHeight, $targetSvgHeight);

            $preview = [
                'background' => $layout['background'] ?? null,
                'background_gradient' => $layout['background_gradient'] ?? null,
                'accent' => $layout['accent'] ?? null,
                'secondary' => $layout['secondary'] ?? null,
                'text' => $layout['text'] ?? null,
                'badge' => $layout['badge'] ?? null,
                'qr' => ['size_px' => $qrSize],
                'svg' => ['width' => $svgWidth, 'height' => $svgHeight],
            ];

            $options = [
                'badge_label' => $layout['badge_label'] ?? 'Digitale Gästebox',
                'instructions_heading' => $layout['instructions_heading'] ?? "So funktioniert's",
                'link_heading' => $layout['link_heading'] ?? 'Alternative zum Einscannen',
                'cta_label' => $layout['cta_label'] ?? 'Scan mich & starte direkt',
                'cta_caption' => $layout['cta_caption'] ?? 'Scan mich & starte direkt',
                'link_label' => $layout['link_label'] ?? null,
                'logo_url' => $layout['logo_url'] ?? null,
                'formats' => $layout['formats'] ?? ['pdf', 'png'],
            ];

            InviteLayout::updateOrCreate(
                ['slug' => $layout['id']],
                [
                    'name' => $layout['name'],
                    'subtitle' => $layout['subtitle'] ?? null,
                    'description' => $layout['description'] ?? null,
                    'paper' => $layout['paper'] ?? 'a4',
                    'orientation' => $layout['orientation'] ?? 'portrait',
                    'preview' => $preview,
                    'layout_options' => $options,
                    'instructions' => $layout['instructions'] ?? [],
                    'is_active' => true,
                ]
            );
        }
    }
}
