<?php

namespace Tests\Feature\Api\Event;

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

class EventPhotosLocaleTest extends TestCase
{
    use RefreshDatabase;

    public function test_photos_endpoint_localizes_task_titles(): void
    {
        config(['app.supported_locales' => ['de', 'en']]);

        $event = Event::factory()->create([
            'status' => 'published',
            'default_locale' => 'de',
        ]);

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

        $emotion = Emotion::factory()->create([
            'name' => ['de' => 'Freude', 'en' => 'Joy'],
        ]);

        $task = Task::factory()->create([
            'tenant_id' => $event->tenant_id,
            'title' => ['de' => 'Kussmoment', 'en' => 'Kiss Moment'],
            'description' => ['de' => 'DE', 'en' => 'EN'],
            'example_text' => ['de' => 'DE Example', 'en' => 'EN Example'],
        ]);

        Photo::factory()->for($event)->create([
            'tenant_id' => $event->tenant_id,
            'task_id' => $task->id,
            'emotion_id' => $emotion->id,
            'created_at' => now(),
            'status' => 'approved',
        ]);

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

        $responseEn->assertOk();
        $responseEn->assertHeader('X-Content-Locale', 'en');
        $responseEn->assertJsonFragment(['task_title' => 'Kiss Moment']);

        $etag = $responseEn->headers->get('ETag');
        $this->assertNotEmpty($etag);

        $responseDe = $this->withHeaders([
            'X-Device-Id' => 'device-123',
            'Accept-Language' => 'de-DE',
        ])
            ->getJson("/api/v1/events/{$token}/photos?locale=it");

        $responseDe->assertOk();
        $responseDe->assertHeader('X-Content-Locale', 'de');
        $responseDe->assertJsonFragment(['task_title' => 'Kussmoment']);

        $this->withHeaders([
            'X-Device-Id' => 'device-123',
            'If-None-Match' => $etag,
        ])->getJson("/api/v1/events/{$token}/photos?locale=en")
            ->assertStatus(304);
    }
}
