<?php

namespace Tests\Feature\Tenant;

use App\Models\Event;
use Illuminate\Support\Arr;

class EventInviteAdvancedLayoutTest extends TenantTestCase
{
    public function test_advanced_layout_elements_render_in_exports(): void
    {
        $event = Event::factory()
            ->for($this->tenant)
            ->create([
                'name' => ['de' => 'Canvas Test Event'],
                'slug' => 'canvas-test-event',
            ]);

        $joinToken = $event->joinTokens()->firstOrFail();

        $payload = [
            'metadata' => [
                'layout_customization' => [
                    'mode' => 'advanced',
                    'layout_id' => 'evergreen-vows',
                    'headline' => 'Konva Test Headline',
                    'link_label' => 'fotospiel.de/einladung',
                    'badge_label' => 'Fotobox',
                    'cta_label' => 'Los gehts',
                    'accent_color' => '#FF5A8D',
                    'text_color' => '#111111',
                    'background_color' => '#FFFFFF',
                    'elements' => [
                        [
                            'id' => 'headline',
                            'type' => 'headline',
                            'x' => 140,
                            'y' => 280,
                            'width' => 620,
                            'height' => 200,
                            'font_size' => 68,
                            'align' => 'left',
                        ],
                        [
                            'id' => 'description',
                            'type' => 'description',
                            'x' => 140,
                            'y' => 540,
                            'width' => 620,
                            'height' => 280,
                            'font_size' => 28,
                            'content' => 'Zeig uns deine Lieblingsmomente & erfülle die Tages-Challenges!',
                        ],
                        [
                            'id' => 'qr',
                            'type' => 'qr',
                            'x' => 720,
                            'y' => 460,
                            'width' => 500,
                            'height' => 500,
                        ],
                        [
                            'id' => 'link',
                            'type' => 'link',
                            'x' => 820,
                            'y' => 1000,
                            'width' => 380,
                            'height' => 120,
                            'align' => 'center',
                        ],
                        [
                            'id' => 'cta',
                            'type' => 'cta',
                            'x' => 820,
                            'y' => 1150,
                            'width' => 380,
                            'height' => 120,
                        ],
                    ],
                ],
            ],
        ];

        $this->authenticatedRequest(
            'PATCH',
            "/api/v1/tenant/events/{$event->slug}/join-tokens/{$joinToken->id}",
            $payload
        )->assertOk();

        $response = $this->authenticatedRequest(
            'GET',
            "/api/v1/tenant/events/{$event->slug}/join-tokens/{$joinToken->id}/layouts/evergreen-vows.pdf",
            [],
            ['Accept' => 'application/pdf']
        );

        $response->assertOk();
        $response->assertHeader('Content-Type', 'application/pdf');

        $pngResponse = $this->authenticatedRequest(
            'GET',
            "/api/v1/tenant/events/{$event->slug}/join-tokens/{$joinToken->id}/layouts/evergreen-vows.png",
            [],
            ['Accept' => 'image/png']
        );

        $pngResponse->assertOk();
        $pngResponse->assertHeader('Content-Type', 'image/png');

        $refreshed = $event->fresh()->joinTokens()->first();
        $meta = Arr::get($refreshed->metadata, 'layout_customization');

        $this->assertSame('advanced', $meta['mode']);
        $this->assertCount(5, $meta['elements']);
    }
}
