<?php

namespace Tests\Feature;

use App\Models\Member;
use App\Models\Organization;
use App\Models\Room;
use App\Models\User;
use App\Services\Integrations\EventsLinkService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class EventsLinkServiceTest extends TestCase
{
    use RefreshDatabase;

    protected User $user;

    protected Organization $organization;

    protected function setUp(): void
    {
        parent::setUp();

        $this->user = User::create([
            'public_id' => 'test-user-events-link',
            'name' => 'Test User',
            'email' => 'events-link@example.com',
        ]);

        $this->organization = Organization::create([
            'owner_ref' => $this->user->public_id,
            'name' => 'Test Org',
            'slug' => 'test-org',
            'timezone' => 'UTC',
            'settings' => ['onboarded' => true],
        ]);

        Member::create([
            'owner_ref' => $this->user->public_id,
            'organization_id' => $this->organization->id,
            'user_ref' => $this->user->public_id,
            'role' => 'owner',
        ]);

        config([
            'meet.events_api_url' => 'https://events.test/api/service/v1',
            'meet.events_api_key' => 'test-events-key',
        ]);
    }

    public function test_link_stores_programme_snapshot_on_room_from_events_response(): void
    {
        Http::fake([
            'https://events.test/api/service/v1/events/42/link-room' => Http::response([
                'event' => [
                    'id' => 42,
                    'title' => 'Summit',
                    'entity_id' => 'vs-abc',
                    'registration_url' => 'https://events.test/e/summit',
                    'short_code' => 'summit',
                    'programme_snapshot' => [
                        'title' => 'Summit programme',
                        'days' => [
                            [
                                'label' => 'Day 1',
                                'items' => [
                                    ['time' => '09:00', 'title' => 'Opening keynote', 'host' => 'Alex'],
                                ],
                            ],
                        ],
                    ],
                    'linked_programme_items' => [
                        ['time' => '09:00', 'title' => 'Opening keynote', 'host' => 'Alex'],
                    ],
                ],
            ], 200),
        ]);

        $room = Room::create([
            'owner_ref' => $this->user->public_id,
            'organization_id' => $this->organization->id,
            'host_user_ref' => $this->user->public_id,
            'title' => 'Summit keynote',
            'type' => 'town_hall',
            'status' => 'scheduled',
            'scheduled_at' => now()->addDay(),
            'timezone' => 'UTC',
            'settings' => config('meet.default_settings'),
        ]);

        app(EventsLinkService::class)->link($room, $this->user, 42);

        $room->refresh();

        $this->assertSame('events', $room->source['app'] ?? null);
        $this->assertSame('Summit programme', $room->setting('programme_snapshot')['title'] ?? null);
        $this->assertSame('Opening keynote', $room->setting('linked_programme_items')[0]['title'] ?? null);
    }
}
