<?php

namespace Tests\Feature\Tenant;

use App\Models\Event;
use App\Models\GuestNotification;
use App\Models\GuestPolicySetting;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;

class GuestNotificationBroadcastTest extends TenantTestCase
{
    public function test_admin_can_list_guest_notifications(): void
    {
        $event = Event::factory()->for($this->tenant)->create();

        GuestNotification::factory()->count(2)->create([
            'tenant_id' => $this->tenant->id,
            'event_id' => $event->id,
        ]);

        $response = $this->authenticatedRequest('GET', "/api/v1/tenant/events/{$event->slug}/guest-notifications");

        $response->assertOk();
        $response->assertJsonCount(2, 'data');
    }

    public function test_admin_can_create_broadcast_notification(): void
    {
        $event = Event::factory()->for($this->tenant)->create();

        $payload = [
            'title' => 'Gruppenfoto gleich',
            'message' => 'Kommt bitte zur Bühne, wir starten in 5 Minuten.',
            'type' => 'broadcast',
            'cta' => [
                'label' => 'Route öffnen',
                'url' => 'https://example.com/map',
            ],
            'expires_in_minutes' => 120,
        ];

        $response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/guest-notifications", $payload);

        $response->assertCreated();
        $response->assertJsonPath('data.title', 'Gruppenfoto gleich');

        $this->assertDatabaseHas('guest_notifications', [
            'event_id' => $event->id,
            'title' => 'Gruppenfoto gleich',
        ]);
    }

    public function test_guest_notification_default_ttl_applies(): void
    {
        $now = now();
        Carbon::setTestNow($now);

        GuestPolicySetting::flushCache();
        GuestPolicySetting::query()->create([
            'id' => 1,
            'guest_notification_ttl_hours' => 6,
        ]);

        $event = Event::factory()->for($this->tenant)->create();

        $payload = [
            'title' => 'Upload-Reminder',
            'message' => 'Bitte lade deine letzten Fotos hoch.',
            'type' => 'broadcast',
        ];

        $response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/guest-notifications", $payload);

        $response->assertCreated();

        $notification = GuestNotification::query()->where('event_id', $event->id)->first();

        $this->assertNotNull($notification);
        $this->assertNotNull($notification->expires_at);
        $this->assertEquals($now->copy()->addHours(6)->toDateTimeString(), $notification->expires_at?->toDateTimeString());

        Carbon::setTestNow();
    }

    public function test_admin_cannot_access_foreign_event(): void
    {
        $otherTenantEvent = Event::factory()->create([
            'slug' => 'foreign-'.Str::random(6),
        ]);

        $response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$otherTenantEvent->slug}/guest-notifications", [
            'title' => 'Hi',
            'message' => 'Nope',
        ]);

        $response->assertStatus(403);
    }
}
