<?php

namespace Tests\Feature\Api\Event;

use App\Models\Event;
use App\Models\Photo;
use App\Services\EventJoinTokenService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PendingUploadsTest extends TestCase
{
    use RefreshDatabase;

    public function test_pending_uploads_returns_only_device_pending_photos(): void
    {
        $event = Event::factory()->create([
            'status' => 'published',
        ]);

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

        $pendingMine = Photo::factory()->for($event)->create([
            'status' => 'pending',
            'created_by_device_id' => 'device-123',
        ]);

        Photo::factory()->for($event)->create([
            'status' => 'pending',
            'created_by_device_id' => 'device-999',
        ]);

        Photo::factory()->for($event)->create([
            'status' => 'approved',
            'created_by_device_id' => 'device-123',
        ]);

        $response = $this->withHeaders(['X-Device-Id' => 'device-123'])
            ->getJson("/api/v1/events/{$token}/pending-photos");

        $response->assertOk();
        $response->assertJsonCount(1, 'data');
        $response->assertJsonFragment([
            'id' => $pendingMine->id,
            'status' => 'pending',
        ]);
        $response->assertJson(['meta' => ['total_count' => 1]]);
        $this->assertNotEmpty($response->json('data.0.thumbnail_url'));
    }

    public function test_pending_uploads_returns_empty_without_device_id(): void
    {
        $event = Event::factory()->create([
            'status' => 'published',
        ]);

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

        Photo::factory()->for($event)->create([
            'status' => 'pending',
            'created_by_device_id' => 'device-123',
        ]);

        $response = $this->getJson("/api/v1/events/{$token}/pending-photos");

        $response->assertOk();
        $response->assertJson(['data' => [], 'meta' => ['total_count' => 0]]);
    }
}
