<?php

namespace Tests\Feature;

use App\Http\Controllers\TranslationController;
use App\Models\Label;
use App\Models\Song;
use App\Models\SongArrangement;
use App\Models\SongArrangementLabel;
use App\Models\SongSlide;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Tests\TestCase;

final class TranslatePageTest extends TestCase
{
    use RefreshDatabase;

    public function test_translate_page_response_contains_ordered_groups_and_slides(): void
    {
        $song = Song::factory()->create([
            'title' => 'Grosser Gott',
        ]);

        $arrangement = SongArrangement::factory()->create([
            'song_id' => $song->id,
            'name' => 'Normal',
            'is_default' => true,
        ]);

        $labelLater = Label::factory()->create([
            'name' => 'Refrain',
            'color' => '#22c55e',
        ]);

        $labelFirst = Label::factory()->create([
            'name' => 'Strophe 1',
            'color' => '#0ea5e9',
        ]);
        $sectionFirst = songSectionFor($song, $labelFirst, 1);
        $sectionLater = songSectionFor($song, $labelLater, 2);

        SongArrangementLabel::factory()->create([
            'song_arrangement_id' => $arrangement->id,
            'song_section_id' => $sectionFirst->id,
            'order' => 1,
        ]);

        SongArrangementLabel::factory()->create([
            'song_arrangement_id' => $arrangement->id,
            'song_section_id' => $sectionLater->id,
            'order' => 2,
        ]);

        SongSlide::factory()->create([
            'song_section_id' => $sectionFirst->id,
            'order' => 2,
            'text_content' => "Zeile A\nZeile B",
            'text_content_translated' => "Line A\nLine B",
        ]);

        SongSlide::factory()->create([
            'song_section_id' => $sectionFirst->id,
            'order' => 1,
            'text_content' => "Zeile C\nZeile D\nZeile E",
            'text_content_translated' => null,
        ]);

        SongSlide::factory()->create([
            'song_section_id' => $sectionLater->id,
            'order' => 1,
            'text_content' => 'Refrain',
            'text_content_translated' => 'Chorus',
        ]);

        $controller = app(TranslationController::class);
        $inertiaResponse = $controller->page($song);

        $request = Request::create('/songs/'.$song->id.'/translate', 'GET');
        $request->headers->set('X-Inertia', 'true');

        $response = $inertiaResponse->toResponse($request);

        $this->assertInstanceOf(JsonResponse::class, $response);

        $payload = json_decode((string) $response->getContent(), true, 512, JSON_THROW_ON_ERROR);

        $this->assertSame('Songs/Translate', $payload['component']);
        $this->assertSame($song->id, $payload['props']['song']['id']);
        $this->assertSame('Grosser Gott', $payload['props']['song']['title']);
        $this->assertCount(2, $payload['props']['song']['groups']);
        $this->assertSame('Strophe 1', $payload['props']['song']['groups'][0]['name']);
        $this->assertSame(1, $payload['props']['song']['groups'][0]['slides'][0]['order']);
        $this->assertSame("Zeile C\nZeile D\nZeile E", $payload['props']['song']['groups'][0]['slides'][0]['text_content']);
        $this->assertSame(2, $payload['props']['song']['groups'][0]['slides'][1]['order']);
        $this->assertSame("Zeile A\nZeile B", $payload['props']['song']['groups'][0]['slides'][1]['text_content']);
        $this->assertSame('Refrain', $payload['props']['song']['groups'][1]['name']);
        $this->assertSame('Chorus', $payload['props']['song']['groups'][1]['slides'][0]['text_content_translated']);
    }
}
