<?php

namespace Tests\Feature;

use App\Models\Label;
use App\Models\Song;
use App\Models\SongArrangement;
use App\Models\SongArrangementLabel;
use App\Models\SongSection;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;

class ArrangementControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_create_arrangement_uses_default_song_group_ordering(): void
    {
        [$song, $normal] = $this->createSongWithDefaultArrangement();
        $user = User::factory()->create();
        Auth::login($user);

        $response = $this->post(route('arrangements.store', $song), [
            'name' => 'Kurz',
        ]);

        $response->assertRedirect();

        $newArrangement = SongArrangement::query()
            ->where('song_id', $song->id)
            ->where('name', 'Kurz')
            ->first();

        $this->assertNotNull($newArrangement);

        $defaultLabelOrder = SongArrangementLabel::query()
            ->where('song_arrangement_id', $normal->id)
            ->orderBy('order')
            ->pluck('song_section_id')
            ->all();

        $newLabels = SongArrangementLabel::query()
            ->where('song_arrangement_id', $newArrangement->id)
            ->orderBy('order')
            ->pluck('song_section_id')
            ->all();

        $this->assertSame($defaultLabelOrder, $newLabels);
    }

    public function test_clone_arrangement_duplicates_current_arrangement_groups(): void
    {
        [$song, $normal] = $this->createSongWithDefaultArrangement();
        $user = User::factory()->create();
        Auth::login($user);

        $response = $this->post(route('arrangements.clone', $normal), [
            'name' => 'Normal Kopie',
        ]);

        $response->assertRedirect();

        $clone = SongArrangement::query()
            ->where('song_id', $song->id)
            ->where('name', 'Normal Kopie')
            ->first();

        $this->assertNotNull($clone);
        $this->assertFalse($clone->is_default);

        $originalLabels = SongArrangementLabel::query()
            ->where('song_arrangement_id', $normal->id)
            ->orderBy('order')
            ->pluck('song_section_id')
            ->all();

        $cloneLabels = SongArrangementLabel::query()
            ->where('song_arrangement_id', $clone->id)
            ->orderBy('order')
            ->pluck('song_section_id')
            ->all();

        $this->assertSame($originalLabels, $cloneLabels);
    }

    public function test_update_arrangement_reorders_and_persists_groups(): void
    {
        [, $normal, $verse, $chorus, $bridge] = $this->createSongWithDefaultArrangement();
        $user = User::factory()->create();
        Auth::login($user);

        $response = $this->put(route('arrangements.update', $normal), [
            'groups' => [
                ['section_id' => $chorus->id, 'order' => 1],
                ['section_id' => $bridge->id, 'order' => 2],
                ['section_id' => $verse->id, 'order' => 3],
                ['section_id' => $chorus->id, 'order' => 4],
            ],
        ]);

        $response->assertRedirect();

        $updated = SongArrangementLabel::query()
            ->where('song_arrangement_id', $normal->id)
            ->orderBy('order')
            ->pluck('song_section_id')
            ->all();

        $this->assertSame([
            $chorus->id,
            $bridge->id,
            $verse->id,
            $chorus->id,
        ], $updated);
    }

    public function test_cannot_delete_the_last_arrangement_of_a_song(): void
    {
        [$song, $normal] = $this->createSongWithDefaultArrangement();
        $user = User::factory()->create();
        Auth::login($user);

        $this->assertSame(1, $song->arrangements()->count());

        $response = $this->delete(route('arrangements.destroy', $normal));

        $response->assertRedirect();
        $response->assertSessionHas('error', 'Das letzte Arrangement kann nicht gelöscht werden.');

        $this->assertTrue(SongArrangement::query()->whereKey($normal->id)->exists());
        $this->assertSame(1, $song->arrangements()->count());
    }

    private function createSongWithDefaultArrangement(): array
    {
        $song = Song::factory()->create();

        $verseLabel = Label::factory()->create(['name' => 'Verse 1']);
        $chorusLabel = Label::factory()->create(['name' => 'Chorus']);
        $bridgeLabel = Label::factory()->create(['name' => 'Bridge']);

        $verse = SongSection::factory()->create(['song_id' => $song->id, 'label_id' => $verseLabel->id, 'order' => 1]);
        $chorus = SongSection::factory()->create(['song_id' => $song->id, 'label_id' => $chorusLabel->id, 'order' => 2]);
        $bridge = SongSection::factory()->create(['song_id' => $song->id, 'label_id' => $bridgeLabel->id, 'order' => 3]);

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

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

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

        SongArrangementLabel::factory()->create([
            'song_arrangement_id' => $normal->id,
            'song_section_id' => $verse->id,
            'order' => 3,
        ]);

        return [$song, $normal, $verse, $chorus, $bridge];
    }
}
