<?php

namespace Tests\Feature\Api\Event;

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

class EventTasksLocaleTest extends TestCase
{
    use RefreshDatabase;

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

        $event = Event::factory()->create([
            'default_locale' => 'de',
            'status' => 'published',
        ]);
        $token = app(EventJoinTokenService::class)->createToken($event, ['label' => 'test']);
        $plainToken = $token->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 Beschreibung', 'en' => 'EN Description'],
            'example_text' => ['de' => 'DE Anleitung', 'en' => 'EN Instructions'],
            'emotion_id' => $emotion->id,
        ]);

        $collection = TaskCollection::factory()->create([
            'tenant_id' => $event->tenant_id,
        ]);

        $collection->tasks()->attach($task->id, ['sort_order' => 1]);
        $collection->events()->attach($event->id, ['sort_order' => 1]);

        $response = $this->getJson("/api/v1/events/{$plainToken}/tasks?locale=en");

        $response->assertOk();
        $response->assertHeader('X-Content-Locale', 'en');
        $response->assertJsonCount(1, 'data');
        $response->assertJsonPath('data.0.id', $task->id);
        $response->assertJsonPath('data.0.title', 'Kiss Moment');
        $response->assertJsonPath('data.0.description', 'EN Description');
        $response->assertJsonPath('data.0.instructions', 'EN Instructions');
        $response->assertJsonPath('data.0.duration', 3);
        $response->assertJsonPath('data.0.is_completed', false);
        $response->assertJsonPath('data.0.emotion.slug', 'emotion-'.$emotion->id);
    }

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

        $event = Event::factory()->create([
            'default_locale' => 'de',
            'status' => 'published',
        ]);
        $token = app(EventJoinTokenService::class)->createToken($event, ['label' => 'fallback']);
        $plainToken = $token->plain_token;

        $task = Task::factory()->create([
            'tenant_id' => $event->tenant_id,
            'title' => ['de' => 'Aufgabe', 'en' => 'Task'],
            'description' => ['de' => 'Beschreibung', 'en' => 'Description'],
            'example_text' => ['de' => 'Beispiel', 'en' => 'Example'],
        ]);

        $collection = TaskCollection::factory()->create([
            'tenant_id' => $event->tenant_id,
        ]);

        $collection->tasks()->attach($task->id, ['sort_order' => 1]);
        $collection->events()->attach($event->id, ['sort_order' => 1]);

        $response = $this->withHeaders(['Accept-Language' => 'zz-ZZ'])
            ->getJson("/api/v1/events/{$plainToken}/tasks?locale=it");

        $response->assertOk();
        $response->assertHeader('X-Content-Locale', 'de');
        $response->assertJsonPath('data.0.title', 'Aufgabe');
    }
}
