<?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 EventAchievementsLocaleTest extends TestCase
{
    use RefreshDatabase;

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

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

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

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

        $task = Task::factory()->create([
            'tenant_id' => $event->tenant_id,
            'title' => ['de' => 'Feuerring', 'en' => 'Fire Ring'],
            'description' => ['de' => 'DE Beschr', 'en' => 'EN Desc'],
            '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,
            'likes_count' => 42,
            'guest_name' => 'LocaleTester',
            'status' => 'approved',
        ]);

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

        $responseEn->assertOk();
        $responseEn->assertHeader('X-Content-Locale', 'en');
        $responseEn->assertJsonPath('highlights.top_photo.task', 'Fire Ring');
        $responseEn->assertJsonPath('highlights.trending_emotion.name', 'Joy');

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

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