<?php

namespace Tests\Feature;

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

final class SongsBlockTest extends TestCase
{
    use RefreshDatabase;

    public function test_songs_block_shows_unmatched_song_with_matching_options(): void
    {
        $user = User::factory()->create();
        $service = Service::factory()->create();

        Song::factory()->create([
            'title' => 'Amazing Grace',
            'ccli_id' => '22025',
        ]);

        Song::factory()->create([
            'title' => 'How Great Is Our God',
            'ccli_id' => '4348399',
        ]);

        ServiceSong::factory()->create([
            'service_id' => $service->id,
            'order' => 2,
            'song_id' => null,
            'cts_song_name' => 'Unmatched Song',
            'cts_ccli_id' => '123456',
        ]);

        ServiceSong::factory()->create([
            'service_id' => $service->id,
            'order' => 1,
            'song_id' => null,
            'cts_song_name' => 'First Song',
            'cts_ccli_id' => '654321',
        ]);

        Auth::login($user);

        $response = $this->get(route('services.edit', $service));

        $response->assertOk();
        $response->assertInertia(
            fn ($page) => $page
                ->component('Services/Edit')
                ->has('serviceSongs', 2)
                ->where('serviceSongs.0.cts_song_name', 'First Song')
                ->where('serviceSongs.0.song_id', null)
                ->where('serviceSongs.1.cts_song_name', 'Unmatched Song')
                ->where('serviceSongs.1.song_id', null)
                ->has('songsCatalog')
                ->where('songsCatalog', fn ($songsCatalog) => collect($songsCatalog)->contains(
                    fn (array $song) => $song['title'] === 'Amazing Grace' && $song['ccli_id'] === '22025'
                ))
        );
    }

    public function test_songs_block_provides_matched_song_data_for_arrangement_configurator_and_translation_toggle(): void
    {
        $user = User::factory()->create();
        $service = Service::factory()->create();

        $song = Song::factory()->create([
            'title' => 'Living Hope',
            'ccli_id' => '7106807',
            'has_translation' => true,
        ]);

        $verse = Label::factory()->create([
            'name' => 'Strophe 1',
            'color' => '#3B82F6',
        ]);

        $chorus = Label::factory()->create([
            'name' => 'Refrain',
            'color' => '#10B981',
        ]);

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

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

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

        ServiceSong::factory()->create([
            'service_id' => $service->id,
            'order' => 1,
            'song_id' => $song->id,
            'song_arrangement_id' => $normal->id,
            'use_translation' => true,
            'cts_song_name' => 'Living Hope',
            'cts_ccli_id' => '7106807',
        ]);

        Auth::login($user);

        $response = $this->get(route('services.edit', $service));

        $response->assertOk();
        $response->assertInertia(
            fn ($page) => $page
                ->component('Services/Edit')
                ->where('serviceSongs.0.song_id', $song->id)
                ->where('serviceSongs.0.song.has_translation', true)
                ->where('serviceSongs.0.song.arrangements.0.name', 'Normal')
                ->where('serviceSongs.0.song.arrangements.0.groups.0.name', 'Strophe 1')
                ->where('serviceSongs.0.song.groups.0.name', 'Strophe 1')
                ->where('serviceSongs.0.song_arrangement_id', $normal->id)
        );
    }
}
