<?php

namespace Tests\Feature\Api\Event;

use App\Models\Event;
use App\Models\EventPackage;
use App\Models\EventType;
use App\Models\Package;
use App\Services\EventJoinTokenService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Tests\TestCase;

class EventBrandingResponseTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_returns_extended_branding_shape_with_logo_and_buttons(): void
    {
        $package = Package::factory()->create([
            'branding_allowed' => true,
        ]);

        $eventType = EventType::factory()->create([
            'icon' => 'party',
        ]);

        $event = Event::factory()->create([
            'status' => 'published',
            'event_type_id' => $eventType->id,
            'settings' => [
                'branding' => [
                    'palette' => [
                        'primary' => '#123456',
                        'secondary' => '#654321',
                        'background' => '#f0f0f0',
                        'surface' => '#ffffff',
                    ],
                    'typography' => [
                        'heading' => 'Playfair Display',
                        'body' => 'Inter, sans-serif',
                        'size' => 'l',
                    ],
                    'logo' => [
                        'mode' => 'upload',
                        'value' => 'branding/test.png',
                        'position' => 'center',
                        'size' => 'l',
                    ],
                    'buttons' => [
                        'style' => 'outline',
                        'radius' => 18,
                        'primary' => '#ff0000',
                        'secondary' => '#00ff00',
                        'link_color' => '#111111',
                    ],
                    'mode' => 'dark',
                ],
            ],
        ]);

        EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => 0,
            'purchased_at' => now(),
            'gallery_expires_at' => now()->addDays(7),
        ]);

        $token = app(EventJoinTokenService::class)->createToken($event, ['label' => 'branding-check']);
        $response = $this->getJson('/api/v1/events/'.$token->plain_token);

        $response->assertOk();
        $response->assertJsonPath('branding.palette.primary', '#123456');
        $response->assertJsonPath('branding.palette.surface', '#ffffff');
        $response->assertJsonPath('branding.typography.heading', 'Playfair Display');
        $response->assertJsonPath('branding.typography.size', 'l');
        $response->assertJsonPath('branding.logo.mode', 'upload');
        $this->assertStringContainsString('/api/v1/branding/asset/', (string) $response->json('branding.logo.value'));
        $response->assertJsonPath('branding.logo.position', 'center');
        $response->assertJsonPath('branding.buttons.style', 'outline');
        $response->assertJsonPath('branding.buttons.radius', 18);
        $response->assertJsonPath('branding.mode', 'dark');
    }

    public function test_it_uses_tenant_branding_when_use_default_flag_is_enabled(): void
    {
        $package = Package::factory()->create([
            'branding_allowed' => true,
        ]);

        $event = Event::factory()->create([
            'status' => 'published',
            'settings' => [
                'branding' => [
                    'use_default_branding' => true,
                    'primary_color' => '#000000',
                    'secondary_color' => '#111111',
                ],
            ],
        ]);

        $event->tenant->update([
            'settings' => [
                'branding' => [
                    'primary_color' => '#abcdef',
                    'secondary_color' => '#fedcba',
                    'background_color' => '#ffffff',
                    'buttons' => [
                        'style' => 'filled',
                        'radius' => 8,
                    ],
                ],
            ],
        ]);

        EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => 0,
            'purchased_at' => now(),
            'gallery_expires_at' => now()->addDays(14),
        ]);

        $token = app(EventJoinTokenService::class)->createToken($event, ['label' => 'branding-default']);

        $response = $this->getJson('/api/v1/events/'.$token->plain_token);

        $response->assertOk();
        $response->assertJsonPath('branding.use_default_branding', true);
        $response->assertJsonPath('branding.primary_color', '#abcdef');
        $response->assertJsonPath('branding.secondary_color', '#fedcba');
        $response->assertJsonPath('branding.buttons.radius', 8);
    }

    public function test_branding_asset_uses_public_disk(): void
    {
        Storage::fake('public');
        Storage::disk('public')->put('branding/logos/test.png', 'logo');

        $url = URL::temporarySignedRoute('api.v1.branding.asset', now()->addMinutes(5), [
            'path' => 'branding/logos/test.png',
        ]);

        $response = $this->get($url);

        $response->assertOk();
        $this->assertSame('logo', $response->streamedContent());
    }
}
